script problem

0 views
Skip to first unread message

anil

unread,
Dec 16, 2009, 12:45:02 AM12/16/09
to VGLUG
Dear guy's

I have made one script but want some edition dont know how to add it
into it..

script is
----------------------------------------------------
#!/bin/bash
for n in 1 2 3 4 7 8 9 10 11
do
host=192.$n.0.1
ping -c5 $host &> /dev/null
if [ $? = 0 ]; then
echo "$host is UP"
else echo "$host is Down" | mail -s "$host is DOWN" a...@xyz.com
fi
done
-----------------------------------------------------------

It is working but it's thrwing mail only when host is down if host
revert back online means once its send for perticuler down host and
after i want a mail lafter that host up that -- host is up....

( host down this mail is coming but iots not sending when that host
gets up) i have set this in cron with *** every second its checing for
up & Down )
how to do it..?!!

Thanks & Regards

Alok Thaker

unread,
Dec 16, 2009, 3:27:42 AM12/16/09
to vg...@googlegroups.com
You haven't mentioned to send the mail when the host is up only you have setup for sending the mail when the host is down.

Thanks,
Alok
Inferno Solutions !
www.inferno.co.in !
Go with the best !
All Linux-Java Solutions-Training under one Roof !
M:-9925519798


--
Please read http://www.catb.org/~esr/faqs/smart-questions.html before posting.
You received this message because you are subscribed to the "Vibrant GNU/Linux User Group".
To stop receiving emails from this group, mail to VGLUG-un...@googlegroups.com
To post to this group, send email to VG...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/VGLUG

anil

unread,
Dec 16, 2009, 3:37:12 AM12/16/09
to VGLUG
Dear alok,

Thanks for replying .. that is what i dont know how to set can you
please tell me how to set it if that down host is getting up then it
throw one mail.

Thanks

On Dec 16, 1:27 pm, Alok Thaker <alok.a...@gmail.com> wrote:
> You haven't mentioned to send the mail when the host is up only you have
> setup for sending the mail when the host is down.
>
> Thanks,
> Alok
> Inferno Solutions !www.inferno.co.in!
> Go with the best !
> All Linux-Java Solutions-Training under one Roof !
> M:-9925519798
>
> > Please readhttp://www.catb.org/~esr/faqs/smart-questions.html<http://www.catb.org/%7Eesr/faqs/smart-questions.html>before posting.

Alok Thaker

unread,
Dec 16, 2009, 4:04:27 AM12/16/09
to vg...@googlegroups.com
Hi,

Try this :-

#!/bin/bash
for n in 1 2 3 4 7 8 9 10 11
do
host=192.$n.0.1
ping -c5 $host &> /dev/null
if [ $? = 0 ]; then
echo "$host is UP" | mail -s "$host is up" a...@xyz.com

else  echo "$host is Down" | mail -s "$host is DOWN" a...@xyz.com
fi
done
 
Thanks,
Alok
Inferno Solutions !
www.inferno.co.in !
Go with the best !
All Linux-Java Solutions-Training under one Roof !
M:-9925519798

anil

unread,
Dec 16, 2009, 4:23:10 AM12/16/09
to VGLUG
Dear alok,

It'll send a mail for all hosts which are up or what .?
if yes then i'll get 10 mails in every second. cause * * * * incron
running this script.

so need a mail when host is donw and then again need a mail when that
one perticuler down host will gets up.
( if any mistake sorry )
Thanks

On Dec 16, 2:04 pm, Alok Thaker <alok.a...@gmail.com> wrote:
> Hi,
>
> Try this :-
>
> #!/bin/bash
> for n in 1 2 3 4 7 8 9 10 11
> do
> host=192.$n.0.1
> ping -c5 $host &> /dev/null
> if [ $? = 0 ]; then
> echo "$host is UP" | mail -s "$host is up" a...@xyz.com
> else  echo "$host is Down" | mail -s "$host is DOWN" a...@xyz.com
> fi
> done
>
> Thanks,
> Alok
> Inferno Solutions !www.inferno.co.in!
> Go with the best !
> All Linux-Java Solutions-Training under one Roof !
> M:-9925519798
>

ElectroMech

unread,
Dec 16, 2009, 12:38:52 PM12/16/09
to vg...@googlegroups.com
Hi,

