Saturday, May 29, 2010

Execute an External Command

In Perl, to execute an external command and capture its output can be done in one line.

#!/usr/bin/perl

# Use backticks (``) operator 
@output = `tasklist.exe`;

# Display the output
foreach $line (@output) {
    print "$line";
}
Output:

D:\>exec_cmd.pl

Image Name        PID Session Name  Session# Mem Usage
================= === ============= ======== ==========
System              4 Services             0   32,236 K
smss.exe          332 Services             0      760 K
csrss.exe         436 Services             0    1,728 K
wininit.exe       476 Services             0    2,020 K
csrss.exe         488 Console              1    8,268 K
You can use Perl to analysis the output and send an email alert if any error detected.

No comments:

Post a Comment