所以基本上我有15个左右的脚本,可以使用SSH库连接到各种网络设备。我想创建一个可以运行其他python脚本的顶层python文件,这样用户就可以决定要运行哪些脚本。有人建议我使用子进程库,这似乎对我想做的事情最有意义。请注意,我的python脚本包含命令行argparse参数,以便它运行,例如:
python San_cable_test.py -deviceIP 172.1.1.1 -deviceUsername myUsername -devicePassword myPassword
到目前为止,我已经创建了一个顶层python文件,该文件被设置为调用两个python脚本,用户可以从中输入两个python脚本。但是,当我运行程序并选择其中一个选项并获取用户参数时,我得到一个
^{pr2}$我尝试了两种不同的方法,我将展示回溯:
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
这是我第一次使用subprocesses库,我不知道我是否正确地调用了这些脚本。问题是这些脚本是使用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)
参数前面缺少选项名。在
但是,如果您将这些其他脚本更改为Python模块,您可以简单地将它们作为函数导入和调用,而不是将它们作为子进程运行,这样可能会更干净。在
相关问题 更多 >
编程相关推荐