Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
来自Perl的Putty命令_Perl_Putty - Fatal编程技术网

来自Perl的Putty命令

来自Perl的Putty命令,perl,putty,Perl,Putty,我正在尝试从perl运行putty中的几个命令。现在我的代码让我进入putty,但我不确定如何从那里执行命令 my $PUTTY = 'C:\Users\Desktop\putty.exe'; my $dacPutty = "$PUTTY, -ssh username@server - l username -pw password"; system ($dacPutty); system (ls); 改用plink。( ) plink与putty位于同一目录中。通常在Perl中,最好在存

我正在尝试从perl运行putty中的几个命令。现在我的代码让我进入putty,但我不确定如何从那里执行命令

my $PUTTY = 'C:\Users\Desktop\putty.exe';
my $dacPutty = "$PUTTY, -ssh username@server - l username -pw password";
system ($dacPutty);

system (ls);
改用plink。( )
plink与putty位于同一目录中。

通常在Perl中,最好在存在Perl模块的地方使用Perl模块,而不是使用shell模块

使用模块更便于携带,并且通常可以让您进行更多的控制<代码>系统引入了许多安全漏洞,因此最好尽可能避免它

在本例中,使用Net::SSH::Perl

一旦安装:

use Net::SSH::Perl;
my $ssh = Net::SSH::Perl->new("host1");
$ssh->login("user1", "pass1");

$ssh->cmd("cd /some/dir");
$ssh->cmd("foo");
为确保可靠性,您应该实际检查每个命令的结果:

my ($stdout, $stderr, $exit) = $ssh->cmd("cd /some/dir");
unless ($exit == 0) {
     // Handle failed cd
}
文档指出,使用SSH-1时,每个
cmd
都会重新连接,因此上面的方法不起作用——您将
cd
放在一个shell中,然后
foo
放在一个全新的shell中。如果必须使用SSH-1,则需要执行以下操作:

$ssh->cmd("cd /some/dir; foo");

(即使您正在对plink进行
system
调用,您也可以使用类似的技巧)

因此您希望从Perl中控制putty ash会话?安装cygwin并使用其
ssh
OP有一个后续问题,但还没有足够的代表对您的答案发表评论:“我已经成功地开始使用plink,但是我仍然不确定如何直接从我的代码更改plink中的目录”-cbrunner9you-get-a-new-session-for-every-command即使重新使用连接,SSH-2也会出现这种情况。