【发布时间】:2017-05-16 07:05:56
【问题描述】:
感谢Python Library,我能够使用他们的示例远程登录到 Cisco 交换机,我将其用于学习目的,特别是学习 python。
然而,虽然所有的代码看起来一般都很容易阅读,但我对以下内容有点困惑:
1- 为什么使用下面的 if 语句 2-为什么在用户名和密码写入方法后使用“\n” 3-当更改实际上已提交并成功时,为什么我没有在我的 bash 终端上获得输出
HOST = "172.16.1.76"
user = raw_input("Enter your Telnet username : ")
password = getpass.getpass()
tn = telnetlib.Telnet(HOST)
tn.read_until("Username: ")
tn.write(user + '\n') <----- 2
if password: <----- 1
tn.read_until("Password: ")
tn.write(password + "\n") <------2
tn.write("show run \n")
time.sleep(5)
output = tn.read_all() <----- 3
print output
print "=" * 30
print "Configuration Complete."
我不确定为什么要使用上面的 if 语句,通常在输入用户名后,您会立即收到密码提示。为什么我们不能只输入:
tn.read_until("Username: ")
tn.write(user + '\n')
tn.read_until("Password: ")
tn.write(password + "\n")
至于第二点,为什么要在写方法中的密码和用户名后面加上'\n',如果我们在添加之后要按回车呢?
【问题讨论】:
标签: python python-2.7 automation cisco-ios getpass