I made a MAC address changer for my personal use to convert my MAC address. I want to make this script work on every operating system because the ifconfig
command has different syntax depending on which kind of operating system it is being used on. To make this possible, I fit this code in my script:
def mac_change(user_interface, user_mac):
check_uname = subprocess.check_output(['uname'])
print(check_uname)
if str(check_uname) == 'Linux':
subprocess.call(['ifconfig', user_interface, 'down'])
subprocess.call(['ifconfig', user_interface, 'hw', 'ether', user_mac])
subprocess.call(['ifconfig', user_interface, 'up'])
elif str(check_uname) == 'Darwin':
subprocess.call(['ifconfig', user_interface, 'down'])
subprocess.call(['sudo', 'ifconfig', user_interface, 'ether', user_mac])
subprocess.call(['ifconfig', user_interface, 'up'])
else:
print('Incompatible software.')
This is the part of the code where the MAC address of the user-specified device is changed to what the user enters as an option. I intend to add more operating systems later on after I figure out the issue. Anyway, the issue is that when I run it, it seems that two conditionals run instead of only one. These are the results of a test I ran on my MacBook air, which would be Darwin
:
MAC changer started!
Changing MAC address to 00:11:22:33:44:55...
Darwin
ifconfig: down: permission denied
Password:
ifconfig: up: permission denied
Incompatible software.
MAC Address successfully changed! New MAC Address: 00:12:13:14:15:19
Why is it printing Incompatible software? It shouldn't. I commented out the whole else part and then it started running the elif
along with the if part. I commented out the elif
and ran the if and else parts and it started printing out Incompatible software again even though in the end, the MAC address did end up converting. But why is this happening? If anybody needs the full python file to understand and answer my question then I'll provide it. Also, if more information is needed, I'll provide it. Thanks!