0

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!

Captain Jack Sparrow
  • 971
  • 1
  • 11
  • 28
  • have a look here https://stackoverflow.com/questions/1854/what-os-am-i-running-on – balderman Aug 07 '20 at 21:22
  • Thanks. I will try and see if using this command instead of sub process fixes the issue. – yusufbashir Aug 08 '20 at 03:09
  • 1
    `check_uname` is going to have a newline at the end, making it not match either of your OS types - print the `repr()` of the variable to verify this. – jasonharper Aug 08 '20 at 03:19
  • Thanks. I changed my code to `check_uname = subprocess.check_output(['uname']) if repr(check_uname) == "d'Darwin\n'":` ive done this same thing for the linux system check too. it returns with a fail and gives me the else statement answer which is "Incompatible software." Seems that the problem was never that two statements are running. The real problem is that my code doesn't doesn't match the output of check_uname with "b'Darwin\n'" or "b'Linux\n'". I even checked if my system was Darwin or linux and it was Darwin. But this code is still not matching with either. Maybe I wrote it wrong? – yusufbashir Aug 08 '20 at 05:34
  • @yusufbashir There is an easier way to get the system platform than using `uname`. Try `import platform; platform.system()` – Captain Jack Sparrow Aug 08 '20 at 14:20
  • 1
    @Noah Broyles Yup I actually just tried that this morning and came here to announce that the issue is resolved but thanks anyways to everyone for all the help! – yusufbashir Aug 08 '20 at 14:37

0 Answers0