nixCraft Linux Forum

nixCraft

Linux Tech Support Forum

Perl programming problem -- Redirecting STDOUT

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 ...


Go Back   nixCraft Linux Forum > Linux Getting Started > Linux software

Register FAQ Members List Calendar Forgotten your password? Mark Forums Read
  #1 (permalink)  
Old 02-17-2006, 09:04 AM
Junior Member
User
 
Join Date: Jan 2005
Posts: 19
Rep Power: 0
sonaikar
Default Perl programming problem -- Redirecting STDOUT

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:umper;

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
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-10-2006, 02:24 AM
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
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads

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


All times are GMT +5.5. The time now is 01:16 AM.


Powered by vBulletin® Version 3.7.4 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36