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

How to verify location of expect installation?
[vinod@tutorialbyexample.com ~]$ which 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.

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
#Checking the src dir if exist, if not then it will create
if [ ! -d "${SCRIPT_SRC}" ]; then
        mkdir ${SCRIPT_SRC}

#This is for looping the all hosts configure in this


for i in 0 1
        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

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.



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