Reputation: 3
Following a course on Udemy, I've been trying to make a program that automatically changes the mac address of an interface in Linux using the ifconfig command, with the subprocess and optparse modules.
My question is about my elif statement in the get_arguments() function below. I wanted it so that if the program is run on the command line without specifying arguments, then the user will be asked for input for the interface and new_mac variables.
Somehow with the get_arguments() function written below,
elif not options.interface:
parser.error("[-] Please specify an interface. Use -- help for more info.")
will be executed, printing the text and stopping the program with parser.error(), without even asking for input, if there are no arguments specified when running the program on the command line.
But, writing it this way,
if options.interface or options.new_mac:
if not options.interface:
parser.error("[-] Please specify an interface. Use -- help for more info.")
if not options.new_mac:
parser.error("[-] Please specify a new MAC address. Use --help for more info.")
else:
return options
The program would stop to get input and all would be well.
Here is the program:
#!/usr/bin/env python
import subprocess
import optparse
def get_arguments():
parser = optparse.OptionParser()
parser.add_option("-i", "--interface", dest="interface", help="Interface to change MAC address")
parser.add_option("-m", "--mac", dest="new_mac", help="New MAC address")
(options, arguments) = parser.parse_args()
if not options.interface and options.new_mac:
options = False
return options
elif 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.")
else:
return options
def change_mac(interface, new_mac):
print("[+] Changing MAC address for '" + interface + "' to '" + new_mac + "'")
subprocess.call(["sudo", "ifconfig", interface, "down"])
subprocess.call(["sudo", "ifconfig", interface, "hw", "ether", new_mac])
subprocess.call(["sudo", "ifconfig", interface, "up"])
subprocess.call(["sudo", "ifconfig", interface])
print("[+] Done!")
options = get_arguments()
if not options:
interface = raw_input("Specify interface > ")
new_mac = raw_input("Specify new MAC address > ")
change_mac(interface, new_mac)
else:
change_mac(options.interface, options.new_mac)
Upvotes: 0
Views: 148
Reputation: 532208
Just check if a value was given for each option immediately after parsing, and prompt the user for a value at that time. There's no need for an elif
; just handle each one independently.
Also, don't use optparse
; use argparse
instead.
#!/usr/bin/env python
import subprocess
import argparse
def get_arguments():
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--interface", dest="interface", help="Interface to change MAC address")
parser.add_argument("-m", "--mac", dest="new_mac", help="New MAC address")
args = parser.parse_args()
if args.interface is None:
args.interface = raw_input("Specify interface > ")
if args.new_mac is None:
args.new_mac = raw_input("Specify new MAC address > ")
return args
def change_mac(interface, new_mac):
print("[+] Changing MAC address for '" + interface + "' to '" + new_mac + "'")
subprocess.call(["sudo", "ifconfig", interface, "down"])
subprocess.call(["sudo", "ifconfig", interface, "hw", "ether", new_mac])
subprocess.call(["sudo", "ifconfig", interface, "up"])
subprocess.call(["sudo", "ifconfig", interface])
print("[+] Done!")
args = get_arguments()
change_mac(args.interface, args.new_mac)
I also strongly suggest that, as a beginner, you switch to Python 3.
Upvotes: 0