So, here is the situation.

Once upon a time, an organization decided to switch all of its network equipment (routers, switches) from Telnet to SSH. Try as we did, we could not get the necessary modules to install on our antiquated equipment so that we could just call Net::SSH::Perl.

So, we devised a cunning plan to use Net::Telnet to telnet to a unix server that does have SSH capability. This has worked marvelously, as we no longer had to recode the entire series of scripts to use Net::SSH::Perl instead of Net::Telnet.

So now, the powers that be have declared that Telnet shall no longer be an option on these unix servers, and have given us very little notice.

So, my plan is as follows: Install Putty tools on the server (windows, btw) and somehow initiate a shell to it, run plink to run putty from the command line to ssh to that unix server, then everything else remains the same.

However, I am not sure how to accomplish this. Basically, my code looks sort of like this:

my $device = Net::Telnet->new(Timeout => 10, Errmode => 'return'); $device->open(Host => &SSH_HELPIP, Port => &SSH_HELPPORT); if(!$device) { print "Unable to connect to SSH Helper\n" if ($var->{debug}); return undef; } my ($prematch, $match) = $device->waitfor('/:/'); #print "$prematch and $match<br>\n"; if ($prematch =~ /login/) { $device->print(&SSH_ID); } else { print "Timeout waiting for login prompt on SSH Helper - $prema +tch and $match\n" if ($var->{debug}); return undef; } my ($prematch, $match) = $device->waitfor('/:/'); #print "$prematch and $match<br>\n"; if ($prematch =~ /Password/) { $device->buffer_empty(); $device->print(&SSH_PW); } else { print "Timeout waiting for password prompt on SSH Helper\n" if + ($var->{debug}); return undef; } $device->waitfor('/\$/'); $device->print('ssh -l testid ' . $ip); my ($prematch, $match) = $device->waitfor('/\?|word:/') +; if ($prematch =~ /re you sure you want to continue conn +ecting/) { $device->print("yes"); my ($newpmatch, $newmatch) = $device->waitfor('/word +:/'); if ($newpmatch =~ /pass/) { $device->print(&PWD()); } } else { if ($prematch =~ /pass/) { $device->print(&PWD()); } } return $device;

So, I am basically establishing a device handle through telnet, and using its waitfor and cmd and print capabilities to parse commands and respons appropriately.

I will eventually have to re-write the whole system to use Net::SSH::Perl, but I need a quick solution that can allow me to SSH to my helper server, and then reuse my existing code to interact and parse.

Any ideas?

Thanks!


In reply to Making a shell work like a Net::Telnet session by pbwiz1970

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.