Operating System - HP-UX
1835204 Members
2904 Online
110077 Solutions
New Discussion

Re: Automating a Telnet Session

 
Mark Sellan
Advisor

Automating a Telnet Session

I've been asked to write a script to periodically logs into our router and captures the NAT table info.

Here's an excerpt of what I'm having problems with:

(echo "password";echo "show ip nat trans";sleep 30) | telnet $IP_ADDRESS > $DIR/info.$TAG

Basically I thought this would echo the router password (yeah I know it's clear text) and router command piping them through telnet and redirect the output to a file.

What happens though is that I seem to get to the router (it responds with the expected prompt, but it's as if I'm not really logged in. I cannot issue any commands to it.

Even if I just try on the command line to pipe the password to the telnet session it appears to log in but I cannot type any commands.

Is there something I'm doing wrong in trying to initiate this telnet session?

I thought about Expect but was hoping to avoid the addition of another piece of software. Are there other ways to automate telnet sessions?

Thanks much,

-mark

9 REPLIES 9
Bernie Vande Griend
Respected Contributor

Re: Automating a Telnet Session

I would highly recommend expect for this. This is exactly the type of thing that expect is good at.
Ye who thinks he has a lot to say, probably shouldn't.
James R. Ferguson
Acclaimed Contributor

Re: Automating a Telnet Session

Hi Mark:

Try 'remsh' or 'rexec', as for instance:

# remsh thehost -l root -n "(date;hostname;pwd) > /tmp/result"

...which would place the output of the commands into /tmp/result on 'thehost', and then:

# remsh thehost -l root -n "cat /tmp/result"

Regards!

...JRF...
Regards!

...JRF...
Herve BRANGIER
Respected Contributor

Re: Automating a Telnet Session

Hi

I think it's better to use expect, but you can
try with telnet. Don't forget to add a sleep
between each stage of connection :

#!/bin/sh
# Wait login prompt
sleep 5
echo $LOGIN
# Wait passwd prompt
sleep 5
echo $PASSWD
# Wait connection is established
echo ls
# Here your commands are executed (don't use sleep)
# Don't forget to disconnect from remote
echo exit
# At the end don't forget to quit your script
# after executions
sleep 30
exit


You need to try to find your own sleeping times.

HTH

Herv?

Sridhar Bhaskarla
Honored Contributor

Re: Automating a Telnet Session

Mark,

As pointed by Herves, sleep is very important.
Try like this

(sleep 5
echo password
sleep 5
echo show ip nat trans
sleep 15
echo exit) |telnet $IP_Address

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Mark Sellan
Advisor

Re: Automating a Telnet Session

Thanks so much everyone for your suggestions...unfortunately, none seem to work with the Cisco router I have to communicate with.

I guess I am going to have to install Expect. Can I get this through the HP-UX Shareware site? Are any of you using it? I'd like to get any feedback about it you could share.

Thanks again,

-mark
Sridhar Bhaskarla
Honored Contributor

Re: Automating a Telnet Session

Mark,

You can get expect from this following site free.

http://hpux.ee.ualberta.ca/hppd/hpux/Tcl/expect-5.31/

You can use this sample script to get what you want.

#!/wherever/expect -f

set prompt "(%|#|\\$|>)"

set timeout 10

spawn telnet $IP_ADDRESS
expect "login: "
send "password\r"
expect "$prompt"
send "show ip nat trans "
expect "$prompt"
send "exit\r"

You can simplify the script by replacing $prompt with the prompt of your router.

The above is only a template. May not work.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Bernie Vande Griend
Respected Contributor

Re: Automating a Telnet Session

Note, you will also need depots of tk and tcl before you install the expect depot. Both are also available from the site Sridhar gave you:

http://hpux.ee.ualberta.ca/hppd/hpux/Tcl/tcl-8.2.1/

http://hpux.ee.ualberta.ca/hppd/hpux/Tcl/tk-8.2.1/

install these and then the expect depot and you'll be all set to write a script. You should be able to find some examples at

http://www.scriptics.com/scripting/

Ye who thinks he has a lot to say, probably shouldn't.

Re: Automating a Telnet Session

Hi Mark,
If I understand correctly, you want to do the Batch Telnet I mean u want to execute some script or commands on other systems thru telnet.
If you want to execute the just the script on the other system then the best way to make the Trust Relationship between the two systems and then use the remsh command.
If you don't want to go with this and simply want Batch Telnet then follow the steps:
1. You should have Perl 5.6.1 or later version install on your machine; not necessary on the other end.
2. Just copy this under the $PERL_HOME/site/lib/Net directory(Attached)
you can find more about this at http://search.cpan.org/doc/JROGERS/Net-Telnet-3.02/lib/Net/Telnet.pm

3. Write this simple perl script

use Net::Telnet ();
$t = new Net::Telnet (Timeout => 90,
Errmode=>'die');
$t->open('your hostname');
$t->waitfor('/login: $/i');
$t->print('your_username');
$t->waitfor('/password: $/i');
$t->print('your_password');
$t->waitfor('/\$ $/i');
$t->print('script_to_run');
@lines = $t->waitfor('/\$ $/i');
print @lines;


Now just replace the above with your info.
your_hostname
your_username
your_password
script_to_run

This will solve your problem.

Thanks
Zafar
Win/Win
Sean Venter
New Member

Re: Automating a Telnet Session

Hi

The script below shows how to send some initial commands to a router (like password) and then enter your own commands.

router=
( echo
echo
echo
while read cmd
echo $cmd
done
) | telnet $router | tee

You may want to sleep for a few seconds between commands but I have not needed to.

Of course to have no input from you required, echo exit inside the brackets and remove the while loop. The output can then be redirected to a file rather than tee'd.

From my understanding, the reason your original script didn't work is that you were typing in was not being picked up by the script and passed on to the telnet session.

It does not work trying to script a session to another unix host. Any ideas on why?

Sean