user1500970
user1500970

Reputation: 107

SSHProcessError The ssh process was terminated in $ssh->waitfor

I am very new to Perl script and trying to write a perl code to ssh to a Router and then run scp export command on the router (scp export from router to a remote destination server).

#!/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('[email protected]\'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";

I am able to see that script logins to the router fine (I can see a new user on the router everytime I run the script). However, after few seconds, script terminates with error: SSHProcessError The ssh process was terminated. at cron1 line 22

What is wrong with line 22: $ssh->waitfor('[email protected]\'s password:\s*') or die "prompt 'password' not found";

Upvotes: 2

Views: 3146

Answers (2)

rmeden
rmeden

Reputation: 129

I ran into the same problem when $hostname wasn't valid. Doing a simple hostname check first fixed it for me..

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

update: It seems just about any ssh connection error will cause the SSHProcessError error.

Upvotes: 0

Abhimanyu Dutta
Abhimanyu Dutta

Reputation: 29

I used a different approach to login to a server. Have made a function out of this piece of code, works fine every time for me. See if this can help you out..

  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;
             }

Upvotes: 0

Related Questions