RabbieBurns Posted January 21, 2010 Posted January 21, 2010 Ive got a server that runs a runs a script on the hour. Trouble is said server is a bit prone to the occasional lockup or crash etc and sometimes the script wont run untill I can get someone to reboot the server.. Got a backup server that runs, that isnt really capable of running the script regularly but could cope with it for a couple of days.. I was wondernig if there was a wayh of running a script on the backup server to do the following Check main server is replying to ping if OK do nothing if no ping, run the script locally Both machines are gentoo linux so a perl / bash / python or whatever would be fine to run Any ideas would be appreciated.. cheers..
morganw Posted January 21, 2010 Posted January 21, 2010 This looks like a BASH script that would do it. #!/bin/bash # Simple SHELL script for Linux and UNIX system monitoring with # ping command # ------------------------------------------------------------------------- # Copyright (c) 2006 nixCraft project # This script is licensed under GNU GPL version 2.0 or above # ------------------------------------------------------------------------- # This script is part of nixCraft shell script collection (NSSC) # Visit http://bash.cyberciti.biz/ for more information. # ------------------------------------------------------------------------- # Setup email ID below # See URL for more info: # http://www.cyberciti.biz/tips/simple-linux-and-unix-system-monitoring-with-ping-command-and-scripts.html # ------------------------------------------------------------------------- # add ip / hostname separated by white space HOSTS="cyberciti.biz theos.in router" # no ping request COUNT=1 # email report when SUBJECT="Ping failed" EMAILID="[email protected]" for myHost in $HOSTS do count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }') if [ $count -eq 0 ]; then # 100% failed echo "Host : $myHost is down (ping failed) at $(date)" | mail -s "$SUBJECT" $EMAILID fi done Got it from here UNIX / Linux Shell Script For Monitoring System network with ping command
Gerry Posted January 21, 2010 Posted January 21, 2010 I Googled perl ping and got this: Net::Ping - perldoc.perl.org The example script shows how to use it. Then, to launch a script if there's no ping, try: exec - perldoc.perl.org Then you could cron your "pinging" script to run it every minute, hour, day, specific time etc...
box_l Posted January 21, 2010 Posted January 21, 2010 (edited) how about these? #!/bin/bash if ping -c 1 4.2.2.2 then : # colon is a null and is required else **** your command here **** fi and found this in an old script of mine, cant remember where it came from tho. this one would detect if the link was dropping some of the packets #!/bin/bash while [ -n "$(ping -c 1 192.168.0.1|grep 100%)" ] do : # colon is a null and is required else **** your command here **** BoX Edited January 21, 2010 by box_l 1
RabbieBurns Posted January 21, 2010 Author Posted January 21, 2010 how about these? #!/bin/bash if ping -c 1 4.2.2.2 then : # colon is a null and is required else **** your command here **** fi Thats perfect. Simple and effective. I added a > /dev/null to the first line to supress the output of the ping to the screen. Thanks
Rokixz Posted November 3, 2012 Posted November 3, 2012 Hello, I'm trying to make script with bash so I want to gain more experience with this super tool. I've trying to make a host script to make job more effective and faster. #!/bin/bash #By Rokas Klyvis echo "Iveskite adresa arba IP" read hostname host $hostname host www.$hostname host mail.$hostname whois $hostname | grep -E 'Domain:|Nameserver:|Status:|Registrar:' PACKETS=1 for myHost in $hostname do check=$(ping -c $PACKETS $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }') if [ $check -eq 0 ]; then echo "Hostname : $myHost nepingina" else echo "Hostas pingina OK" fi done So for example, if I put domain, that doesn't exist, this script gives error ./host.sh: line 16: [: -eq: unary operator expected I understand, that awk cutting doesn't help anymore, tried to make if statements with != ping: unknown host $myHost but still no luck, I just need to make, if host doesn't exists, just to show single line "Host doesn't exist" and that's it. It should be useful for many sysadmins so would be lovely to get your pros help for such newbies like me Regards.
Steve21 Posted November 3, 2012 Posted November 3, 2012 (edited) It's been a while since I've done bash, but can't you ignore most of that and just use the return value from ping? ping -c 1 $myHost > /dev/null if [ $? -eq 0 ]; then echo "Ping Failed" else echo "Ping Ok" fi Every command in bash will always return a return code to $? so just use that to get your answer from. No need to populate/copy from output (or shouldn't be if i remember correctly) Should work anyway. - Edit: Might have my "failed" and "ok" wrong way round. But can't really test it on windows machine If it's doing it back to front, just change echos etc Steve Edited November 3, 2012 by Steve21
Rokixz Posted November 3, 2012 Posted November 3, 2012 Thanks for quick reply, will try to make advanced sheet and than share, or maybe there are some good admin scripts? Will check later on
costis Posted September 17, 2014 Posted September 17, 2014 heh, sorry for opening this again, I did this: while true; do ping -c 3 8.8.8.8 | grep "0 received" && mplayer -volume 150 -loop 0 ~/Music/siren_alert.mp3 || echo Ping OK : $(date) done This way only when it does not receive 3 consecutive pings (with "-c 3") it alerts me. If it misses only one or two everything is fine.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now