Shell script for telnet and run commands

October 2016

A shell script has been designed which has the features of automatic telnet and executes run commands for other machines, thereby ending the session of telnet without any kind of interaction from the user. The user just needs to run the shell script or call the function from another script. This is widely applicable for Linux, but for the UNIX system one needs prior installation of TCL and INSTALL packages. There is a separate directory for installing the Expect command. Specific commands are used for hiding the conversation between the other machine and the shell script in Linux. Address, username and password are assigned while calling the script. The IP address is required for declaration of the shell, after which the run command to be executed is sent marking the end of the <a href='/s/telnet+'>telnet session.


I have made a shell script which can telnet automatically and run commands on the other machine and end the telnet session without any interaction from the user; the user just runs the script or calls it from another script.
But you need to install EXPECT and TCL packages on your Unix system.

The script contains the following instructions:

#!/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 conversation 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 you call the expect script, you should make the expect script executable, and this can be done by typing:
$ chmod +x expect_script


Try this:
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 you have to type enter manually).   

How to execute the "expect" command expect -f <file name>
Ex: expect -f <filename>.expect


Thanks to ZooZoo for this tip on the forum.

Related :

This document entitled « Shell script for telnet and run commands » from CCM ( is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.