简体   繁体   中英

TypeError: can only concatenate str (not "NoneType") to str, working with optparse module

import subprocess
import optparse

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"])


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")

(options, arguments) = parser.parse_args()
change_mac(options.interface, options.new_mac)

this is the error

Traceback (most recent call last):
  File "main.py", line 19, in <module>
    change_mac(options.interface, options.new_mac)
  File "main.py", line 7, in change_mac
    print("[+] Changing MAC address for " + interface + " to " + new_mac)
TypeError: can only concatenate str (not "NoneType") to str

I'm aware of Python Basics and ive done a few programs before this , I was following a course and the guy made this exact program but he isnt getting errors, I am. If somebody could help me understand the problem it would be really helpful cuz i dont want to progress further into the course without fixing this.

Consider adding default values to those options:

parser.add_option( "-i", "--interface", dest="interface", help="interface to change its MAC address", default="interface not provided")
parser.add_option( "-m", "--mac", dest="new_mac", help="new MAC address", default="mac address not provided")

Try this

def change_mac(interface, new_mac):
    interface = interface or '' # instead of empty string you can set a value
    new_mac = new_mac or '' # instead of empty string you can set a value
    print("[+] Changing MAC address for " , interface , " to " , new_mac)

Or you can have default value in the method's definition

def change_mac(interface='some_value', new_mac='Some Value'):
    print("[+] Changing MAC address for " , interface , " to " , new_mac)

Try:

import subprocess
import optparse

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"])


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")

(options, arguments) = parser.parse_args()
change_mac(options.interface, options.new_mac)

The reason for this error is that you can only use the "+" if you want to add two or more strings. But interface and new_mac are not strings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM