Helo,
this is my first try with perl. I've programmed mainly in C-style
languages and lisp, and I know that my code isn't very "perl-like".
So, can somebody enlight me of a better way to right this ?
Thanks for your time,
Duarte
Problem:
Write a program that receives two arguments: a ip address and a integer
that I call COUNT.
Use external program ping to check for replies, making COUNT tries.
Return 6 numbers: send packets, received packets, lost packets, min,
average, and max roundtrip.
The output of ping is:
(for a successful ping):
[root@linux modules]# ping -w 5 -c 3 10.15.96.2
Warning: no SO_TIMESTAMP support, falling back to SIOCGSTAMP
PING 10.15.96.2 (10.15.96.2) from 10.15.96.1 : 56(84) bytes of data.
64 bytes from 10.15.96.2: icmp_seq=0 ttl=128 time=1.465 msec
64 bytes from 10.15.96.2: icmp_seq=1 ttl=128 time=168 usec
64 bytes from 10.15.96.2: icmp_seq=2 ttl=128 time=154 usec
--- 10.15.96.2 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max/mdev = 0.154/0.595/1.465/0.615 ms
[root@linux modules]#
(for a failure):
[root@linux modules]# ping -w 5 -c 3 10.15.96.5
Warning: no SO_TIMESTAMP support, falling back to SIOCGSTAMP
PING 10.15.96.5 (10.15.96.5) from 10.15.96.1 : 56(84) bytes of data.
>From 10.15.96.1: Destination Host Unreachable
--- 10.15.96.5 ping statistics ---
4 packets transmitted, 0 packets received, +1 errors, 100% packet loss
[root@linux modules]#
Now, what I've done....
#!/usr/bin/perl
# used for testing connectivity
# Arguments: IP to check, count
# results: 1 line with send receive lost min med max
print "ICMP Test to @ARGV[0], @ARGV[1] times.\n";
open(line, "ping -w 5 -c @{ARGV[1]} -q @{ARGV[0]} 2>/dev/null |")
|| die "can't fork: $!";
while (<line>) {
$linha=$_;
if(/transmitted/)
{
($trans,$t,$t, $received,$t,$t, $lost,$t,$tmp) = split (/ /,
$linha);
chop($lost);
chop($tmp);
if($lost == 0 && $lost ne "0")
{ $lost=$tmp;}
print "$trans $received $lost ";
}
if(/mdev/)
{
($t,$t,$t, $pac) = split (/ /, $linha);
($min, $med, $max) = split (/\//, $pac);
print "$min $med $max\n";
}
next ;
}
#strangely if the ping fails and I try to close line I get a error
on perl:
# [root@linux modules]# ./test_icmp.pl 10.15.96.5 3
# ICMP Test to 10.15.96.5, 3 times.
# bad ping: 256 at ./test_icmp.pl line 36.
# 4 0 100 [root@linux modules]#
if($received==@ARGV[1])
{
close line || die "bad ping: $! $?";
exit;
}
print "0 0 0\n";
--
Duarte Manuel Cordeiro
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]