2

我对 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";

4

2 回答 2

0

当 $hostname 无效时,我遇到了同样的问题。做一个简单的主机名检查首先为我修复它..

die "Host $hostname not found" unless gethostbyname($hostname);

更新:似乎任何 ssh 连接错误都会导致 SSHProcessError 错误。

于 2013-07-16T21:15:47.657 回答
0

我使用不同的方法登录到服务器。用这段代码制作了一个功能,每次对我来说都很好。看看能不能帮到你。。

  sub connect {

              my $host =shift;
              my $password=shift;
              my $user=shift;
              my $counter=0;
              my $login_output;
              $ssh = Net::SSH::Expect->new (
                                                host => "$host",
                                                password=> "$password",
                                                user => "$user",
                                                raw_pty => 1
                                           );

            $login_output = $ssh->run_ssh();

            LABLE_login:  # Lable used for looping
            $login_output=$ssh->read_all();


            if( $login_output =~ /yes/){  # To check if you are logging in for the first time
                                            $ssh->send("yes\n");
                                            sleep(2);
                                            $login_output=$ssh->read_all();
                                            if( $login_output =~ /Password/){
                                                                                    $ssh->send("$password\n");
                                                                            }
                                       }

            elsif( $login_output =~ /Password/)
                                                    {
                                                            $ssh->send("$password\n");

                                                    }
 else
                    {
                      sleep(2);
                      $counter++;
                      if($counter eq 3)
                            { print color('red');print "Cannot connect to host exiting now\n";print color('reset');exit();}
                      goto LABLE_login;
                    }

            $login_output=$ssh->send("$password\n");
            return $ssh;
             }
于 2014-08-22T05:12:10.780 回答