AIX25
asked on
How do I set up a shell script using ifconfig?
I have a remote AIX server running on a generator. Many times the generator goes out and I only have a window of 20 mins with the network up and 45 mins the server is powered. I need help creating a script where it goes out and checks the network every 10 mins and checks if the network is up. If the network is up thats fine. If the script detects the network is down, then I want the script to check the network in 5 mins to make sure the network has come back up or not and then repeat the same step; check it again in 5 mins. If the network has been down for 20 mins, then I want the script to shutdown the server before the generator runs out of power. I need this server to always perform a clean shutdown and not to abrubtly shutoff because the power goes out.
ASKER
Our system is high security and we use a seperate task schedule app. We do not use cron and have disabled it. I am running AIX 5.3. I need help with creating a basic script to perform what I need.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
do I just enter /tmp/count? Or do I put a number in place of count? Also, where do I enter in the shutdown command after the script finds that the network is down? Also, I do not know the gateway address, would I be able to just put my IP address that is on the AIX server?
Thanks in advance
Thanks in advance
The 1st question "do I just enter /tmp/count? Or do I put a number in place of count?".
if your system do not have init command then you put the shutdown command in that line
you may find the default gateway ip by running
netstat -rn
OR
you may ping the ip address of any system you know on the same lan or on wan
if your system do not have init command then you put the shutdown command in that line
you may find the default gateway ip by running
netstat -rn
OR
you may ping the ip address of any system you know on the same lan or on wan
ASKER
All the information that you gave me has been VERY helpful. Here is what I do to check if the network is up: I execute # ifconfig en0 monitor , then #ifconfig en0 and gives me an output of en0: flags=5e080863,c0<UP,BROAD CAST. So I have ifconfig monitor on. To test this I unplug the server from the network and then execute #ifconfig en0 again of course while the server is not plugged in to the network and I get an output of en0: flags=5e080863,c0<,BROADCA ST. The thing that is missing from this output is 'UP' which indicates the network is down. In summary, how do I apply this to a script to catch when the 'UP' is there or not? And incorporate checking in 10min increments. Then when the 'UP' is missing for 20mins to shutdown the server.
Please help me...Thank you
Please help me...Thank you
then change this line
/path/to/ping ip_address
by
/path/to/ifconfig en0 | /usr/bin/grep UP
/path/to/ping ip_address
by
/path/to/ifconfig en0 | /usr/bin/grep UP
ASKER
Please check this for me that I have done it correctly:
while true
do
/usr/sbin/ifconfig en0 | /usr/bin/grep UP
if [ $? -eq 0 ]
then
echo 0 > /tmp/count
else
if [ -f /tmp/count ]
then
c=`cat /tmp/count`
if [ $c -ge 4 ]
then
/usr/sbin/init
else
echo `/usr/bin/expr $c + 1` > /tmp/coun
fi
else
echo 1 > /tmp/count
fi
fi
/usr/bin/sleep 300
done
I am not a shell scripter or anything near it. So, can you please tell me what $c -ge 4 is? Also, What time intervals will this script keep checking the network? Also, will this script shutdown the server? And after finding the network is down will it shutdown the server?
THANK YOU!
while true
do
/usr/sbin/ifconfig en0 | /usr/bin/grep UP
if [ $? -eq 0 ]
then
echo 0 > /tmp/count
else
if [ -f /tmp/count ]
then
c=`cat /tmp/count`
if [ $c -ge 4 ]
then
/usr/sbin/init
else
echo `/usr/bin/expr $c + 1` > /tmp/coun
fi
else
echo 1 > /tmp/count
fi
fi
/usr/bin/sleep 300
done
I am not a shell scripter or anything near it. So, can you please tell me what $c -ge 4 is? Also, What time intervals will this script keep checking the network? Also, will this script shutdown the server? And after finding the network is down will it shutdown the server?
THANK YOU!
Here's an amended version of omarfarid's script that hopefully is a little clearer (note that you should give the points on omarfarid)
#!/bin/ksh
COUNT=/tmp/count$$ # Temp file for storing count of network down state
INTERVAL=300 # Number of seconds between checks
while true
do
if /usr/sbin/ifconfig en0 | /usr/bin/grep UP >/dev/null
then
echo 0 >$COUNT
else
if [ -f $COUNT ]
then
c=`cat $COUNT`
if [ $c -ge 4 ]
then
/usr/sbin/shutdown -F
else
let c=c+1
echo $c >$COUNT
fi
else
echo 1 >$COUNT
fi
fi
/usr/bin/sleep $INTERVAL
done
since you want to check if the interface is down for 20 min, and since the script sleeps 300 sec (5 min) then the script will wait 4 times 5 min before it decides to shutdown the system. The mentioned line $c -ge 4 is a test of the counter stored in the /tmp/count file. the content of the file is read into variable c and -ge is a test if it is greater than or equal (please see man test for more info on tests). Please note that the line
/usr/sbin/init
should be
/usr/sbin/init 0 (this is run level 0 which basically means bring system down and if your system supports power off then it will power off the system, please check man init for more info).
/usr/sbin/init
should be
/usr/sbin/init 0 (this is run level 0 which basically means bring system down and if your system supports power off then it will power off the system, please check man init for more info).
Thank you Tintin for the enhancements to the script and welcome to share points :)
I do not have an aix system and it is hard to know all commands available on that platform (of course I can search Internet for docs but this takes time)
I do not have an aix system and it is hard to know all commands available on that platform (of course I can search Internet for docs but this takes time)
ASKER
I am sorry...let me reiterate. I only have a 20 min window at max when the network will be up. So, when the script checks for the network and finds the 'UP' missing I want it to check every 5 mins for 3 times, totaling 15mins. Then after the 3rd check, if the 'UP' is missing to shutdown the sever. But if the 'UP' is there every 5 mins to not shutdown server.
I hope this make sense to what I am trying to do.
I hope this make sense to what I am trying to do.
then change 4 to 2
0 - sleep 5 - 1 - sleep 5 - 2 - sleep 5 - shutdown
0 - sleep 5 - 1 - sleep 5 - 2 - sleep 5 - shutdown
ASKER
ok, I will change the 4 to 2. But what is what you wrote mean:"0 - sleep 5 - 1 - sleep 5 - 2 - sleep 5 - shutdown" mean? Do I need to add this in to the script too? Finally, do I need to create a /tmp/count directory? SORRY to keep asking soo many questions..its just I am not good at scripting.
No don't add to script , I was showing time line along with counter value :)
counter time counter time counter time action
0 5 1 10 2 15 shutdown
counter time counter time counter time action
0 5 1 10 2 15 shutdown
ASKER
Ok thank you omarfarid. I am going to test the script now and let you know how it goes. But, do I need to create a /tmp/count directory?? Because tintin has indicated in the script he put up...# Temp file for storing count of network down state
No, the file will be created if not found. If you wish you may create it yourself 1st time but no need to keep track of this file by yourself:
echo 0 > /tmp/count
echo 0 > /tmp/count
ASKER
I ran the script and does nothing. But, it did create a count file that had 0 in it. What do I need to do?
ASKER
I copied exactly what tintin and you scripted for me and it does not shutdown the server. What should I do?
It should shutdown the server if the interface is down.
Run it again by doing
ksh -x scriptname
and post the results
Run it again by doing
ksh -x scriptname
and post the results
ASKER
Here is my output with ksh -x network-montior:
# ksh -x network-monitor2
+ COUNT=/tmp/count548932
+ INTERVAL=60
+ true
+ /usr/sbin/ifconfig en0 monitor
+ /usr/bin/grep UP
+ /usr/sbin/ifconfig en0
+ 1> /dev/null
+ echo 0
+ 1> /tmp/count548932
+ /usr/bin/sleep 60
I tested it and it does not shutdown the server.
Here is my script:
#!/bin/ksh
COUNT=/tmp/count$$ # Temp file for storing count of network down state
INTERVAL=60 # Number of seconds between checks
while true
do
/usr/sbin/ifconfig en0 monitor
if /usr/sbin/ifconfig en0 | /usr/bin/grep UP >/dev/null
then
echo 0 >$COUNT
else
if [ -f $COUNT ]
then
c=`cat $COUNT`
if [ $c -ge 2 ]
then
/usr/sbin/shutdown -F
else
let c=c+1
echo $c >$COUNT
fi
else
echo 1 >$COUNT
fi
fi
/usr/bin/sleep $INTERVAL
done
# ksh -x network-monitor2
+ COUNT=/tmp/count548932
+ INTERVAL=60
+ true
+ /usr/sbin/ifconfig en0 monitor
+ /usr/bin/grep UP
+ /usr/sbin/ifconfig en0
+ 1> /dev/null
+ echo 0
+ 1> /tmp/count548932
+ /usr/bin/sleep 60
I tested it and it does not shutdown the server.
Here is my script:
#!/bin/ksh
COUNT=/tmp/count$$ # Temp file for storing count of network down state
INTERVAL=60 # Number of seconds between checks
while true
do
/usr/sbin/ifconfig en0 monitor
if /usr/sbin/ifconfig en0 | /usr/bin/grep UP >/dev/null
then
echo 0 >$COUNT
else
if [ -f $COUNT ]
then
c=`cat $COUNT`
if [ $c -ge 2 ]
then
/usr/sbin/shutdown -F
else
let c=c+1
echo $c >$COUNT
fi
else
echo 1 >$COUNT
fi
fi
/usr/bin/sleep $INTERVAL
done
What does the line
/usr/sbin/ifconfig en0 monitor
do?
Are you 100% the interface is down when you are testing?
/usr/sbin/ifconfig en0 monitor
do?
Are you 100% the interface is down when you are testing?
#/bin/ksh
COUNTER=0
LOG=/tmp/pingdead.out
GW=$(lsattr -El inet0 | grep route | cut -d"," -f6 | cut -d" " -f1)
while [[ $COUNTER -le 3 ]];do
/usr/bin/ping -c1 -w1 $GW
if [[ $? -eq 0 ]];then
COUNTER=0 >> $LOG
sleep 300
else
COUNTER=$(( $COUNTER+1 )) >> $LOG
sleep 300
fi
if [[ $COUNTER -eq 4 ]];then
echo "I am unable to connect to my friends...Commiting suicide" >> $LOG
echo `date` >> $LOG
echo "Server shutdown initiated" >> $LOG
sudo shutdown -Fr now
fi
done
COUNTER=0
LOG=/tmp/pingdead.out
GW=$(lsattr -El inet0 | grep route | cut -d"," -f6 | cut -d" " -f1)
while [[ $COUNTER -le 3 ]];do
/usr/bin/ping -c1 -w1 $GW
if [[ $? -eq 0 ]];then
COUNTER=0 >> $LOG
sleep 300
else
COUNTER=$(( $COUNTER+1 )) >> $LOG
sleep 300
fi
if [[ $COUNTER -eq 4 ]];then
echo "I am unable to connect to my friends...Commiting suicide" >> $LOG
echo `date` >> $LOG
echo "Server shutdown initiated" >> $LOG
sudo shutdown -Fr now
fi
done
You can modify the script a little as per requirement...
I am not sure if the steps are executed in the proper sequence:
+ /usr/sbin/ifconfig en0 monitor
+ /usr/bin/grep UP
+ /usr/sbin/ifconfig en0
grep is run before the 2nd ifconfig that should show the interface status. Can you run my version of the script (with -x) and post output? Also, if you disconnect the interface and run ifconfig do you see the interface down immediately or does it take time? Can you leave the script running and disconnect the cable for say 10 min?
+ /usr/sbin/ifconfig en0 monitor
+ /usr/bin/grep UP
+ /usr/sbin/ifconfig en0
grep is run before the 2nd ifconfig that should show the interface status. Can you run my version of the script (with -x) and post output? Also, if you disconnect the interface and run ifconfig do you see the interface down immediately or does it take time? Can you leave the script running and disconnect the cable for say 10 min?
if not then check how many times it failed to reach gateway (could be in a counter in flat file). It then shutdown system with proper tools.
A more advanced way is to have a power management S/W installed to monitor power and then shutdown system after 20 min if power is still down.
What is your platform?