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

$cmd ="snmpset -v3 -a MD5 -A auth_Key_1492514201 -x DES -X encr_Key_10 +0265 -u myUser252094357 -l authPriv 172.16.232.53:8500 1.3.6.1.4.1.89 +62.2.1.2.1.10.7.1.20.2 i 2"; $x= system "$cmd"; or $x =`$cmd`;
i get the output on the screen but not in the variable Can anyone help me with this.... Thanks

Replies are listed 'Best First'.
Re: getting the output of command in variable
by graff (Chancellor) on Feb 01, 2007 at 13:49 UTC
    Depending on your OS (update: or depending on the shell that your perl uses to run a backtick command), this might work:
    $cmd = "snmpset ... all those options ... 2>&1"; $x = `$cmd`;
    I'm guessing that the reason your initial backtick attempt didn't work as expected is that the snmpset command is printing its output to STDERR rather than to STDOUT. The backtick operator only captures STDOUT from the process being run, but if the process includes a redirection of STDERR to STDOUT, you should get what you want.

    BTW, you should not have a space inside the  <code> tag; note that  <c> ... </c> works too.

Re: getting the output of command in variable
by grinder (Bishop) on Feb 01, 2007 at 13:52 UTC

    If you are still receiving output on the screen when you run $x = `$cmd` it may be the output from STDERR that you are seeing.

    If you want to snag both STDOUT and STDERR and treat them separately, you should look at something like IPC::Run, which you give you sufficient control over the process being run to do such a thing.

    • another intruder with the mooring in the heart of the Perl

Re: getting the output of command in variable
by roboticus (Chancellor) on Feb 01, 2007 at 13:41 UTC
    phemal

    Use the $x = `cmd`; form to get the results of the command into the variable.

    The $x = system "$cmd"; form executes the command in $cmd, and puts the result code into $x.

    Also, don't put a space inside your code tag--it's difficult to read your post.

    --roboticus Update: Clarified second statement.

Re: getting the output of command in variable
by rkrieger (Friar) on Feb 01, 2007 at 14:13 UTC

    You may want to review your use of system() and check the manual (perlfunc) for that command. It tells you that system() is not for capturing the output of the command performed.

    You're probably looking for backticks or the qx{} operator. These (usually) interpolate, so you can use variables in the command invocation. See perlop for more information. It also contains pointers on redirecting the STDOUT and STDERR for your command.

    The code below printed "2007-02-01 15:14:19 CET" on my console.

    #!/usr/bin/perl use strict; use warnings; my $date_cmd = '/bin/date'; my $date_format = q{ "+%Y-%m-%d %H:%M:%S %Z" }; # See date(1), str +ftime(3) chomp(my $date = qx{ ${date_cmd} ${date_format} } ); print "${date}\n";

    Update: Pretty-printed code; added reference to output redirection (perlop manual); fixed tense in text.

Re: getting the output of command in variable
by zentara (Cardinal) on Feb 01, 2007 at 17:27 UTC
    IPC::Open3 is another alternative to capturing both the stdout and stderr, goggle for many previously posted examples. You might be running into one of those peculiar programs that demand to output to a /dev/tty for stdout. In that case you may need to use IO::Pty, or Expect.

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum