Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

System() or the like

by PyroX (Pilgrim)
on Sep 18, 2001 at 02:06 UTC ( #112985=perlquestion: print w/replies, xml ) Need Help??

PyroX has asked for the wisdom of the Perl Monks concerning the following question:

Is there ANY solution to get the text output from system()?

For example:
$run="snmpwalk $host $opt_c ifType.".$thisport; $type=system($run); print "$MAC $type $ifName $vlan $host\n";
As you can see, I want to return the result of an snmp walk, IN A VARIABLE. It obviously returns the results instantly, but I need it in a var, so I can work with it.

Edit kudra, 2001-09-18 Added code tags

Replies are listed 'Best First'.
Re: System() or the like
by blakem (Monsignor) on Sep 18, 2001 at 02:10 UTC
    You can use the backticks (like the apostrophe, but slanted the other way... usually found next to the !/1 key) to capture the output of another process....
    $run="snmpwalk $host $opt_c ifType.".$thisport; $type=`$run`; print "$MAC $type $ifName $vlan $host\n";

    -Blake

Re: System() or the like (code)
by deprecated (Priest) on Sep 18, 2001 at 02:48 UTC
    Um, I am surprised nobody has mentioned qx//. I tend to prefer qx{ foo } over backticks because it is much easier to see at high resolution.

    Also, several shell programming books I have read say that the use of backticks is, er, deprecated. They prefer to use $(), which qx{} somewhat resembles.

    Just my too sense.

    brother dep

    --
    Laziness, Impatience, Hubris, and Generosity.

      I've not heard that Perl's backticks are deprecated. The $() syntax is a ksh feature, which by the way nests very easily, and is not available in sh. I don't agree that $() looks like qx{} but regardless agree that it's a more elegant solution.
Re: System() or the like
by geektron (Curate) on Sep 18, 2001 at 03:17 UTC
    If you're using SNMP.pm, there's already a method for doing this:

    From the cpan documentation:

    get_table() - retrieve a table from the remote agent Blocking $response = $session->get_table($oid); Non-blocking $ok = $session->get_table( -baseoid => $oid, [-callback => sub {},] [-delay => $seconds] );
    it might be best to use a module here rather than depending on system commands and/ or backticks.

    i do a fair to large amount of SNMP work at work, and I use SNMP.pm for most, if not all, apps that need this kind of info.

      Just to avoid confusion, I think you mean Net::SNMP.
Re: System() or the like
by maverick (Curate) on Sep 18, 2001 at 02:13 UTC
    Try opening the command to a pipe.
    open(PIPE,"$run |") || die "pipe open failed: $!"; while(<PIPE>) { print $_; } close(PIPE);

    /\/\averick
    perl -l -e "eval pack('h*','072796e6470272f2c5f2c5166756279636b672');"

Re: System() or the like
by VSarkiss (Monsignor) on Sep 18, 2001 at 02:12 UTC

    Use backticks:

    $run="snmpwalk $host $opt_c ifType.".$thisport; $type=`$run`; print "$MAC $type $ifName $vlan $host\n";

    More information at perlfunc:system; in particular, the second paragraph.

    HTH

Re: System() or the like
by howie (Sexton) on Sep 18, 2001 at 03:13 UTC
    either $type=`$run`; (those are backticks, not quotes) or
    open(OUTPUT,"$run|"); $type=<OUTPUT>; close(OUTPUT);
    should do it. Both are in the 'Social Perl' chapter of the camel book, IIRC.
Re: System() or the like
by cider (Acolyte) on Sep 18, 2001 at 12:57 UTC

    Yes.

    I present thee with hoho_system(), it can be called with the silent option below to not additionally display output to stdout:

  • $type = hoho_system("$run", "silent");

    Or, it can also be noisy, and may be executed like so:

  • $type = hoho_system("$run");

    And for bonus points, I have included support for an optional global $logfile variable, which can be declared as the following optionally:

  • $logfile = "some_file_that_exists";

    # on with the show, cider@compulsion.org

    sub hoho_system { my ($cmd, $option) = @_; my ($returned_result); my $silent = 0; if ( defined($option) ) { if ( $option =~ m/silent/ ) { $silent = 1 +; } } if ( defined($logfile) ) { open LOG, "+>> $logfile"; print LOG "$cm +d: $_\n"; } open CMD, "$cmd |" or die "failure running $cmd: $!\n"; while(<CMD>) { print unless $silent; print LOG "$_"; $returned_result = "$_\n$returned_result"; } close CMD; if ( defined($logfile) ) { close LOG; } return $returned_result; }

    Using arrays instead of scalars in multi-line responses would be advisable, and for such you may change the $returned_result references to @returned_result, and change the $returned_result assignment line to push @returned_result, "$_";

    busunsl: switched pre tag to code tag. htmlism.

      Sorry to spoil that, but I think if you present a solution, it should work:

      running it gives me:

      syntax error at C:\WINNT\Profiles\int5398\src\test\xx.pl line 9, near +"if $option " syntax error at C:\WINNT\Profiles\int5398\src\test\xx.pl line 19, near + "}"
      The line while () { starts an endless loop.

      $returned_result will be empty.

      And even if it would contain something, it would be in reverse order.

      Please test your code before posting.

DONE: System() or the like
by PyroX (Pilgrim) on Sep 18, 2001 at 20:27 UTC
    Thanks guys,
    It looks like this will work the best for me, thank you everyone.
    $run="snmpwalk $host $opt_c ifType.".$thisport; open(PIPE,"$run |") || die "Failed to get type, stopped."; while(<PIPE>) { $type=$_; } close(PIPE); print "$MAC $type $ifName $iftomodule{$ifIndex} $iftopor +t{$ifIndex} $ifIndex $bridgeport $vlan $host\n";

    Edit kudra, 2001-09-19 Added code tags

      Thank you soooooo much this help a ton! $run="dir.".$thisport; open(PIPE,"$run |") || die "Failed to get type, stopped."; while(<PIPE>) { $type=$_; print($type); } close(PIPE);

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://112985]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2023-10-02 08:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?