in reply to Net::SSH2 command output polling
Vins, Honestly I've always used "Net::SSH::Perl" module. Found the interaction with TTY/PTYs to be less troublesome. Not to mention retrieving large amounts of "sh arp" and "sh cam dyn" from IOS/CATOS devices. Here's a sample I use from my script for logging in. ############### $ssh = Net::SSH::Expect->new( host => $ip, user => $suser, password => $spass, timeout => 3, log_file => "$ofdir/$stime.$bname.$ip.dump_out.$^T.txt", raw_pty => 1); $ok = $ssh->run_ssh(); if(!(defined $ok) || ($ok eq "")) { $ok = 0; } else { $ok = 1; } if($debug == 1) { print "$meth... "; } $ok = $ssh->waitfor('key verification failed', 5); if((!$ok) || ($ok eq "")) { $ok = 0; } if($ok == 1) { if($debug == 1 ) { print "FOUND ERROR ID KEY FAILED\n"; } $fpssh = 1; } else { if($debug == 1) { print "DID NOT FIND.. CONTINUE\n"; } $fpssh = 0; } $ok = 0; if($fpssh == 1) { if($debug == 1) { print "FOUND host key failed PROMPT\n"; } $val = "host_key_failed_prompt"; $ssh->close(); exit; } #EO if($fpssh == 1) $meth = "FIND #"; if($debug == 1) { print "$meth... "; } $ok = $ssh->waitfor('#', 30, -re); if((!$ok) || ($ok eq "")) { $ok = 0; } if($ok == 1) { if($debug == 1) { print "GO\n"; } $fppt = 1; } else { if($debug == 1) { print "ERROR\n"; } $fppt = 0; } $ok = 0; if($fppt != 1) { $val = "did_not_find_en_prompt"; $ssh->close(); exit; } #EO if($fpps == 1) $meth = "SEND SH ARP"; if($debug == 1) { print "$meth... \n"; } $ssh->send($cmd); if($debug == 1) { print "GO\n"; } while(defined ($line = $ssh->read_line())) { if(!(defined $line) || ($line eq "")) { next; } if($debug == 1) { print "LINE: \"" . __LINE__ . "\" LINE: \"$line\"\n"; } push @outs, $line; } $ssh->send("\n\n"); ############## To show the current buffer from the script incase of problems using "expect" like interactions.. if($debug == 1) { print "PEEK: \"" . $ssh->peek(0) . "\" L: \"" . __LINE__ . "\"\n"; } -- Notice the line "while(defined ($line = $ssh->read_line()))". This is different from the original line in the documentation of "@outs = $ssh->waitfor('#', -re, 30);" the "read line" is more fault tolerant. -- I make sure each module (login, run command, exit) is their own section of code, having nested if statements is bad if your are using "parallel fork manager" and if you have to exit/next in the middle of the script. Joe
|
---|