import subprocess as sub
import argparse
import re

def get_args():
  parser = argparse.ArgumentParser()
  parser.add_arguments('-i', '--interface', dest = 'interface', help = 'Interface name whose MAC is to be changed')
  parser.add_arguments('-m', '--mac', dest = 'new_mac', help = 'New MAC Address')
  options = parser.parse_args()
  
  #Check for errors i.e if the user does not specify the interface or new MAC
  #Quit the program if any one of the argument is missing
  #While quitting also display an error message
  
  if not options.interface:
    #Code to handle if interface is not specified
    parser.error('[-] Please specify an interface in the arguments, use --help for more info.')
    
  elif not options.new_mac:
      #Code to handle if new MAC Address is not specified
      parser.error('[-] Please specify a new MAC Address, use --help for more info.')
      
  return options

def change_mac(interface, new_mac):
  
  #Cecking if the new MAC Address has a length of 17 or not. If not print an error and quit, else change the MAC Address
  if len(new_mac) != 17:
    print('[-] Please enter a valid MAC Address')
    quit()
  
  print('\n[+] Changing the MAC Address to', new_mac)
  sub.call(['sudo', 'ifconfig', interface, 'down'])
  sub.call(['sudo', 'ifconfig', interface, 'hw', 'ether', new_mac])
  sub.call(['sudo', 'ifconfig', interface, 'up'])
  
command_args = get_args()

change_mac(command_args.interface, command_args.new_mac)