cancel
Showing results for 
Search instead for 
Did you mean: 

Telnet Script

SOLVED
Go to solution
Kenneth_18
Frequent Advisor

Telnet Script

Hello,

Can anyone show me a sample script that can automatically telnet into another server and execute another script within that server.

I need to create one and pattern it from your sample.

Thanks as I don't have any scripting skills whatsoever.

11 REPLIES
Stuart Browne
Honored Contributor
Solution

Re: Telnet Script

If it's just to execute one command, you might be better served setting up remote equivalency (either using ssh (as has been discussed in previous threads), or using rsh).

If it does need to be 'telnet', then it's easiest (or perhaps more stable) to use expect.

Most modern linux distributions would come with Expect.

A simple expect script could be something like:

---------------------------------------#!/usr/bin/expect

set timeout -1

spawn /usr/bin/telnet

expect "login:"
send "\r"
expect "password:"
send "\r"
expect "]$"
send "yourcommand\r"
expect "]$"
---------------------------------------

The man page for Expect can tell you more.

You can truely do some amazing stuff with expect.. A bit of TCl knowledge and you're laughing ;)
One long-haired git at your service...
Balaji N
Honored Contributor

Re: Telnet Script

install expect.

+++++++++++++++cut here+++++++++++++++++++++++
#!/usr/bin/expect -f
#
# This script telnets to a specific host as a specifc user and sets the required
# environment variables.

#Change default timeout
set timeout 100

#Variables. Change as necessary
set TERM xterm ;#default TERM
set prompt "(%|#|\\$) $" ;# default prompt
catch {set prompt $env(EXPECT_PROMPT)}

#Host Name
set SERVER "[lrange $argv 0 0]"
set USER "[lrange $argv 1 1]"
set PASSWD "[lrange $argv 2 2]"

#Command List
set COMMANDS "[lrange $argv 3 $argc]"

#Spawn Telnet
spawn telnet
expect "telnet> "

#Initialise a Connection
send "open $SERVER\r"

#Login to the Server
expect {
-re "Connection timed out" {
send_user "Unable to connect to $host. Exiting..."
exit 1
}
timeout {
send_user "Timed out connecting to $host. Exiting..."
exit 1
}
"login* "
}
send "$USER\r"
expect "Password:*"
send "$PASSWD\r"

expect {
-re "Login incorrect*" {
send_user "Looks like the password is wrong. Spawning a shell..."
interact
exit 1
}
-re $prompt
}


#loop through arguments
foreach command $COMMANDS {
send "$command\r"
expect -re $prompt
}

send_user "Expect Scripts Ends. Good Bye!!!\n"
+++++++++++++++cut here+++++++++++++++++++++++

call this script as

scriptname servername username password command[s]

and, if you want to have a basic script and then work out from there, check out autoexpect.
snipped below is from the man page of autoexpect.
++++++++++++++++++++++++++++++++++++++++++++++
AUTOEXPECT(1) AUTOEXPECT(1)

NAME
autoexpect - generate an Expect script from watching a session

SYNOPSIS
autoexpect [ args ] [ program args... ]

INTRODUCTION
autoexpect watches you interacting with another program and creates an Expect script that reproduces your interactions. For
straightline scripts, autoexpect saves substantial time over writing scripts by hand. Even if you are an Expect expert, you will
find it convenient to use autoexpect to automate the more mindless parts of interactions. It is much easier to cut/paste hunks of
autoexpect scripts together than to write them from scratch. And if you are a beginner, you may be able to get away with learning
nothing more about Expect than how to call autoexpect.

++++++++++++++++++++++++++++++++++++++++++++++


hth
-balaji
Its Always Important To Know, What People Think Of You. Then, Of Course, You Surprise Them By Giving More.
Stuart Browne
Honored Contributor

Re: Telnet Script

*chuckle* balaji's is a bit more thorough :P

and mine was missing (at the very least) a ' --' on the fisrt line..
One long-haired git at your service...
Balaji N
Honored Contributor

Re: Telnet Script

me copy pasted a snippet from one of my monitor scripts :-D
-balaji
Its Always Important To Know, What People Think Of You. Then, Of Course, You Surprise Them By Giving More.
Balaji N
Honored Contributor

Re: Telnet Script

pls zero point this.


to add to that Stuart, one thing i have mastered from Windows is the art of Ctrl+C and Ctrl+V
-balaji
Its Always Important To Know, What People Think Of You. Then, Of Course, You Surprise Them By Giving More.
Stuart Browne
Honored Contributor

Re: Telnet Script

:) I still use ctrl-ins (copy)/shift-ins (paste). Old-school Windows hang-over :)

The expect routine we use for stuff like that is a little more complex.. about 5kb back-ended with a PHP script (7kb) and an Interbase database (1.2mb) with all the hosts and authentication details of our various client sites.. Much fun ;)

The above was vaguely copied from the quick-hack routine :P

0pts here too pls.
One long-haired git at your service...
John Meissner
Esteemed Contributor

Re: Telnet Script

I use expect to automate my telnet sesssions...

I'm attaching a script

all you need to do is change your username and your password and the servername
All paths lead to destiny
Caesar_3
Esteemed Contributor

Re: Telnet Script

Hello!

After install of expect
use this script

Caesar
Yan LI_4
Occasional Visitor

Re: Telnet Script

I was trying to use the script
expect {
timeout { exit 1 }
"login*"
}

keep getting error
invalid command name "
timeout {
exit 1
}
"login: "
"

Another question is about match_max, set timeout; when there are a large amount of data coming from the remote server which size is larger than match_max and the time is longer than the timeout, the script exit at timeout interval which is unexpected.

Could anybody help on this?

Many Thanks!
John Meissner
Esteemed Contributor

Re: Telnet Script

I use expect to telnet out to other servers (linux and HP-UX) it is fantastic and can be found here: http://expect.nist.gov/

it's SOOOOO easy to use.
All paths lead to destiny
John Meissner
Esteemed Contributor

Re: Telnet Script

sorry for the double post (the one above)
no points for that one please

one nice thing about expect is that it has a build it script writter called autoexpect

if you are having errors you can use autoexpect to automatically generate a script for you.

just type:
autoexpect -f filename telnet servername

and proceed to log in
once you are done hit Ctrl D (or Ctrl C - i can't remember which) and expect will write a script of your session to "filename" you specified in your command line. edit that file and remove some of the unnecessary lines and you'll be all set
All paths lead to destiny