On Wed, Dec 16, 2009 at 2:53 PM, anil <anillo...@gmail.com> wrote:
Dear alok,

It'll send a mail for all hosts which are up or what .?
if yes then i'll get 10 mails in every second. cause * * * * incron

Second ???

The minimum time with cron is 1 minute.

I never heard about second.
 
running this script.

so need a mail when host is donw and then again need a mail when that
one perticuler down host will gets up.
( if any mistake sorry )

No need to say sorry try to be specific and give proper description of your task , discussion or problem.

Thanks and regards.
 
--
--
Nilesh Vaghela
ElectroMech
Redhat Channel Partner and Training Partner
404, Maulik arcade, Above Karnavati Pagarakha Bazar,
Nr. Mansi cross Road,
Satellite Rd, Ahmedabad
25, The Emperor, Fatehgunj, Baroda.
www.electromech.info

anil

unread,
Dec 16, 2009, 11:00:18 PM12/16/09
to VGLUG
Dear sir thanks.

But still problem is thr when ever any host is getting down it is
sending so many mails...i want to set like whenevr any host get down
just it send only one mail and when it gets up again it'll send mail
like host is up.. want to set this for 10 hosts.

how to set it please suggest.

thanks

On Dec 16, 10:38 pm, ElectroMech <electrom...@electromech.info> wrote:
> Hi,
>

Abhishek

unread,
Dec 17, 2009, 5:41:09 AM12/17/09
to VGLUG
Hi Anil,

I have made some changes to the script. Hope it may help you.

######################

#!/bin/bash

touch /tmp/host_down

for i in $(cat /tmp/host_down)
do
/bin/ping -c5 $i &> /dev/null


if [ $? = 0 ]; then

echo "$i is UP"| mail -s "$i is UP" a...@xyz.com
fi
done

cat /dev/null > /tmp/host_down

for n in 1 2 3

do
host=192.$n.0.1
/bin/ping -c5 $host &> /dev/null
if [ $? = 1 ]; then
echo "$host is DOWN"| mail -s "$host is DOWN" a...@xyz.com
echo $host >> /tmp/host_down
fi
done
############################

Best Regards,
Abhishek

> > 25, The Emperor, Fatehgunj, Baroda.www.electromech.info- Hide quoted text -
>
> - Show quoted text -

anil

unread,
Dec 17, 2009, 6:00:51 AM12/17/09
to VGLUG

> > > 25, The Emperor, Fatehgunj, Baroda.www.electromech.info-Hide quoted text -


>
> > - Show quoted text -

Dear abhishek,

Thanks for replying

how it'll work not getting yaar.!! script is not working better now..

old one is working and sending mails when hosts are down but not
sending mails when they are up cause no settings in script( old
script).

Please help.

Abhishek

unread,
Dec 17, 2009, 6:17:00 AM12/17/09
to VGLUG, anil
Hi Anil,

The script will create a file in /tmp/host_down which will retain the
servers which are down. In the first for loop it will check only those
servers which are down and if any of them is up then only it will send
the mail.

In the second for loop it is the usual logic that you are using only
exception is that it will add the failed servers in /tmp/host_down.

You can check the servers which are down in /tmp/host_down.

Best Regards,
Abhishek

> > > > 25, The Emperor, Fatehgunj, Baroda.www.electromech.info-Hidequoted text -


>
> > > - Show quoted text -
>
> Dear abhishek,
>
> Thanks for replying
>
> how it'll work not getting yaar.!! script is not working better now..
>
> old one is working and sending mails when hosts are down but not
> sending mails when they are up cause no settings in script( old
> script).
>

> Please help.- Hide quoted text -

Bhatt Niraj

unread,
Dec 17, 2009, 4:00:10 PM12/17/09
to vg...@googlegroups.com


2009/12/17 Abhishek <abhishe...@gmail.com>

You received this message because you are subscribed to the "Vibrant GNU/Linux User Group".
To stop receiving emails from this group, mail to VGLUG-un...@googlegroups.com
To post to this group, send email to VG...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/VGLUG

Hi,
 
Insted of using own script you can use monit , it is build in C and working fine.
 
For your script,
 
1. Create list of all your hosts.
2. One Flag variable for each hosts (like DOWN= 1/0)
3. Check ping state of your host, if it down then first check whether its flat is set to 1 or not, if its one then no mail else set its flag to 1 and send mail.
4. When you host will up then just reset appropirate flag to 0 and send mail.
 
