#!/usr/bin/perl # tst_ssh_host_key.pl use strict; use warnings; use threads; use CGI qw/:standard/; use Net::SSH::Expect; use Getopt::Std; my $nl = "\n"; my $debug =""; my $ssh = open_ssh('172.23.5.13','user','password'); print "ssh=$ssh",$nl; print "DONE",$nl; exit; sub open_ssh { my $nl="\n"; my $debug =1; my $command_prompt = '>'; my $login_output =""; my $username; my $password; my $host_ip; my $ssh; my $Permission_denied = 'Permission denied'; ($host_ip,$username,$password) = @_; unless ($host_ip) {return ("FAILED-NO_HOST_IP,end_login");} if ($debug) {print "open_ssh,$host_ip,$username,$password",$nl;} $ssh = Net::SSH::Expect-> new ( host => $host_ip, user => $username, password => $password, timeout => 10, raw_pty => 1, debug => 1 ); print "Before ssh->login",$nl; eval { $login_output = $ssh ->login(); }; print "After ssh->login",$nl; if ($debug) {print "login_output=",$login_output,$nl;print "global=",$@, $nl;} if (($@) || ($login_output !~ /$command_prompt/)) { if (($@ =~ 'Error') || ($@ =~ 'Aborted') || ($@ =~ $Permission_denied) || ($login_output =~ $Permission_denied) || ($login_output !~ /$command_prompt/)) { unless ($login_output) {$login_output = 'no_login_output';} return ("FAILED,".$login_output.",end_login"); } } return $ssh; } -------------------------- Run Output : ./tst_ssh_host_key.pl open_ssh,172.23.5.13,user,password Before ssh->login FIX: .ssh/known_hosts After ssh->login login_output= global=SSHProcessError The ssh process was terminated. at ./tst_ssh_host_key.pl line 42 ssh=FAILED,no_login_output,end_login DONE