bigrayhicks has asked for the wisdom of the Perl Monks concerning the following question:

Hello.. I am new to this board and it has been some time since i have worked writing PERL. I have been asked to take a look at the attached script and see if I can get it changed from using TELNET to using SSH. I wonder if it is as simple as changing the module from NET::TELNET to NET::SSH. Any advice?

#!/usr/bin/perl use Net::Telnet; use strict; #********************** User defined variables *********************** +************** my $username='adminuser'; my $password='xxxxxxxxx'; my $machine='192.168.102.21'; #********************************************************************* +************** #Open telnet session to host my $telnet = new Net::Telnet ( Timeout=>30, Errmode=>'die', Input_log +=> 'logs\\telnet-remove-exclusions-log-WLC02.txt'); $telnet->open($machine); $telnet->waitfor('/User: $/i'); $telnet->print($username); $telnet->waitfor('/Password:$/i'); $telnet->print($password); $telnet->waitfor('/\(Cisco Controller\) >$/i'); #check for error status which should be undefined my $msg = $telnet->errmsg; print "$msg\n"; #loop while the command does not timeout looking for a different promp +t while (not $msg) { my @output = $telnet->cmd(String => 'show exclusionlist', Prompt => +'/Would you like to display the next 15 entries?/', errmode => 'retur +n'); $msg = $telnet->errmsg; $telnet->print('N'); print @output; foreach my $mac (@output) { if ($mac =~ m/([a-fA-F0-9]{2})\:([a-fA-F0-9]{2})\:([a-fA-F0-9]{2})\ +:([a-fA-F0-9]{2})\:([a-fA-F0-9]{2})\:([a-fA-F0-9]{2})/) { my $cmd = "config exclusionlist delete " . $mac; $telnet->cmd(String => $cmd, Prompt => '/\(Cisco Controller\) >$ +/i'); } } } #once command times out looking for the continue prompt, run it one mo +re time with the regular prompt my @output = $telnet->cmd(String => 'show exclusionlist', Prompt => '/ +\(Cisco Controller\) >$/i', errmode => 'return'); print @output; foreach my $mac (@output) { if ($mac =~ m/([a-fA-F0-9]{2})\:([a-fA-F0-9]{2})\:([a-fA-F0-9]{2})\ +:([a-fA-F0-9]{2})\:([a-fA-F0-9]{2})\:([a-fA-F0-9]{2})/) { my $cmd = "config exclusionlist delete " . $mac; $telnet->cmd(String => $cmd, Prompt => '/\(Cisco Controller\) >$ +/i'); } } $telnet->print('logout');
Any help on this is greatly appreciated!

Replies are listed 'Best First'.
Re: Help migrating script from TELNET to SSH
by zentara (Cardinal) on Sep 21, 2011 at 18:03 UTC

        Yeah, I found subclassing Expect to be the easiest way to go (this was several years ago when SSH first started being supported across the board on our switches and routers). Too many of the dumber devices have cranky SSH support when it comes to keys and using multiple channels.

        Read the source of Net::Telnet for hints and a good starting regexp to match the various prompts. Then subclass Expect and add login(), enable(), cmd() methods.

        The reasons not to use a full-blown CPAN module are that once you get past real SSH hosts and into routers and then switches and access points the capabilities change so much that the modules don't work across them all. And when you submit bugs to vendors it goes much smoother if you can tell them that you're just using openssh.

        Just start with Expect, enable the logging and spawn a ssh and work through the login() and cmd() steps using just the expect, send methods. Then factor that code out into a subclass of Expect.

        You might want to look at Net::CLI::Interact which I just noticed a few weeks ago, it looks decently promising.

Re: Help migrating script from TELNET to SSH
by MidLifeXis (Monsignor) on Sep 21, 2011 at 16:51 UTC

    What have you tried? Did you look for any CPAN modules? If you want me to do your work, that can be negotiated for $$.

    This is not a code writing service. You will get out of PerlMonks what you put into PerlMonks. Post what you have tried, what didn't work (and how).

    As for advice - look at the ssh modules on CPAN. Compare the APIs between them and Net::Telnet. Try things.

    --MidLifeXis

Re: Help migrating script from TELNET to SSH
by salva (Canon) on Sep 23, 2011 at 08:47 UTC