Regards,
Niraj Bhatt
 


--
Niraj Bhatt
------------------------------------------------------------
Give me space to stand, I will move the earth

ElectroMech

unread,
Dec 17, 2009, 11:49:39 PM12/17/09
to vg...@googlegroups.com
Hi

On Thu, Dec 17, 2009 at 4:30 PM, anil <anillo...@gmail.com> wrote:


On Dec 17, 3:41 pm, Abhishek <abhishekbose...@gmail.com> wrote:
> Hi Anil,
>
> I have made some changes to the script. Hope it may help you.

This can be achieve in single command also .
Like


ping -c1 192.168.0.253 &>/dev/null && echo "Ping successful" | mail -s "ping sucess" user1 || echo  "Ping not successful" | mail -s "unsuccessful_ping" user1


If you want you can incorporate variable with scrip to pass the ip to be ping.
 

Abhishek

unread,
Dec 18, 2009, 12:51:00 AM12/18/09
to VGLUG

Hi Nilesh bhai,

You are correct that your single line command will also work. But he
needs some thing which will only mail when some host is DOWN and again
when the host is UP. The command which you have given, it will mail
for all the servers whether it is DOWN or UP continuously whenever the
script runs. The script which I had made incorporates this facility.
Let me know if I am wrong.

Best Regards,
Abhishek

On Dec 18, 5:49 am, ElectroMech <electrom...@electromech.info> wrote:
> Hi
>

manish choudhary

unread,
Dec 17, 2009, 11:18:06 PM12/17/09
to vg...@googlegroups.com
the correct script is in 033 rhce book so just see in that book
and create the perfect script then it will be work

 

nehal dattani

unread,
Dec 18, 2009, 2:21:51 PM12/18/09
to vg...@googlegroups.com
Hi Anil,

This should work for you. Some minor changes may be required as per your network settings.

Let me know if you face any problems.
##############################################################################
#! /bin/bash

for n in 1 2 3 4 5 6 7 8 9 10 11
do
host=192.168.1.$n
ping -c 5 $host
st=$?
if [ $st = 0 -a ! -f /tmp/$host.dwn ]; then
echo "Host $host is up";
elif [ $st = 0 -a -f /tmp/$host.dwn ]; then
rm -f /tmp/$host.dwn
echo "Host $host is up again at `date +%M:%H,%d%m%y`" | mail -s "$host is up now"
elif [ $st -ne 0 -a  -f /tmp/$host.dwn ];
then echo "Host $host is down";
elif [ $st -ne 0 -a  ! -f /tmp/$host.dwn ];
then
touch /tmp/$host.dwn
echo "Host $host is down at `date +%M:%H,%d%m%y`" | mail -s "$host is down"
fi
done
##############################################################################

--
Please read http://www.catb.org/~esr/faqs/smart-questions.html before posting.
You received this message because you are subscribed to the "Vibrant GNU/Linux User Group".
To stop receiving emails from this group, mail to VGLUG-un...@googlegroups.com
To post to this group, send email to VG...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/VGLUG



--
The production of too many useful things results in too many useless people.

With best regards,
Nehal Dattani

anil

unread,
Dec 21, 2009, 11:03:30 PM12/21/09
to VGLUG

> > Please readhttp://www.catb.org/~esr/faqs/smart-questions.html<http://www.catb.org/%7Eesr/faqs/smart-questions.html>before posting.


> > You received this message because you are subscribed to the "Vibrant
> > GNU/Linux User Group".
> > To stop receiving emails from this group, mail to
> > VGLUG-un...@googlegroups.com
> > To post to this group, send email to VG...@googlegroups.com

> > For more options, visit this group athttp://groups.google.com/group/VGLUG


>
> --
> The production of too many useful things results in too many useless people.
>
> With best regards,
> Nehal Dattani

Dear nehal Thanks a lot this script works..
can you pelase set the last one thing for me in this script.. when
host gets down it send's only 2 mails and again when that one host
get's up it'ss send 2 mails that host is up .

Now it is sending mails properly when host is down but it is sending
mails till host gets up so need 4 mails only when down and wen it
get's up.

Thanks

Reply all
Reply to author
Forward
0 new messages