Page 1 of 1

Writing a short "C" script to change mac address

Posted: Sat Nov 16, 2013 1:12 pm
by jessie
To connect my linux machine to the internet in my accommodation I have to change its mac address. Which means using roughly the following:

[-> sudo -s]

system("ifconfig eth0 down")
system("ifconfig eth0 hw ether XX:XX:XX:XX:XX:XX")
system("ifconfig eth0 up")

Which only seems to work if I run each one via a separate program :? Otherwise it registers the mac as changing but still no connection. So, I have to root via sudo -s and then run 3 programs after logging in which I call via:

./down
./ether
./up

Can this all be done via one program and/or could this all be automated to run at start up?

Re: Writing a short "C" script to change mac address

Posted: Sat Nov 16, 2013 1:52 pm
by cwsnyder
The default MAC address is built-in to your Network Interface in your machine. My suggestion would be to remove/replace/disable your existing Network Interface with an expansion card Network Interface Card. What you are doing can't be made permanent. If you wish, you could execute as a shell script as follows:

Code: Select all

#!/bin/bash
wait(sudo ifconfig eth0 down)
wait (sudo ifconfig eth0 hw ether XX:XX:XX:XX:XX:XX)
wait(sudo ifconfig eth0)
In this case compiling a C executable would actually be counter-productive. Once you make the shell script, and make it executable with a chmod -x <scriptname>, where <scriptname> is the name for your script, you can add it to your start-up applications, but include the full path when you execute as a start-up, as in /home/<user>/<scriptname> .

Re: Writing a short "C" script to change mac address

Posted: Sun Nov 17, 2013 4:29 am
by xenopeek
cwsnyder's suggestion will ask for your password, as ifconfig needs root permissions. So probably better to run this as root.

I suggest you instead make a script like this:

Code: Select all

#!/bin/bash

# redirect stdout and stderr to logfile
exec >>/var/log/mac-address-changer.log 2>&1

# change mac address
ifconfig eth0 down
ifconfig eth0 hw ether XX:XX:XX:XX:XX:XX
ifconfig eth0 up
Make it executable as per cwsnyder's example and then edit root's cron to run this file at startup. To edit root's cron run:

Code: Select all

sudo crontab -e
If you have no default editor, you will be asked which you want to use. Pick nano from the list. At the end of the file add a line like:

Code: Select all

@reboot /path/to/your/script
And replace /path/to/your/script with the full path to your script. This will run the script as root on start up, not needing you to enter your password. I've added a command at the top to log output to a file, which you should always do for scripts running through cron--so you can look at the log file if it doesn't work and see where it went wrong. Log file is /var/log/mac-address-changer.log in my example.

Alternatively, you could put your three commands in /etc/rc.local before the final `exit 0`.