Expect in UNIX


About Expect:
Expect, an extension to the Tcl scripting language written by Don Libes, is a program to automate interactions with programs that expose a text terminal interface. Automate control of interactive applications such as telnet, ftp, passwd, fsck, rlogin, tip, ssh, and others. Expect uses pseudo terminals (Unix). More details  wiki/Expect 



Expect three command’s required for automate any script:
spawn – to start the command
send – to send the strings to the process
expect – wait for the specific string from the process

How to Install expect?
Installation details refer from expect.sourceforge.net

Note: Expect & TCL installation required in source.

How to execute the expect command?
Logging into Unix server and type the below command's:

Which version?
[vinod@tutorialbyexample.com ~]$ expect -version
expect version 5.44.1.15

How to verify location of expect installation?
[vinod@tutorialbyexample.com ~]$ which expect
/usr/bin/expect

Type the expect command and enter:
[vinod@tutorialbyexample.com ~]$ expect
expect1.1> ls
it will display list of all file or dir located under this dir

Case -1 
Now let's discussed more about ssh by expect with real scenario, we have three system, System-1, System-2 and System-3. System-1 have expect script and it will logging into System-2 and System-3 and perform df -h Unix command and it will check disk thresh hold, if more than 95% then it will send email to configure email id's.


















Assumptions:
1. ssh installed and configure in both remote system.
2. expect installed in source system, which is going to connect remote system.
3. User configure in all three System.

Logging into Unix system-1:
e.g:root/anyuser with password

create the scriptfordfoverssh.sh file with help of:
vi scriptfordfoverssh.sh then enter
A windows will appear with edit option:
pest the below content in that editopr:

# Environment Variables
SCRIPT_HOME_DIR=/home/root
SCRIPT_SRC="${SCRIPT_HOME_DIR}"/script_for_ssh_df
SCRIPT_FLE="${SCRIPT_SRC}"/script_by_expect_sshdf.sh
MAX_VAL=95
  
#Checking the src dir if exist, if not then it will create
if [ ! -d "${SCRIPT_SRC}" ]; then
        mkdir ${SCRIPT_SRC}
fi

#This is for looping the all hosts configure in this

#system-2
SSH_HOSTS[0]="user1@system-2"
SSH_PWDS[0]="password1"

#System-3
SSH_HOSTS[1]="user2@system-3"
SSH_PWDS[1]="password2"
  
for i in 0 1
do
        echo "${SSH_HOSTS[i]}"
        #This section is only on for executing the df command with logging by ssh
        echo "#!/usr/bin/expect" > $SCRIPT_FLE
        echo "spawn /usr/bin/ssh \"${SSH_HOSTS[i]}\"" >> $SCRIPT_FLE
        echo "expect \"password:\"" >> $SCRIPT_FLE
        echo "send \"${SSH_PWDS[i]}\r\"" >> $SCRIPT_FLE
        echo "expect \"$ \"" >> $SCRIPT_FLE
        echo "send \"df -h\r\"" >> $SCRIPT_FLE
        echo "send \"exit\r\"" >> $SCRIPT_FLE
        echo "expect \"quit\r\"" >> $SCRIPT_FLE
        sleep 2
        chmod 755 $SCRIPT_FLE

        $SCRIPT_FLE > ${SCRIPT_SRC}/sshdf_log_script_executions.txt

        cd ${SCRIPT_SRC}
        grep % sshdf_log_script_executions.txt | sed 's/%//g' 
             | awk '$5 > ${MAX_VAL} {print $1,$2,$3,$4,$5"%";}' 
             | column -t | grep / > ${SCRIPT_SRC}/sshdf_log_script_executions.txt

        if grep % sshdf_log_script_executions.txt | sed 's/%//g' 
           | awk '$5 > ${MAX_VAL} {print $1,$2,$3,$4,$5"%";}' | column -t 
           | grep /  >/dev/null; then
                cat  ${SCRIPT_SRC}/sshdf_log_script_executions.txt 
                    | mailx -s "Disk utilization are reaching more than the 
                    ${MAX_VAL}% in ${SSH_HOSTS[i]}" vinod@tutorialbyexample.com
        fi
done

chmod 775 scriptfordfoverssh.sh

Configure the cronttab -e option and set:
0*/4 * * * /home/youruserid/scriptfordfoverssh.sh

With this configuration, script will run Cron Job in Unix and it will check if disk space occupied more then 95% then email will send to vinod@tutorialbyexample.com.

References:
https://en.wikipedia.org/wiki/Expect
http://expect.sourceforge.net/


5 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Nice blog post.thanks for share the information.

    ReplyDelete
  3. this blog has given a good information thanks for sharing it

    ReplyDelete
  4. Nice Post. I read your blog very useful to me. Thanks for sharing with me. software

    ReplyDelete

Please provide your input, it will honer for me...