This is a discussion on Perl programming problem -- Redirecting STDOUT within the Linux software forums, part of the Linux Getting Started category; Dear Vivek sir, I am writing a program to remotely run "netstat -pant" using perl. I have written all the ...
|
|||||||
| Register | FAQ | Members List | Calendar | Forgotten your password? | Mark Forums Read |
|
|||
|
Dear Vivek sir,
I am writing a program to remotely run "netstat -pant" using perl. I have written all the necessary stuff to ssh on the remote machine. I have problem the command "my %list = system(netstat -pant) " works. It redirects the output by default to STDOUT and not to the hash as explained below. I tried to redirect default STDOUT to a tempfile but again problem is when I want to print output to screen (STDOUT) it will not. The program for netstat is as below, copy it and run so you can understand problem correctly. ************************************** !/usr/bin/perl use strict; use Data: open STDOUT,">tmpfile" or die "Cannot open temp file"; my $ecmd = qq{netstat -pant}; my %ary=system($ecmd); close STDOUT; print Dumper %ary; *************************************** You will find the file tmpfile having all the output of netstat command. Do the needful. With regards, Sameer |
| Sponsored Links | ||
|
|
|
|||
|
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 |
![]() |
| Bookmarks |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) |
|
| Thread Tools | |
| Display Modes | |
|
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Client-Server Programming in TCP | sureshvaikundam | Networking, Firewalls and Security | 0 | 04-08-2008 06:57 PM |
| perl scripting | pansarevai | Coding in General | 2 | 03-17-2008 06:34 PM |
| Simple Xen API programming | unixfoo | XEN | 0 | 12-28-2007 12:00 PM |
| Redirecting question | sonaikar | Linux software | 1 | 06-25-2005 01:18 AM |
| kenrel Programming -- books/ references | sonaikar | Linux software | 2 | 06-16-2005 08:06 PM |