View Single Post

  #2 (permalink)  
Old 03-10-2006, 02:24 AM
crb3 crb3 is offline
Junior Member
 
Join Date: Mar 2006
Posts: 2
Rep Power: 0
crb3
Default

Looks to me like you're going at it the long way around. Perl is supposed to be easier, and usually is.

First, you're using a system() call:

$ecmd="netstat -pant";
$return_value=system($ecmd);

When you do that, all of the called program's STDOUT traffic goes to stdout, and thus to the screen; what you get back in a scalar is the called program's exit-value, as shown above.
If you use the backtick operator instead:

$ecmd="netstat -pant";
$returned_lines=`$ecmd`; # notice: those are backticks, not ticks

You'll get all of the command's (stdout) output text in one long multiline string, without the bother of catching anything in a tempfile.
If you direct it into an array instead, forcing it into list-context with parentheses:

(@returned_line=`netstat -pant`;

You'll end up with an array of lines (each ending in \n -- chomp the lines to get rid of that) which you can process in a foreach loop:

foreach $line (@returned_line{
chomp $line;
next if $line =~ /^\s*$/; # skip empty lines
... # do needed checking and filtering
}

...which is probably where the meat of your script will be. In your sample, of course you'd just dump it out for inspection (I'm handing Dumper a reference to the array here to get the result shown as one complex variable):

$dumped=Dumper(\@returned_line;
print $dumped;

Also, I noticed that your sample code used the '%' prefix, not the '@' prefix, for your array, which would make it an associative array or hash.
A hash doesn't belong here: I don't think it will do what you want, plus it won't load up that way (you load one of those with 'key=value' pair.

fwiw
cr
Reply With Quote