Skip to content

Instantly share code, notes, and snippets.

@believeohiozua
Last active December 27, 2022 23:06
Show Gist options
  • Save believeohiozua/9e031b96115684a25bdfa659b648d741 to your computer and use it in GitHub Desktop.
Save believeohiozua/9e031b96115684a25bdfa659b648d741 to your computer and use it in GitHub Desktop.
How to change a linux mac address with Python
#!/usr/bin/env python3
import subprocess
import optparse
'''
Readme.md
To run this application
python3 main.py -i etho -m 00:11:22:33:44:55
NOTE:
The value of the mac_address (m) must be 12 digits with each two separated by : as seen above
The value of the interface must be eth0 (if you wish to change only the mac address of your linux
Credits : https://www.udemy.com/course/learn-python-and-ethical-hacking-from-scratch/
'''
def get_args():
parser = optparse.OptionParser()
parser.add_option('-i', '--interface', dest='interface', help='Interface to change the mack address')
parser.add_option('-m', '--mac', dest='new_mac', help='New mac address')
(options, arguments) = parser.parse_args()
if not options.interface:
parser.error('[-] please specify an interface, use --help for more info.')
elif not options.new_mac:
parser.error('[-] please specify a new mac address, use --help for more info.')
return options
def change_mac(interface, new_mac):
print('[+] Changing MAC Address for ' + interface + ' to ' + new_mac)
subprocess.call(['ifconfig', interface, 'down'])
subprocess.call(['ifconfig', interface, 'hw', 'ether', new_mac])
subprocess.call(['ifconfig', interface, 'up'])
options = get_args()
change_mac(options.interface, options.new_mac)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment