Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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










In reply to Re: Net::SSH2 command output polling by t_rex_joe
in thread Net::SSH2 command output polling by VinsWorldcom

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (7)
    As of 2014-12-20 03:52 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (95 votes), past polls