Operating System - HP-UX
1754020 Members
7209 Online
108811 Solutions
New Discussion

Re: How to write script for telnet session

 
R. Allan Hicks
Trusted Contributor

Re: How to write script for telnet session

I've written some telnet scripts in PERL.

PERL has a telnet module. With it, I can login to my routers, enter commands to the routers to gather statistics. The script looks like this

#!/usr/contrib/bin/perl -w -I/opt/perl/lib/5.6.1/Net
#
use Telnet;
$|=1;

$Vanguard=shift(@ARGV);
$Password=shift(@ARGV);
$theVanguard=Net::Telnet->new(Timeout=>10,Prompt=>'/OK\n/',Host=>$Vanguard)
or die("Cannot Connect $Vanguard");

print "Hostname : $Vanguard\n";

#################
# Wait for OK - then send ATDS to connect
#################
$theVanguard->prompt("/Enter Password:/");
@listing=$theVanguard->cmd("atds");
print "@listing";

###
# Enter password and wait for enter selection prompt
###

$theVanguard->prompt('/Enter Selection:/');
@listing=$theVanguard->cmd($Password) or die ("Wrong Password $Vanguard\n");
print "@listing";

###
# Enter Commands to get statistics
###

$theVanguard->prompt('/Enter Selection:/');
@listing=$theVanguard->cmd("5");
print "@listing";


The $theVanguard=Net::Telnet->new(Timeout=>10,Prompt=>'/OK\n/',Host=>$Vanguard)
opens the connection to the host($Vanguard which is the first argument passed to the script) and waits for the host to respond with "OK" for up to 10 seconds. If it fails then the die clause is executed.

It sends the password to the host (which is the second argument passed to the script) and waits for up to 10 seconds for the host to respond with "Enter Selection"

When the enter selection is sent, it starts sending commands to the host. The @listing array contains the host's responses to the commands that are printed to stdout. I generally redirect standard out to a file for later review.

PERL Black Book ISBN: 1-57610-465-6 Publiched by CoriolisOpen Press Written by Steven Holzer is an excellent resource and contains the documentation on using the telnet module.

-Good Luck
"Only he who attempts the absurd is capable of achieving the impossible
rmueller58
Valued Contributor

Re: How to write script for telnet session

I've used both Expect, Perl and KSH and found expect 100 times less cryptic to write these type of things then Perl..

We are using it for in house OLTP between multiple boxes, inserting records into Informix, Postgres or Mysql databases..

In addition to automated file transfers..

I general use perl for in file edits, using perl -pi -e g///s file and that's about it.