python - 使用子进程库:错误:无法识别的参数
问题描述
所以基本上我有 15 个左右的脚本可以使用 SSH 库连接到各种网络设备。我想创建一个可以运行其他 python 脚本的顶级 python 文件,以便用户可以决定他们想要运行哪些脚本。有人建议我使用 subprocess 库,这似乎对我想做的事情最有意义。需要注意的是,我的 python 脚本包含用于运行的命令行 argparse 参数,例如:
python San_cable_test.py -deviceIP 172.1.1.1 -deviceUsername myUsername -devicePassword myPassword
到目前为止,我已经创建了一个顶级 python 文件,该文件设置为调用两个 python 脚本以开始用户可以输入的内容。但是,当我运行程序并选择其中一个选项并获取用户参数时,我得到一个
error: unrecognized arguments:
我尝试了两种不同的方式,我将显示回溯:
usage: San_cable_test.py [-h] [-deviceIP DEVICEIP]
The name of this script is: San_cable_test.py
[-deviceUsername DEVICEUSERNAME]
[-devicePassword DEVICEPASSWORD]
San_cable_test.py: error: unrecognized arguments: 172.1.1.1 myUsername myPassword
和
usage: San_cable_test.py [-h] [-deviceIP DEVICEIP]
[-deviceUsername DEVICEUSERNAME]
The name of this script is: San_cable_test.py
[-devicePassword DEVICEPASSWORD]
San_cable_test.py: error: unrecognized arguments: -deviceIP 172.1.1.1 -deviceUsername myUsername -devicePassword myPassword
这是我第一次使用 subprocces 库,我不知道我是否正确调用了这些脚本。问题是这些脚本是使用 argparse 在命令行中运行的,所以这就是问题所在。不幸的是,我正在使用 2.7.16,因为这家奇怪的公司事情,我一直试图让我的经理知道 2.7 很快就会不受支持,但目前这不相关。这是我的代码的重要部分。我真的很感激帮助!
def runMain():
scriptName = os.path.basename(__file__)
print("The name of this script: " + scriptName + "\n")
scriptPurpose = 'This script is the top-level module that can invoke any script the user desires !\n'
while True:
optionPrinter()
user_input = input("Please select an option for which your heart desires...\n")
switch_result = mySwitch(user_input)
if switch_result == "our_Switch":
deviceIP = raw_input("Enter the IP address for the device")
deviceUsername = raw_input("Enter the username for the device")
devicePassword = raw_input("Enter the password for the device")
subprocess.call(['python', 'our_Switch.py', deviceIP, deviceUsername, devicePassword])
elif switch_result == "San_cable_test":
deviceIP = raw_input("Enter the IP address for the device")
deviceUsername = raw_input("Enter the username for the device")
devicePassword = raw_input("Enter the password for the device")
subprocess.call(['python', 'San_cable_test.py', deviceIP, deviceUsername, devicePassword])
else:
print("Exiting the program now, have a great day !\n")
sys.exit(-1)
if __name__ == '__main__':
这是在其中一个脚本中使用的 argparse 示例
def runMain():
scriptName = os.path.basename(__file__)
print("The name of this script is: " + scriptName)
scriptPurpose = 'This script enables and disables the SAN switches'
parser = argparse.ArgumentParser(description=scriptPurpose, formatter_class=RawTextHelpFormatter)
parser.add_argument("-deviceIP", help="Target device IP address", type=str)
parser.add_argument("-deviceUsername", help="Target device username", type=str)
parser.add_argument("-devicePassword", help="Target device password", type=str)
args = parser.parse_args()
if args.deviceIP is None:
print("The device IP parameter is blank\n")
else:
deviceIP = args.deviceIP
if args.deviceUsername is None:
print("The device userName parameter is blank\n")
else:
deviceUsername = args.deviceUsername
if args.devicePassword is None:
print("The device password parameter is blank\n")
else:
devicePassword = args.devicePassword
print("**********************\n")
print (deviceIP + " " + deviceUsername + " " + devicePassword)
print("**********************\n")
print("This script allows the user to enable and disable ports on a SAN switch to see how it behaves\n")
print("Checking to see if the SAN switch is pingable\n")
test_ping = canPing(deviceIP)
if test_ping:
print("The switch is pingable, let's proceed !\n")
else:
print("This device is not pingable unfortunately, sorry... : (\n")
sys.exit(-1)
sshConnection = connectToSSH(deviceIP, deviceUsername, devicePassword)
while True:
optionPrinter()
user_input = input("Select an option from the menu\n")
switch_result = mySwitch_function(user_input)
if switch_result == 'ShowPort':
portShow(sshConnection)
elif switch_result == 'SwitchShow':
switchShow(sshConnection)
elif switch_result == 'EnablePort':
enablePort(sshConnection)
elif switch_result == 'DisablePort':
disablePort(sshConnection)
elif switch_result == 'disableEnable':
disableEnableIteration(sshConnection)
else:
print("Program is exiting now, have a great day/night ! \n")
sys.exit(-1)
解决方案
您在参数之前缺少选项名称。
subprocess.call(['python', 'our_Switch.py', '-deviceIP', deviceIP, '-deviceUsername', deviceUsername, '-devicePassword', devicePassword])
但是,如果您将这些其他脚本更改为 Python 模块,您可以简单地导入并直接作为函数调用,而不是将它们作为子进程运行,它可能会更简洁。
推荐阅读
- android - 如何仅更新 Android 中现有 Json 对象中对象的非空 Json 字段
- php - 如何在 WebApp(PHP、Google API)中使用 Google Drive Files
- mysql - 如何根据通过的标准在一行中自行加入表并显示有限数量的结果
- mysql - 如何恢复MYSQL数据库,一旦你丢失了最新的frm文件但你有最新的ib文件
- reactjs - 在没有 Redux 的情况下组合 Reducer
- encryption - 我如何验证消息的签名?
- arduino - 无逆时针旋转
- bash - ctrl+c 不等待子进程(后台进程)以陷阱完成
- javascript - 添加 AQICN 小部件,但代码中有一些错误
- c# - 管理 IObservable 依赖项