Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1440
  • Last Modified:

AIX -- How do I create a ping shell script

I need help creating a shell script using ping.  I need it to check the network and see if its up every 5 mins. If the script finds the network up, I want it to go back to sleep. However if it finds the network down to check it 3 times in 5 min intervals. By the third check, if the the network is still down to shutdown the server.  
*I am running AIX 5.3
0
AIX25
Asked:
AIX25
  • 6
  • 3
  • 2
4 Solutions
 
woolmilkporcCommented:
Hi AIX25,

I would have found this Q earlier had you put it in the AIX zone.
Anyway.
Great idea to shut down a server when it looses its network.

I'm in ol' Europe here, and its a bit late.
Should this question still be open in about 10 hrs. or so, I'll see what I can do.

An idea how to find the standard gateway in AIX (a good address to ping) is here -

gw=$(lsattr -El inet0 | grep route | cut -d"," -f6 | cut -d" " -f1)

à bientôt

wmp





0
 
AIX25Author Commented:
Ok hopefully it is still open because I like the way you explain things, you make it easy to understand.
Also I ran #gw=$(lsattr -El inet0 | grep route | cut -d"," -f6 | cut -d" " -f1) and nothing came back or no output.

Thank you
0
 
woolmilkporcCommented:
The command I gave you just fills the variable 'gw' with an IP address.
You'll have to issue 'echo $gw' to view the variable's contents.

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
kishored2004Commented:
#/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
0
 
woolmilkporcCommented:
@kishored - thanks for using my 'lsattr' thing. Seems to be useful, right?
@AIX25 - try that piece. Looks sensible, in a way.
 
wmp
 
0
 
woolmilkporcCommented:
... I just saw the -r flag of shutdown (=reboot). I think this doesn't make sense regarding your requirement, should it be the same as in your 'ifconfig' question.
0
 
kishored2004Commented:
yes...it should have been shutdown -Fh.....

sorry for the mistake

Thanks...wmp...thanks for the lsattr thingy...hope this solves the problem

Best of luck!
0
 
AIX25Author Commented:
I do not know any shell scripting. Do I copy the script EXACTLY?? Also, would you be able to include where I have to enter my certain ip address or any other significant info based of my server.  One final request, can you give me a short explanation of each line on the script.

Thank you very much
0
 
woolmilkporcCommented:
Hi again,
- to comment only on the part I contributed:
Your IP address to ping (your default gateway's address) is found automatically by the script. It's the line
GW=$(lsattr -El inet0 | grep route | cut -d"," -f6 | cut -d" " -f1)
which means
- Look at the basic IP configuration (lsattr -El inet0 - try it and see what it gives),
- take only the line containing 'route' (grep route),
- take the sixth comma-delimited field from begin-of-line (cut -d"," -f6),
- and take the first blank-delimited field ot the remaining string (cut -d " " -f1).
Data is passed from command to command via pipes ( | ) , which take the output of the program on the left and pass it as input to the program on the right.
The result is stored in a variable called GW, whose contents ($GW) are later used as target of the ping. (ping $GW)
Other data specific to your server are not needed. It's all standard.
Have fun!
 
wmp
 
 
0
 
AIX25Author Commented:
@woolmilkporc:
When I name the script, do I need to put a .ksh extension?
Do I execute the script with ./sample_script or ksh -x sample_script?
Finally, in the inittab, what will I need to put in the script to indicate to the inittabe to execute the sample_script??

Thank you!
0
 
woolmilkporcCommented:
1) Unlike with e.g. Windows, an extension is not needed (but not forbidden either).
Choose a meaningful name, so in perhaps several months you can still guess what the script is supposed to do.

2) Look at the first line of the script. The #! (among unixers called a shebang) tells your shell what to do with the script's body. In your case, the #!/bin/ksh means "Call /bin/ksh and let it execute the whole thing"
Additionally the script itself has to be made executable. Do this by issuing 'chmod +x [scriptname]', e.g.  chmod +x sample_script
Given this (the shebang and the chmod), a special call like ksh ... is not needed (but not forbidden, it's Unix).

3) The inittab needs the full path to the script, as I told you in our respective case. The above script itself doesn't need anything special when called from inittab.
It is good practice, however, to call the commands inside the shell by their full paths, so use e.g. /usr/bin/grep instead of grep alone. For commands not in /usr/bin or /bin this is even mandatory.
Find the full paths of commands by issuing 'which [command]', e.g. which grep.
'echo' and 'sleep' are shell builtins and as such don't need a path (although there are standalone versions, too).

Cheers

wmp


0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now