简体   繁体   English

从脚本中,在远程服务器上运行脚本并获取其输出

[英]From a script, run a script on a remote server and get its output

I am executing a Perl script. 我正在执行一个Perl脚本。 I have to SSH to a remote server and execute a python script. 我必须SSH到远程服务器并执行python脚本。 Below is the line of code that I am executing in the perl script. 下面是我在perl脚本中执行的代码行。 I know there are few options like backticks and system command to execute a python script but I have the script in remote server and I cannot use system or backticks command as it is not local machine. 我知道很少有诸如backticks和系统命令之类的选项来执行python脚本,但是我在远程服务器中拥有该脚本,由于它不是本地计算机,因此无法使用system或backticks命令。 I want the output of the command that I am executing through SSH at runtime. 我需要在运行时通过SSH执行的命令的输出。

   my $ssh = new Net::SSH::Expect();
    $ssh->login_username($server_login);
    $ssh->server($server_name);
    $ssh->login();
    my $home_directory= $ssh->pwd;

my $command=qq(/home/feroz/MyScript.py $ARG0 $ARG1 $ARG2 $ARG3 1)
    print "\n-------- BEGIN SCRIPT OUTPUT ----------\n";
    $ssh->exec($command)){

    print "\n -------- END SCRIPT OUTPUT ----------";

Please see Net::SSH::Expect documentation. 请参阅Net :: SSH :: Expect文档。 You aren't using it right 您没有正确使用它

use warnings;
use strict;
use Net::SSH::Expect;

my $ssh = Net::SSH::Expect->new (
    host     => 'host name', 
    user     => 'user name', 
    password => 'password', 
    raw_pty  => 1
);

my $greeting = $ssh->login();  # or $ssh->run_ssh(), if without password
print $greeting;

# Disable terminal translations and echo on the SSH server
$ssh->exec("stty raw -echo");

my $cmd = q( ... );

my $cmd_output = $ssh->exec($cmd);

print $cmd_output, "\n";

The exec method is one of the ways to get the command output. exec方法是获取命令输出的方法之一。 Some others are read_line , peek , eat , and there are yet other ways to interact with the command. 其他一些是read_linepeekeat ,还有其他与命令交互的方式。

Please add checks to see whether the login worked first. 请添加检查以查看登录是否首先生效。

This can accomplish a lot and it involves complexities. 这可以完成很多工作,并且涉及复杂性。 PLease read the documentation. 请阅读文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM