Python Forum

Full Version: MAC Changer Script
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
root@:~# python
Python 2.7.15+ (default, Aug 31 2018, 11:56:52)
[GCC 8.2.0] on linux2

Linux 4.18.0-kali2-amd64 #1 SMP Debian 4.18.10-2kali1 (2018-10-09) x86_64 GNU/Linux
#!/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 its MAC address")
    parser.add_option("-m", "--mac", dest="new_mac", help="New MAC address")
    return parser.parse_args()


def change_mac(interface, new_mac):
    print("[+] Changing MAC address for " + interface + " to " + new_mac)
    subprocess.call(["ifconfig", interface, "down"])
    subprocess.call(["ifconfig", interface, "hw", "ether", new_mac])
    subprocess.call(["ifconfig", interface, "up"])


(options, arguments) = get_arguments()
change_mac(options.interface, options.new_mac)
Output:
Traceback (most recent call last): File "/root/PycharmProjects/mac_changer/mac_changer.py", line 22, in <module> change_mac(options.interface, options.new_mac) File "/root/PycharmProjects/mac_changer/mac_changer.py", line 15, in change_mac print("[+] Changing MAC address for " + interface + " to " + new_mac) TypeError: can only concatenate str (not "NoneType") to str Process finished with exit code 1
Please use python tags when posting code. I put them in for you this time. See the BBCode link in my signature below for instructions. Actually asking a question would also be nice.

It would appear that options.interface and/or options.new_mac are None. You can check which one it is by printing them both on the line before the error. It seems there is a problem either with the arguments you provided or with the parsing of them. Note that the optparse module is deprecated in favor of argparse.
Thank ypu very much Craig.