NetAdminTools.com
 
Categories:
GNU/Linux | Homebrew designs | Perl | Ruby | Administration | Backup/Recovery | Bugs/Fixes | Certification | Database | Email | File/Print | Hardware | Information Grab Bag | Interoperability | GNU/Linux ABCs | Monitoring | Name Resolution | Network Services | Networking | Remote Control | Security | Desktop | Web | BSD | Solaris | GIAGD | ERP | REALbasic

Last 30 Days | Last 60 Days | Last 90 Days | All Articles | RSS


Categories:
·GNU/Linux
·Homebrew designs
·Perl
·Ruby
·Administration
·Backup/Recovery
·Bugs/Fixes
·Certification
·Database
·Email
·File/Print
·Hardware
·Information Grab Bag
·Interoperability
·GNU/Linux ABCs
·Monitoring
·Name Resolution
·Network Services
·Networking
·Remote Control
·Security
·Desktop
·Web
·BSD
·Solaris
·GIAGD
·ERP
·REALbasic
·All Categories


Grabbing output of system commands with perl
Topic:Perl   Date: 2003-05-30
Printer Friendly: Print   Mobile View: mobile

spacerspacer
<<  <   >  >>

Subject

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 $


People:
Places:
Things:
Times:





Please read our Terms of Use and our Privacy Policy
Microsoft, Windows, Windows Server are either trademarks or registered trademarks of Microsoft Corporation. NetAdminTools.com is not affiliated with Microsoft Corporation. Linux is a registered trademark of Linus Torvalds, and refers to the Linux kernel. The operating system of most distributions that contain the Linux kernel is GNU/Linux. All logos and trademarks in this site are property of their respective owner. Copyright 1997-2013 NetAdminTools.com