Operating System - HP-UX
1828663 Members
1400 Online
109984 Solutions
New Discussion

How to write script for telnet session

 
Edward Tse
Occasional Contributor

How to write script for telnet session

Dear all,

I want to write a script ( .sh ) for telnet session and auto login to the system. How to do it ? Please help
11 REPLIES 11
KapilRaj
Honored Contributor

Re: How to write script for telnet session

search in this forum buddy ... this has been already discussed here

Kaps
Nothing is impossible
Sanjay Kumar Suri
Honored Contributor

Re: How to write script for telnet session

Check this thread:

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=496072

sks
A rigid mind is very sure, but often wrong. A flexible mind is generally unsure, but often right.
Manish Srivastava
Trusted Contributor

Re: How to write script for telnet session

Fabio Ettore
Honored Contributor

Re: How to write script for telnet session

Hi,

check this thread:

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=496072

Anyway this was posted more times in ITRC forum, try to insert keywords 'telnet script' in ITRC search.

Best regards,
Ettore
WISH? IMPROVEMENT!
Edward Tse
Occasional Contributor

Re: How to write script for telnet session

It is right?

Script:

Telnet ip-address
sleep 5
echo username
sleep 5
echo password

I try this, but it isn't work. Please help
Nicolas Dumeige
Esteemed Contributor

Re: How to write script for telnet session

There a way to do it this way, check http://steve-parker.org/sh/expect.shtml

But, you'll find that Expect is really what you need, if rsh (or ssh) won't be satisfactory (why ??).

Ant (what make is for C, ant is for Java) has also telnet scripting capabilities).

Cheers

Nicolas
All different, all Unix
Rodney Hills
Honored Contributor

Re: How to write script for telnet session

If you are familiar with "perl", you can drive a telnet session with "Net::Telnet" module. Example-

use Net::Telnet ();
$t = new Net::Telnet (Timeout => 10,
Prompt => '/bash\$ $/');
$t->open("sparky");
$t->login($username, $passwd);
@lines = $t->cmd("/usr/bin/who");
print @lines;

HTH

-- Rod Hills
There be dragons...
rmueller58
Valued Contributor

Re: How to write script for telnet session

Install EXPECT

Download HERE:
http://hpux.cs.utah.edu/hppd/hpux/Tcl/expect-5.41/


#!/usr/bin/expect
spawn telnet
expect "login"
send "USERNAME\r"
expect "Password:"
send "PASSWORD\r"
expect "# "

YOU CAN SEND and Expect commands..

Get the OReilly Book "Expect"

rmueller58
Valued Contributor

Re: How to write script for telnet session

Here is the Expect Main Site
http://expect.nist.gov/

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.