我对 Perl 脚本非常陌生,并尝试编写一个 perl 代码以 ssh 到路由器,然后在路由器上运行 scp export 命令(从路由器到远程目标服务器的 scp 导出)。
#!/usr/local/bin/perl
use Net::SSH::Expect;
use warnings;
$hostname = "Router";
my $ssh = Net::SSH::Expect->new (
host => $hostname,
password=> 'abcd',
user => 'admin',
raw_pty => 1,
timeout => 150,
);
my $login_output = $ssh->login();
if ($login_output !~ /Router/) {
die "Login has failed. Login output was $login_output";
}
$ssh->send("scp export log traffic start-time equal 2013/04/01\@00:00:00 to user\@192.168.1.1:<path> end-time equal 2013/04/01\@01:00:00",3);
$ssh->waitfor('user@192.168.1.1\'s password:\s*') or die "prompt 'password' not found";
$ssh->send("abcd");
$ssh->send("\n");
sleep 100;
my $logout = $ssh->close();
print "=" x 50;
print "\n";
我能够看到该脚本很好地登录到路由器(每次运行脚本时我都可以在路由器上看到一个新用户)。但是,几秒钟后,脚本终止并出现错误: SSHProcessError ssh 进程已终止。在 cron1 第 22 行
第 22 行有什么问题: $ssh->waitfor('user@192.168.1.1\'s password:\s*') or die "prompt 'password' not found";