Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Easy Mac
Copyright (2015) Sean Beck
Licensed under Creative Commons Attribution-ShareAlike 4.0 International
Easily change your MAC address on Linux using `ifconfig`
import argparse
import os
import random
import subprocess
import sys
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('-d', type=str, help='The interface ID whose MAC you wish to change')
return parser.parse_args()
def make_mac():
Makes the MAC look like a VMware MAC address
mac = [
random.randint(0x00, 0x7f),
random.randint(0x00, 0xff),
random.randint(0x00, 0xff)
return ':'.join('{0:02X}'.format(n) for n in mac)
def set_mac(dev, mac):
cmd = 'sudo ip link set down dev {}'.format(dev)
cmd = 'sudo ip link set dev {} address {}'.format(dev, mac)
cmd = 'sudo ip link set up dev {}'.format(dev)
def get_root():
euid = os.geteuid()
if euid != 0:
print '[-] You didn\'t run as root!'
print '[-] Exiting...'
def main():
args = get_args()
if not args.d:
print '[-] Please specify a target device'
mac = make_mac()
print '[+] Generated MAC with value', mac
set_mac(args.d, mac)
print '[+] MAC is now changed'
if __name__ == '__main__':
Copy link

mytchel commented Jul 20, 2015

Pretty sure the #!/usr/bin/python2.7 needs to be at the begining of the file for it to work. Otherwise, cool.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment