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

Hi,
I'm trying to write a script that will put a network interface down/up using ifconfig so that the mac address can be changed. The script will prompt the user to choose a interface, i.e (etc0,etc1,etc...) then, ask if the new mac address will be random, or one chosen by the user.

The problem is that I get an error when trying to execute ifconfig from the system function. Because the interface is selected from user input, I write the system function as system("ifconfig $device down");. If I remove down, there is no error. If I remove the variable $device and put eth0, there is no error. If I set the variable $device to eth0 above the system function, there is no error. Here is part of the script

### Test if a device has been selected #### if ($device eq "") { print "Please Choose a device\n"; system ("ifconfig"); print "Choice: "; $device = <>; } else { ### Display Change Mac Address Menu ### print "1. Create Random new Mac Address for device $device\n"; print "2. Create a Specefic New Mac Address for device $device\n"; print "3. Go Back to MAIN MENU\n\n"; print "Choice: "; $choice = <>; if ($choice == "1") { ### Create Random New MacAddress ### system("ifconfig $device down"); system("macchanger -r $device"); system("ifconfig $device up"); }

When the system function is executed it will do a ifconfig eth0 (if eth0 is what the user choose), then on a new line say: sh: line 1: down command not found.
Any ideas as to what could be causing this error? and how I can fix it?
Thanks in advance.

Replies are listed 'Best First'.
Re: perl system function
by trizen (Hermit) on Mar 07, 2012 at 01:23 UTC
    There is a newline character at the end of the device name.
    This issue can be fixed by changing $device = <>; to chomp($device = <STDIN>);.

      That did the trick. Thank you very much for your help.