I often have to snag the output of system commands and parse them with perl. Well, Urbana turned me on to a method of grabbing the output that is much easier than directing the output of the system command to a file and re-opening the file. Just use a pipe:
#!/usr/bin/perl open (PINGTEST, "/bin/ping -c 5 netadmintools.com |"); $i=1; while (<PINGTEST>){ print "Line # ".$i." ".$_; $i++; } print "All done!\n"; close PINGTEST; |
When this is run:
u-1@srv-1 perlmagic $ ./pt Line # 1 PING netadmintools.com (216.40.230.46): 56 octets data Line # 2 64 octets from 216.40.230.46: icmp_seq=0 ttl=49 time=78.0 ms Line # 3 64 octets from 216.40.230.46: icmp_seq=1 ttl=49 time=91.7 ms Line # 4 64 octets from 216.40.230.46: icmp_seq=2 ttl=49 time=77.1 ms Line # 5 64 octets from 216.40.230.46: icmp_seq=3 ttl=49 time=93.6 ms Line # 6 64 octets from 216.40.230.46: icmp_seq=4 ttl=49 time=81.7 ms Line # 7 Line # 8 --- netadmintools.com ping statistics --- Line # 9 5 packets transmitted, 5 packets received, 0% packet loss Line # 10 round-trip min/avg/max = 77.1/84.4/93.6 ms All done! u-1@srv-1 perlmagic $ |