ping nos permite conocer los tiempos de respuesta.
La opción -c 1 limita el número de paquetes de prueba a uno.
Veamos un ejemplo de uso del comando ping:
$ ping -c 1 instituto PING instituto (193.145.130.147): 56 data bytes 64 bytes from 193.145.130.147: icmp_seq=0 ttl=63 time=0.7 ms --- instituto ping statistics --- 1 packets transmitted, 1 packets received, 0% packet loss round-trip min/avg/max = 0.7/0.7/0.7 msEn este otro ejemplo vemos como es una respuesta negativa:
$ ping -c 1 thatmachine PING thatmachine (193.145.125.192): 56 data bytes --- thatmachine ping statistics --- 1 packets transmitted, 0 packets received, 100% packet lossSupongamos que pueda ocurrir que el comando que estamos lanzando, en este caso
ping, en ocasiones no termine. En el siguiente ejemplo
se muestra como limitar
el tiempo de espera por ping y eliminar la jerarquía de procesos
creada.
lhp@nereida:~/Lperl/src$ cat -n myping2.pl
1 #!/usr/bin/perl -w
2 use strict;
3
4 my @machines = @ARGV;
5 my ($m, $code) = ("", "");
6
7 local $SIG{ALRM} = sub { die "too long"; };
8
9 for $m (@machines) {
10 eval {
11 alarm(1);
12 $code = `ping -c 1 $m`;
13 alarm(0);
14 };
15 if (defined($@) and ($@ =~ /too long/)) {
16 print "El acceso a $m toma demasiado tiempo.\n";
17 system('ps -fA | grep ping');
18
19 print "*************************\n";
20 local $SIG{HUP} = 'IGNORE';
21 kill 'HUP', -$$;
22 system('ps -fA | grep ping');
23 }
24 else {
25 print "From $m:\ncode = $code\n\n";
26 }
27 }
Al ejecutar el programa obtenemos una salida como esta:
lhp@nereida:~/Lperl/src$ ./myping2.pl www.google.com beowulf www.yahoo.com
From www.google.com:
code = PING www.l.google.com (209.85.129.104): 56 data bytes
64 bytes from 209.85.129.104: icmp_seq=0 ttl=240 time=73.0 ms
--- www.l.google.com ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 73.0/73.0/73.0 ms
El acceso a beowulf toma demasiado tiempo.
lhp 2430 8353 0 15:32 pts/14 00:00:00 vi myping2.pl
lhp 5513 31559 1 16:07 pts/15 00:00:00 /usr/bin/perl -w ./myping2.pl \
www.google.com beowulf www.yahoo.com
lhp 5515 5513 0 16:07 pts/15 00:00:00 ping -c 1 beowulf
lhp 5516 5513 0 16:07 pts/15 00:00:00 sh -c ps -fA | grep ping
lhp 5518 5516 0 16:07 pts/15 00:00:00 grep ping
*************************
lhp 2430 8353 0 15:32 pts/14 00:00:00 vi myping2.pl
lhp 5513 31559 1 16:07 pts/15 00:00:00 /usr/bin/perl -w ./myping2.pl \
www.google.com beowulf www.yahoo.com
lhp 5515 5513 0 16:07 pts/15 00:00:00 [ping] <defunct>
lhp 5519 5513 0 16:07 pts/15 00:00:00 sh -c ps -fA | grep ping
lhp 5521 5519 0 16:07 pts/15 00:00:00 grep ping
From www.yahoo.com:
code = PING www.yahoo-ht3.akadns.net (69.147.114.210): 56 data bytes
64 bytes from 69.147.114.210: icmp_seq=0 ttl=46 time=148.3 ms
--- www.yahoo-ht3.akadns.net ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 148.3/148.3/148.3 ms
Casiano Rodríguez León
