Linux 如何在远程设备上通过SSH运行命令?

Linux 如何在远程设备上通过SSH运行命令?,linux,perl,ssh,remote-access,remote-execution,Linux,Perl,Ssh,Remote Access,Remote Execution,如何在远程设备上通过SSH运行命令 我正在尝试下一个想法: 1: 2: 我还尝试通过Perl脚本运行命令: #!/bin/perl use strict; use Net::OpenSSH; my $hostname = "IP"; my $username = "user"; my $password = "password"; my $cmd = "command"; my $ssh = Net::OpenSSH->new(host=>"$hostname", user=&

如何在远程设备上通过SSH运行命令

我正在尝试下一个想法:

1:

2:

我还尝试通过Perl脚本运行命令:

#!/bin/perl
use strict;

use Net::OpenSSH;

my $hostname = "IP";
my $username = "user";
my $password = "password";
my $cmd = "command";

my $ssh = Net::OpenSSH->new(host=>"$hostname", user=>"$username", port=>22, password=>"$password");
$ssh->system($cmd);
但这种方法也不起作用:

[root@0400smarts-voip01 test]# ./test.pl
Connection to IP closed by remote host.

如何解决这个问题?

我觉得这不是编程问题。您的示例#1基本上是在远程系统上运行命令的正确方法。它可能无法工作的原因有很多,主要涉及远程系统的配置方式。考虑到您提供的信息量很小,没有办法知道您出了什么问题。因此,这里真正的问题是:“为什么我不能使用SSH登录到远程系统?”。您需要检查系统日志和配置,这确实需要您有权访问它。你真的能正确登录吗?你应该尝试的第一件事是在详细模式下运行
ssh
,它会告诉你它在后台做什么:
ssh-vvv…
#!/bin/perl
use strict;

use Net::OpenSSH;

my $hostname = "IP";
my $username = "user";
my $password = "password";
my $cmd = "command";

my $ssh = Net::OpenSSH->new(host=>"$hostname", user=>"$username", port=>22, password=>"$password");
$ssh->system($cmd);
[root@0400smarts-voip01 test]# ./test.pl
Connection to IP closed by remote host.