Simple Telnet Automation Using Expect – This is a nice simple bash shell script to automate and test telnet servers, it’s a nice script and well commented. Hope you enjoy, all comments welcome.

Simple Telnet Automation Using Expect – This is a nice simple bash shell script to automate and test telnet servers, it’s a nice script and well commented. Hope you enjoy, all comments welcome..

 

Explanation of Code (Bash Shell)

The code works by the user entering an argument, this argument could be either
-a, -h, or a server nickname. The -a argument would cause the bash shell to
log in and out of every server located in the autotelrc file, using the usernames
and passwords provided. The -h argument would bring up a small help message.
The code written is heavily commented but some parts deserve some explanation
as to their rationale.

 if [ “z$1” == “z” ]; then
echo “You didn’t type a valid argument.”

The z in front of the $1 argument is to tell if the user entered an argument
or not. If something isn’t entered it returns just the z by itself and the error
echo occurs.
If this condition is not true then the script then proceeds to the else statement.
Under this statement is another if statement. This takes account for the -a,
-h and server nickname argument. The counter I has been initialised at 2, this
saves us a line of code, if I = 2 then we would have to use another cut to trim
off the hashes in the rc file. So just incrementing the counter by 1 helps us
here. If the -h argument is used then the if statement give echoes us the help
message, which explains the usage to the user. If the -a argument is used, then
the statement then proceeds to work it’s way through the rc file and telnet
into each using the expect script (explained later) and then log out, this is
useful to a network administrator to test various telnet machines. If the user
specifies an argument with a server nickname then the script searches through
the rc file for the relevant line and telnets into it using the expect script.
The code used here was as follows.

 If [ $1 == $Nickname ]; then
./telExpect.exp $Machine $Username $Password
Fi #finish if statement

This was chosen instead of using the getops because it is much shorter code,
and in searching a small file there is not much performance difference.

Leave a Reply