Listing 1. The ping_gateway.pl program.
1 #!/usr/bin/perl -w
2
3 use Gnome;
4
5 init Gnome::Panel::AppletWidget 'ping_gateway.pl';
6 $a = new Gnome::Panel::AppletWidget 'ping_gateway.pl';
7
8 $off_label = "Check\nGateway\n<click>";
9
10 Gtk->timeout_add( 20000, \&check_gateway );
11 $b = new Gtk::ToggleButton("$off_label");
12 $b->signal_connect ( 'clicked', \&reset_state );
13
14 $b->set_usize(50, 50);
15 show $b;
16 $a->add($b);
17 show $a;
18
19 &fetch_gateway;
20
21 gtk_main Gnome::Panel::AppletWidget;
22
23 sub fetch_gateway {
24
25 foreach $line (`netstat -r`) {
26
27 my ($dest, $gate, $other) = split(' ', $line, 3);
28 $hostname = $gate if $dest eq "default";
29 $hostname = lc($hostname);
30
31 }
32
33 }
34
35 sub reset_state {
36
37 $state = ( $b->get_active() );
38 if (!$state) { $b->child->set("$off_label") }
39 else { $b->child->set("Wait...") }
40
41 }
42
43 sub check_gateway {
44
45 my $uphost;
46
47 if (length ($hostname) > 8) {
48 $uphost = "gateway";
49 } else {
50 $uphost = $hostname;
51 }
52
53
54 if ($state) {
55
56 my $result = system("/bin/ping -c 1 2>&1>
/dev/null $hostname");
57
58 if ($result) {
59 $b->child->set( "$hostname:\nNo\nResponse" )
60 } else {
61 $b->child->set( "$uphost\nis\nalive" );
62 }
63
64 }
65
66 return 1;
67
68 }