Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Ifconfig error capture

by WalkingZero (Sexton)
on Mar 12, 2008 at 21:19 UTC ( #673829=perlquestion: print w/ replies, xml ) Need Help??
WalkingZero has asked for the wisdom of the Perl Monks concerning the following question:

Hello all. I am having difficulty with a spot of code. I have a feeling the answer is simple, and i am being stupid for missing it. In the code below I am running a call to ifconfig to change the mac address of the adapter to one in the given variable. I am trying to check for the error code "SIOCSIFHWADDR: Invalid Argument" When i run the backquoted command though, it does not capture the error code when it comes up. It always returns an empty set. I'd appreciate some help in understanding why this is and what I could do to get around it. I've included the relevant snippet of code below.
$code=`ifconfig eth0 hw ether $mac`; print "Returned error: $code \n"; if $code="SIOCSIFHWADDR: Invalid Argument"{&CMac;}
The conditional statement includes a recursive call to the function it is a part of. The idea is that it keeps attempting to change the MAC address until it hits a valid one. Unfortunately the $code variable always come up empty.


"One future, two choices. Oppose them or let them destroy us" - Propaghandi

Comment on Ifconfig error capture
Download Code
Re: Ifconfig error capture
by kyle (Abbot) on Mar 12, 2008 at 21:27 UTC

    The message you're looking for comes out on STDERR, but the backticks only capture what comes out on STDOUT. What you can do is have the shell redirect the error stream to the normal output stream like so:

    $code = `ifconfig eth0 hw ether $mac 2>&1`;
      Oy! I knew it was something obvious. I actually had to redirect STDERR and also send STDOUT to /dev/null to properly capture the data, but once that was done i just turned my conditional slightly. using an unless statement to check if the $code was null or not. Thank you very much for helping me get past my own idiocy.

      "One future, two choices. Oppose them or let them destroy us" - Propaghandi

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2014-09-18 00:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (101 votes), past polls