#!/bin/sh # the next line restarts using wish \ exec wish "$0" "$@" # tkfping - checks status of hosts in the network # Version 0.1, 2012-03-20 # # Copyright (C) 2012 by Tom Sato # # http://homepage3.nifty.com/tsato/ # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # See the GNU General Public License for more details. set fping "fping -r 1" set interval 5000 set ring_bell 1 set color_unknown "gray" set color_alive "#ccffcc" set color_dead "orange" set label_font "-*-lucida-medium-r-normal-*-8-*-*-*-*-*-*-*" set program_name "tkfping" set title "tkfping (Version 0.1)" wm overrideredirect . 1 wm title . $title wm iconname . $program_name frame .top -borderwidth 0 pack .top if { [ catch { exec which [ lindex $fping 0 ] } ] } { puts "tkfping: can't find fping command - using ping instead" set fping "/bin/ping -c 1 -W 1 -v -q" } if { $argc == 0 } { puts "usage: tkfping [ -geometry ] hostname1 hostname2 ... " lappend hosts "localhost" } else { switch -regexp -- $fping { /bin/ping { lappend hosts [ lindex $argv 0 ] } default { foreach host $argv { switch -regexp -- $host { "^-i[0-9]*" { set interval [ regsub {^-i} $host {} ] } "^-b" { set ring_bell 0 } "^-" { puts "Bad option: $host" } default { lappend hosts $host } } } } } } proc sanitize host { return [ regsub -all {[^a-zA-Z0-9]} $host {_} ] } proc refresh_display {} { global fping interval ring_bell color_unknown color_alive color_dead global hosts alive foreach host $hosts { set key [ sanitize $host ] .top.$key configure -bg $color_unknown } set cmd "$fping $hosts" catch { eval exec $cmd } res set lines [ split $res "\n" ] foreach s $lines { set f [ split $s ] set key [ sanitize [ lindex $f 0 ] ] switch -regexp -- $fping { "/bin/ping" { set key [ sanitize [ lindex $hosts 0 ] ] } } switch -regexp -- $s { {alive|1 received} { .top.$key configure -bg $color_alive if { $ring_bell == 1 } { if { $alive($key) == 0 } { bell } set alive($key) 1 } } {unreachable|0 received} { .top.$key configure -bg $color_dead if { $ring_bell == 1 } { if { $alive($key) != 0 } { bell } set alive($key) 0 } } } } after $interval { refresh_display } } foreach host $hosts { set key [ sanitize $host ] label .top.$key -text $host -font $label_font -bg gray -fg black pack .top.$key -expand true -fill both -side top set alive($key) -1 } refresh_display