Shell script to telnet and run commands [Solved/Closed]

- Nov 12, 2008 at 04:32 AM - Latest reply:  kar416 - Mar 17, 2010 at 02:19 PM
Hello,
i have made a shell script which can telnet automatically and run commands on the other machine and end the telnet session without any interact from the user,only he just run the script or call it from a nother script.
but you need to install EXPECT and TCL packages on your unix system.
the script contains the following instructons:


#!/usr/local/bin/expect -f ####/usr/local/bin/expect is the directory where expect was installed
log_user 0 ####this command is used to hide the conversationn between the script and the other machine
set address [lindex $argv 0] ###assign the first passed parameter while calling the script to $address
set username [lindex $argv 1] ###assign the 2nd passed parameter while calling the script to $username
set password [lindex $argv 2] ###assign the 3rd passed parameter while calling the script to $password
spawn telnet ${address} ###start the telnet session to machine with IP=$address
###start conversation with the machine:
expect "login:"
send -- "${username}\r"
expect "Password:"
send -- "${password}\r"
expect "$ "
send -- "#!/bin/ksh\r" ###declare the shell to be used (optional)
expect "$ "
send -- "###type her any command you want to execute"
expect "$ "
send -- "exit\r" ###end the telnet session and exit the script


to execute this script, I typed:
$ expect_script "address" "username" "password" ###the script name is "expect_script"
but before u call the expect script, you should make the expect script executable, and this can be done by typing:
$ chmod +x expect_script
See more 

3 replies

Best answer
Posts
8
Registration date
Saturday October 4, 2008
Last seen
April 27, 2009
Nov 12, 2008 at 05:58 AM
29
Thank you
hey buddy this is a great thing that you have posted here. thks for the help and hope that will here very somme with some useful tips like that.

Thank you, netgear5 29

Something to say? Add comment

CCM has helped 1699 users this month

#!/usr/local/bin/expect
spawn telnet <machine ip>
expect "login:"
send "<username>\n"
expect "Password:"
send "<password>\n"
send "bash\n"
send "cd /opt\n"
send "ls -ltr\n"  (if you are not giving \n then it will wait for your response or u have to type enter manually).
interact

How to execute the “expect“ command expect –f <file name>
Ex: expect –f <filename>.expect
7
Thank you
A little delayed; however, there are still uses for telnet. For example, I want to have scripts like this run commands for MythTV spawned by the remote machine via Simon. For example.

I speak "Computer, Next". Simon recognizes the phonetics and fires it to the server. The server picks it up and executes the command. Said command then spawns a script, which in turn logs in to the machine I'm talking to and runs the Telnet command to advance the media I'm watching on MythTV.

Myth, to the best of my knowledge does not have the infrastructure to do things with SSH as the commands are assumed to be a local.

By doing it this way, all traffic is internal, and more importantly, the clear telnet session is not left open.

Just my two cents
1
Thank you
Argh!!! Telnet? No!
Telnet sends everything in clear text. Anyone watching your packets would have your password.
You should use SSH instead. It's secure and encrypted.
Not to mention that if you set up ssh key pairs, you can have passwordless logins for your scripts.