所以在我公司,由于产品的兼容性原因,他们正在使我使用python 2.7。我不会在这里讨论。
因此,我正在编写一个程序,该程序使用SSH(特别是交换机)连接到设备,并且可以使用SSH实际访问该设备,并且该设备在我的计算机上可以ping通。问题 ? raw_input似乎没有将其作为字符串。当我尝试input()时,它给了我一个无效的语法错误。
对于我编写的脚本,我通常使用arparse,并且用户通过终端输入IP地址,用户名和密码,但是我希望此脚本不使用argparse并使用input()或raw_input。除了这一个脚本外,我所有的SSH脚本都可以正常工作,只有一个脚本使用raw_input和input()而不是argparse
def runMain():
scriptName = os.path.basename(__file__)
print("The name of this script is: " + scriptName)
print("**************************************\n")
print("This script allows you to enable and disable ports on the SNET or SOOBM switches, have fun ! \n")
print("**************************************\n")
optionPrinter_switches_top()
user_input = raw_input("Make your selection") # This does not work if I change to input(), it exits out of the program
if user_input == 1:
print("You selected the SNET switch, lets proceed !")
deviceIP = input("Enter the IP address for this device") # I tried changing these to raw_input, I get a syntax issue
deviceUsername = input("Enter the username for this device")
devicePassword = input("Enter the password for this device")
confirm_ping = canPing(deviceIP) # This is a boolean function that works correctly in every script but this one.
if confirm_ping:
ssh_SNET = connectToSSH_SNET(deviceIP, deviceUsername, devicePassword)
else:
print("Sorry, that device is not even ping-able. Figure that issue out and retry the program...")
sys.exit(-1)
while True:
SNET_options()
user_choice_SNET = input("Please select an option")
switch_result = SNET_switch_func(user_choice_SNET)
if switch_result == "displayInterfaceBrief":
time.sleep(5)
displayInterfaceBrief_SNET(ssh_SNET)
elif switch_result == "enablePort":
time.sleep(5)
enablePort_SNET(ssh_SNET)
elif switch_result == "disablePort":
disablePort_SNET(ssh_SNET)
elif switch_result == "switchReboot":
reboot_SNET_switch(ssh_SNET)
else:
print("Exiting program now....")
sys.exit(-1)
以下是相关问题:
user_input = raw_input("Make your selection") # This does not work if I change to input(), it exits out of the program
deviceIP = input("Enter the IP address for this device") # I tried changing these to raw_input, I get a syntax issue
deviceUsername = input("Enter the username for this device")
devicePassword = input("Enter the password for this device")
confirm_ping = canPing(deviceIP) # This is a boolean function that works correctly in every script but this one.
结论? input()/ raw_input()存在问题。这是怎么回事,我该如何解决?我不能使用python 3.7,这确实令人沮丧。感谢您的帮助
答案 0 :(得分:0)
默认情况下,尝试将if user_input == 1:
更改为if int(user_input) == 1:
,因为输入函数默认采用字符串格式的输入。
并且,如果您想使用input()
而不是raw_input()
来从python 2.x中的用户那里获取输入,那么您可以尝试以下代码:
if hasattr(__builtins__, 'raw_input'):
input=raw_input
user_input = input("Enter a number: ")