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.
Code Explained (Expect Script's)
There are two expect scripts, one to telnet in and out of every server and
the other to telnet in and give control to the user.
Firstly we set the timeout in the script to 20 seconds, this means that if everything
else goes wrong the program will exit after 20 seconds. We then set the variables
used in the script, which would be passed in as arguments from the bash script.
These arguments are the name of the server the user (username) and the password
of the server. What the script then does is spawn the telnet program then connect
to the server which is the name variable and then wait or expect the response
"login:" it would then send the user (username) variable. It then
waits for the "Password:" prompt and then sends the password variable.
Once this has occurred, the expect script then uses the interact feature, which
hands keyboard control over to the user.
The second script is identical to the first but it sends "exit" once
the machine has accepted the login, this is to test each computer.
Note Remember to create the autotelrc file.
The Code (Bash Script)
#!/bin/sh #Where the script is run from
Chmod 600 #only owner can read/write
if [ "z$1" == "z" ]; then #if the argument is null
echo "You didn't type a valid argument." #echo an error msg
else #otherwise perform the next set of if statements
if [ $1 == "-h" ]; then #if the argument is -h
echo " Usage ./autotel [-a] [-h] Nickname" #echo the usage msg.
echo "-a - Attempts to log on to each telnet server in file autotelrc and
then exits."
echo "-h - Displays this help message."
echo "nickname - Telnets server using the username and password provided
in the telnetrc file"
elif [ $1 == "-a" ]; then #if the argument is -a
n=`wc -l autotelrc | cut -b 0-7` #counts the number of lines in autotelrc
i=2 #Makes the counter start at 2, this stops it from reading the first comment
line
while [ $i -le $n ] #while we still have lines to go through in the file
do #do the following
line=`head -$i autotelrc | tail -1` #The line used in the following loop
Machine=`echo $line | cut -d' ' -f1` #initialising the Machine variable
Username=`echo $line | cut -d' ' -f2` #initialising the Machine variable
Password=`echo $line | cut -d' ' -f3` #initialising the Machine variable
./telExpect.exp $Machine $Username $Password #run the expect script with the
arguments provided
sleep 21 #sleep for 21 seconds to allow script to logout or timeout.
i=`expr $i + 1` #increment counter
done #end while loop
else #wildcard else
n=`wc -l autotelrc | cut -b 0-7` #counts the number of lines in autotelrc
i=2 #Makes the counter start at 2, this stops it from reading the first comment
line
while [ $i -le $n ] #while we still have lines to go through in the file
do #do the following
line=`head -$i autotelrc | tail -1` #The line used in the following loop
Machine=`echo $line | cut -d' ' -f 1` #initialising the Machine variable
Username=`echo $line | cut -d' ' -f 2` #initialising the Machine variable
Password=`echo $line | cut -d' ' -f 3` #initialising the Machine variable
Nickname=`echo $line | cut -d' ' -f 4` #initialising the Machine variable
if [ $1 == $Nickname ] #if the argument matched the nickname
then #then do the following
./telExpect1.exp $Machine $Username $Password #run second expect script
fi #finish if statement
i=`expr $i + 1` #increment counter
done #end while loop
fi #finish if statement
fi #finish if statement
exit 0 #exit
Expect Script 1
#!/usr/bin/expect #Where the script should be run from.
Chmod 600 #only owner can read/write
set timeout 20 #If it all goes pear shaped the script will timeout after 20
seconds.
set Machine [lindex $argv 0] #First argument is assigned to the variable name
set Username [lindex $argv 1] #Second argument is assigned to the variable user
set Password [lindex $argv 2] #Third argument is assigned to the variable password
spawn telnet $Machine #This spawns the telnet program and connects it to the
variable name
expect "login:" #The script expects login
send "$Username " #The script sends the user variable
expect "Password:" #The script expects Password
send "$Password " #The script sends the password variable
interact #This hands control of the keyboard over two you
Expect Script 2
#!/usr/bin/expect #Where the script should be run from.
set timeout 20 #If it all goes pear shaped the script will timeout after 20
seconds.
set Machine [lindex $argv 0] #First argument is assigned to the variable name
set Username [lindex $argv 1] #Second argument is assigned to the variable user
set Password [lindex $argv 2] #Third argument is assigned to the variable password
spawn telnet $name #This spawns the telnet program and connects it to the variable
name
expect "login:" #The script expects login
send "$Username" #The script sends the user variable
expect
"Password:" #The script expects Password
send "$Password" #The script sends the password variable
send "quit" #log out of server.
This article was originally written by Sliptop |