# Help in writing a script in AIX!!!



## wildpunk (Aug 21, 2007)

hi all,

i just started learning AIX.
i need your help in creating a script in AIX.
i want to check whether the internet links at my work are up or not.
if not the script shld send n sms stating the links ip address and a message tht the link is down.
and this script should run every 15 minutes.
i know i can use cronjob to make this script run every 15 minutes.
i can ping and store the ping result in a file.
i can sms the result to a mobile phone as well.
wat i cant do is to compare the result of the file which has ping result to make sure the file has reponse or no response so tht i can use the sms function correctly.

if neone can help me please!!!


----------



## lotuseclat79 (Sep 12, 2003)

Hi wildpunk,

Welcome to TSG!

If you use the Firefox browser, try the Mr. Uptime add-on extension which can be found here.

If this does not fit your requirement let us know that you still need to do the script.

Ok, I just saw this a few hours ago and posted before trying it. Apparently, Mr Uptime does not work for me - don't know if its just new (rel. 0.9.2) and still in testing or works better on other systems. I run Ubuntu Fiesty 7.04 knockoff aka Ultimate Ubuntu 1.4 Live CD - and the Preferences window did not come up in order to be able to set it.

WRT scripting the ping command, you can use the -c parameter as in:
ping -c 1 <url or ip address> and then check the result of the ping command via an echo $? command testing it in an if statement to put out up or down status.
At least that is the way I would write the shell script.

What shell are you using in AIX? sh, csh, zsh, tcsh?

-- Tom


----------



## wildpunk (Aug 21, 2007)

hi tom,
thanks for a fast reply.

to start with i m using korn shel---ksh.

i did the same watever u said.
i started my script with a ping - c command and stored the result in a file called pingresult.txt

now i dunno how to run a if statement to check the contents of the file means 
how can i run if statement to check whether the file has replies or request timed out.

so tht i can do the needfull step.

hope this answers ur question.

thanks 
Tapan


----------



## lotuseclat79 (Sep 12, 2003)

Hi wildpunk,

ksh will be able to process sh commands readily.

Here's a snippet for you to try:
ping -c 1 <url>
if [ $? = 0 ]; then <up>; else <down>; fi

You could also try putting the two stmts above in a while loop with a wait stmt where the argument to the wait stmt is in seconds, i.e. 900=15 minutes:
while <down>
ping -c 1 <url>
if [ $? = 0 ]; then <up>; else <down>; fil
wait 900
end

Read the man page to learn the syntax of scripting, or better yet use Google, like this to search for a tutorial on scripting zsh:
Google search box: zsh +tutorial +scripting

-- Tom


----------



## wildpunk (Aug 21, 2007)

hi tom,

i really appricaite ur help.

Here's a snippet for you to try:
ping -c 1 <url>
if [ $? = 0 ]; then <up>; else <down>; fi

now in the above exp use used $? wats this for.
now n how can i comapre the count
if i do ping -c 1 <url>
it is supposed to ping the url only once.
if it is up it will give me one reply otherwise no replies.
but how can i make sure it gave me a reply so tht i can compre with the $? variable.

i m trying to do man on commands.
but thr are somethins which man cant help out with,
i m using google as well.

thanks for all the help.

waitin for ur reply


----------



## lotuseclat79 (Sep 12, 2003)

Execute the command: ping -c 1 <url> replacing <url> with a web address either by name or ip address.
Immediately afterwards issue the command: echo $?

The return code of the ping command preceeding the echo command is output. A '0' return code indicates the website is up, anything else and most likely the ping command will have 100% packet loss.

ping -c 1 <url> only pings the ,url. once.

The echo $? output is the reply where 0 = up anything else means not accessible.

You do not need more than one count to compare, just use the echo $? output with a count = 1, then check it like in the code snippet and replacing <up> and <down> with the action/command(s) you want. Don't forget to wait 900 for 15 minutes.

-- Tom


----------



## ghostdog74 (Dec 7, 2005)

in some corporations, ping is disallowed. Internet access may be through some proxy servers. If you want to check whether the your internet is up...you can 
1) use wget to download a webpage. If you can view the web page, it means your internet is 
2) use languages like Perl/Python that has HTTP libraries so you can code your own script to download a web page and grab their contents.


----------



## wildpunk (Aug 21, 2007)

hi tom,

y are we using wait 900 here.
i will be assigning this script a cronjob which will run every 10 minutes.
do i still have to use wait for this?


----------



## wildpunk (Aug 21, 2007)

just one more question.
wat if i do

ping -c 1 <url> >output.txt
grep -c "ttl" output.txt

grep -c will show me the count of ttl
which will be 1 if link is up 0 if link is down.
but i dunno how can i use this value to compare.
grep just displays it on screen.
is thr any way to store it in a variable.


----------



## lotuseclat79 (Sep 12, 2003)

wildpunk said:


> hi tom,
> 
> y are we using wait 900 here.
> i will be assigning this script a cronjob which will run every 10 minutes.
> do i still have to use wait for this?


No, only if you don't run a cronjob and run the script from a terminal window instead. Adn 900 represents 15 minutes of wait time, 600 would be 10 minutes.

-- Tom


----------



## lotuseclat79 (Sep 12, 2003)

wildpunk said:


> just one more question.
> wat if i do
> 
> ping -c 1 <url> >output.txt
> ...


Hi wildpunk,

First, ttl is the time to live. For example, when I issue the ping command:
$ ping -c 1 forums.techguy.org
I get the output:
ping -c 1 forums.techguy.org
PING forums.techguy.org (209.183.226.152) 56(84) bytes of data.
64 bytes from www.techguy.org (209.183.226.152): icmp_seq=1 ttl=55 time=143 ms
--- forums.techguy.org ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 143.805/143.805/143.805/0.000 ms

Note the ttl value above, i.e. ttl does not represent what you think it does.

If the ping command returns other than a 0, the site is down. As I have been recommending all along:
ping -c 1 <url>
if [ $? = 0 ]; then <up command action>; else <down command action>; fi
end

-- Tom


----------



## rahul_r_k (Aug 30, 2007)

Hi 
We have some shell scripts running perfectly on Solaris box .
Recently we got a new server ( AIX ) and we are supposed to move few applications using these scripts on it.
Just wanted your opinion on whether we can run those scripts without any changes on AIX box or it will require some alterations ?


----------



## Squashman (Apr 4, 2003)

You should probably look into using some of the very good Open source network monitoring software.
My friend is the Admin for a very large school system and he runs Nagios.
http://www.nagios.org/

Or run nagios and splunk.
http://www.nagios.org/products/enterprisesolutions/splunk/


----------

