pbwiz1970 has asked for the wisdom of the Perl Monks concerning the following question:
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!
|
|---|