尽管先前的If语句为True,但仍执行Elif语句

时间:2020-06-11 15:38:30

标签: python subprocess optparse

在完成有关Udemy的课程之后,我一直在尝试制作一个程序,该程序使用ifconfig命令以及子进程和optparse模块自动更改Linux接口的mac地址。

我的问题是关于下面的get_arguments()函数中的elif语句。 我想要这样做,以便如果程序在命令行上运行而未指定参数,则将要求用户输入界面和new_mac变量。

使用下面编写的get_arguments()函数,

elif not options.interface:
     parser.error("[-] Please specify an interface. Use -- help for more info.")

将被执行,打印文本并使用parser.error()停止程序, 甚至不要求输入,在命令行上运行程序时是否没有指定参数。

但是,用这种方式编写

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

程序将停止获取输入,一切都会好起来。

这是程序:

#!/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)

1 个答案:

答案 0 :(得分:0)

仅在解析后检查是否为每个选项提供了一个值,然后提示用户输入一个值。不需要elif;只需独立处理每个人。

此外,请勿使用optparse;改用argparse

#!/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)

我也强烈建议您作为初学者,改用Python 3。