Something for cron:
#!/bin/sh
if ping -c 1 -w 1 foo >/dev/null; then
echo okay
else
echo shoot
fi
And a standalone script I'm using on one box:
#!/usr/local/bin/tclsh
# network heartbeat monitor for my machines
# with Orinoco PCMCIA wireless network cards
# which unaccountably go kerflooey whenever
# they are put into promiscuous mode ;^(
set fid [ open /var/log/netbeat.log a+ 0644 ]
puts $fid "[ clock format [ clock seconds ] ] desktop:80: netbeat pid: [ pid ]"
close $fid
while { 1 } {
if { [ catch {
set sid [ socket -async desktop 80 ]
} err ] } {
set fid [ open /var/log/netbeat.log a+ 0644 ]
puts $fid "[ clock format [ clock seconds ] ] desktop:80: $err"
close $fid
}
after 1000
if { [ catch { fconfigure $sid -peername } ] } {
set fid [ open /var/log/netbeat.log a+ 0644 ]
puts $fid "[ clock format [ clock seconds ] ] desktop:80: calling 'cardctl reset'"
close $fid
catch { exec cardctl reset }
after 60000
} else {
close $sid
}
after 9000
}
And a newer version of the same:
#!/usr/local/bin/tclsh
# network heartbeat monitor for my machines
# with Orinoco PCMCIA wireless network cards
# which unaccountably go kerflooey whenever
# they are put into promiscuous mode ;^(
# configuration options
set ::host desktop # another host on the lan
set ::port 80 # the port on the other host to 'ping'
set ::logfile /var/log/netbeat.log
set ::kills [ list netwatch mozilla ]
# log pid at startup
proc init { args } {
log "netbeat pid: [ pid ]"
}
proc sleep { seconds } {
set seconds [ expr int($seconds) ]
if { $seconds > 1000 } {
set seconds [ expr { $seconds / 1000 } ]
}
after [ expr { $seconds * 1000 } ]
}
# generic logging function
proc log { args } {
set timestamp [ clock format [ clock seconds ] ]
set fid [ open $::logfile a+ 0644 ]
puts $fid "${timestamp}: ${::host}:${::port}: $args"
close $fid
}
# this works for me!
proc reset { args } {
log "calling 'cardctl reset'"
catch { exec cardctl reset }
# avoid banging on hardware too often
# if the reset fails.
sleep 60
}
# kill things that might be problems
proc kill { args } {
foreach program $::kills {
catch { exec pkill -9 $program }
}
}
init
while { 1 } {
if { [ catch {
set sid [ socket -async $::host $::port ]
sleep 1
} err ] } {
log $err
}
# peerinfo will be unavailable if socket open failed
if { [ catch { fconfigure $sid -peername } ] } {
kill
reset
} else {
close $sid
}
sleep 19
}