I wrote a code that uses Net::SSH::Expect to access a device and then run commands. all is working well until it gets to a device that its key was changed. the script below is a part of the main script i just reduced it to the problematic part. I'm using "eval" around the $ssh-> login and i'm checking the @$ if there are errors the sub returns ERROR else it returns the $ssh. however @$ does not hold the FIX: .ssh/known_hosts as you can see the message comes just after i run $ssh->login.
I have made a work around for it as you can see it the second script "run_system_cmd.pl" below, that uses a "system call" to run the first script and capture its output the @array that
hold the hosts message init.
BUT i don't want to use the workaround i want that the first script would be able to capture the FIX message
so it can deal with it himself.
How can I capture the FIX: .ssh/known_hosts message
from within the first script.
thank you
Avi
----------#!/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) || ($logi +n_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
#!/usr/bin/perl -w #run_system_cmd.pl use strict; use Data::Dumper; my $debug =1; undef my @array; @array = `./tst_ssh_host_key.pl`; print Dumper @array; print "\n"; ---- workaround Run Output: ./run_system_cmd.pl $VAR1 = 'open_ssh,172.23.5.13,user,password '; $VAR2 = 'Before ssh->login '; $VAR3 = 'FIX: .ssh/known_hosts '; $VAR4 = 'After ssh->login '; $VAR5 = 'login_output= '; $VAR6 = 'global=SSHProcessError The ssh process was terminated. at ./tst_ssh_host_key.pl line 42 '; $VAR7 = ' '; $VAR8 = 'ssh=FAILED,no_login_output,end_login '; $VAR9 = 'DONE ';
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |