Hello Monks,

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

first script code and output:
#!/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
----------
Second workaround script
#!/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 ';

In reply to Capture FIX: .ssh/known_hosts message when using Net::SSH::Expect by avim1968

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.