Thursday, May 19, 2011

Script with EXPECT

Script to verify few processes on Remote Server via automated password less login without using SSH RSA/DSA Certificate. Expect is the catch:

################HOST1######################

#!/usr/bin/expect -f
#!/bin/bash
# set Variables
set host1 "HOST1"
set login "testuser"
set PASSWORD "testpass"
# now connect to remote UNIX box (ipaddr) with given script to execute
spawn ssh $login@$host1 "ps -aef|grep -i java"
# Look for passwod prompt
expect "*?assword:*"
# Send PASSWORD aka $PASSWORD
send -- "$PASSWORD\r"
# send blank line (\r) to make sure we get back to ui
send -- "\r"
expect eof

################HOST1######################
################HOST2######################
#!/usr/bin/expect -f
#!/bin/bash
# set Variables
set host1 "HOST2"
set login "testuser"
set PASSWORD "testpass"
# now connect to remote UNIX box (ipaddr) with given script to execute
spawn ssh $login@$host1 "ps -aef|grep -i java"
# Look for passwod prompt
expect "*?assword:*"
# Send PASSWORD aka $PASSWORD
send -- "$PASSWORD\r"
# send blank line (\r) to make sure we get back to gui
send -- "\r"
expect eof
################HOST2######################
################Consolidated#################
#!/bin/bash
########### Script by R A J E S H D O G R A for #########################
##### Setting variables null in case re-run of script ####################
set VAR1 = null;
set VAR2 = null;
set VAR3 = null;
set VAR4 = null;
set VAR5 = null;
############## Collect Data From Remote Servers ############################
/bin/echo "Checking HOST1 processes"
/bin/echo "*****************************************"
/home/monitor/host1 |grep -v testuser|awk '{print $1}'|uniq|sort|tee /home/monitor/temp1
/bin/echo "*****************************************"
sleep 1
/bin/echo "Now Checking CENTIME for processes"
/bin/echo "*****************************************"
/home/monitor/hl-centime |grep -v sscope|awk '{print $1}'|uniq|sort|tee /home/monitor/temp2
/bin/echo "*****************************************"
############ Process the data to find if services are OK ###################
export VAR1=`sed -n '1p' /home/monitor/temp1`;
export VAR2=`sed -n '2p' /home/monitor/temp1`;
export VAR3=`sed -n '1p' /home/monitor/temp2`;
export VAR4=`sed -n '2p' /home/monitor/temp2`;
export VAR5=`sed -n '3p' /home/monitor/temp2`;

#echo $VAR1 $VAR2 $VAR3 $VAR4 $VAR5;

if [ "$VAR1" == "proc1" -a "$VAR2" == "proc2" -a "$VAR3" == "proc3" -a "$VAR4" == "proc4" -a "$VAR5" == "proc5" ];
then
echo "Chill Buddy, its just a regular alert, no need to panic !!"
sleep 2
echo "*********************************************************************"
sleep 1
echo "*********************************************************************"
else
echo "Server is screwed up buddy, reload the processes"
fi
############### Kill the session ###########################################
/bin/rm -f /home/monitor/temp1;
/bin/rm -f /home/monitor/temp2;

/bin/echo "Thats it ! You can close the window"
sleep 2
/bin/echo "******** Auto Logout in 20 seconds********"
sleep 20
kill -HUP `pgrep -s 0 -o`
################Consolidated#################

No comments:

Post a Comment