Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"system" command - redirecting output 5

Status
Not open for further replies.

Graeme06

Technical User
Jun 6, 2006
60
Hi:
I'm new to Perl and I'm using the command:
system('/usr/sbin/ping '.$servername)
where $servername is the name of what I'm trying to ping.

It works fine, but the output goes to the screen, and I want it to go to a a varialbe $result. I can put:
$result = system('/usr/sbin/ping '.$servername)
but that just gives the return code of ping and not the actual output.

Any ideas?

Thanks,
Graeme
 
In general though I am interested in this question as well. If I want to run a perl script, and in it run another batch script or whatever that outputs to standard out, is there any way to capture this data?
 
okay thanks but i ahve no idea what you mean. Where do I put in Net::ping?
 
there must be someway to pipe it. I tried system('/usr/sbin/ping '.$servername.' |')
and the response is no longer written to the screen, but it isn't really redirected anywhere. I don't know what I should stick after that pipe to get it redirected to a variable in my program, if thats even possible.
 
Code:
open FH, "/usr/sbin/ping $servername" or die "Failed to open pipeline";
while(<FH>) {
    print;
}

Trojan.
 
that just gives "Failed to open pipeline at servertest.pl line 4." line 4 being the line the open code is on
 
Code:
use strict;
use warnings;

my @ping = qx{ping [URL unfurl="true"]www.cpan.org};[/URL]

print join('', @ping);
Note: this works on Windows - on Linux ping just keeps going unless you add a parameter to limit the number of pings...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
that just gives "Failed to open pipeline at servertest.pl line 4." line 4 being the line the open code is on

TWB left out the pipe |:

Code:
open FH, "|/usr/sbin/ping $servername" or die "Failed to open pipeline";
while(<FH>) {
    print;
}

 
Kevin, You're right and wrong.
I did leave out the pipe character, but it neeeds to be at the other end!
Code:
open FH, "/usr/sbin/ping $servername |" or die "Failed to open pipeline";
while(<FH>) {
    print;
}

Sorry guys.


Trojan.
 
thanks for the responses, I now have a copule of ways to do this.
 
If it it going to be a simple command try this
Code:
my $result = `mycmd 2>&1`;
the 2>&1 will capture stderr and stdout and return it in $result. The backticks capture output where system() captures return code * 256

Null
 
it worked either way on my windows test server (pipe at beginning or end) but maybe on nix the pipe would have to be on the end to get the output.
 
I bet it didn't
I bet it just appeared to because when the pipe is at the wrong end, the output is not trapped so is sent to the terminal anyway.
If you change the print to read 'print "---> $_";' then I bet you'll see the difference.
Pipe at the start will not show any "--->" prefixes.

Give it a go and see what happens. ;)



Trojan.
 
OK....

this:

Code:
open FH, "ping thesitewizard.com|" or die "Failed to open pipeline";
while(<FH>) {
    print;
}

returns:

Code:
Pinging thesitewizard.com [69.5.9.180] with 32 bytes of data:

Reply from 69.5.9.180: bytes=32 time=854ms TTL=52
Reply from 69.5.9.180: bytes=32 time=984ms TTL=52
Reply from 69.5.9.180: bytes=32 time=1122ms TTL=52
Reply from 69.5.9.180: bytes=32 time=863ms TTL=52

Ping statistics for 69.5.9.180:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 854ms, Maximum =  1122ms, Average =  955ms

this:

Code:
open FH, "|ping thesitewizard.com" or die "Failed to open pipeline";
while(<FH>) {
    print;
}

returns:

Code:
Pinging thesitewizard.com [69.5.9.180] with 32 bytes of data:



Reply from 69.5.9.180: bytes=32 time=1133ms TTL=52

Reply from 69.5.9.180: bytes=32 time=773ms TTL=52

Reply from 69.5.9.180: bytes=32 time=1086ms TTL=52

Reply from 69.5.9.180: bytes=32 time=865ms TTL=52



Ping statistics for 69.5.9.180:

    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),

Approximate round trip times in milli-seconds:

    Minimum = 773ms, Maximum =  1133ms, Average =  964ms

adding $_ to the print line returns the same thing for both sets of code.
 
What's the fascination with pipes here guys? It's not as if ping is likely to suddenly produce 40MB of output or anything.
Code:
print for qx{ping www.cpan.org};
will achieve the same effect. [shadeshappy]

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
KevinADC,

I think you missed my point. If you had changed the print statements as I suggested, you might have noticed that in one case perl is processing the output and in the other it is not.

stevexff,

I don't wish to detract from your points at all but I would say that there is another issue here that is worth explaining.
Just because output appears on the console, doesn't necessarily mean that perl is generating it. This is a good opportunity to demonstrate that issue and explain it.



Trojan.
 
I get what you're saying. That confused me at first too, how it automatically sends information to the console even though Perl never sees it. The pipeline at the beginning makes no sense.
 
I get your point TWB, and it's good point too. You're right as rain. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top