简体   繁体   English

Python Telnet脚本

[英]Python Telnet script

Thanks to Python Library i was able to use their example to telnet to Cisco switches, I am using this for learning purposes, specifically learning python. 多亏了Python库,我得以使用他们的示例远程登录到Cisco交换机,因此我将其用于学习目的,特别是学习python。

However, although all the code seem generally easy to read, I am a bit confused as to the following: 但是,尽管所有代码看起来通常都很容易阅读,但是我对以下内容有些困惑:

1- why use the if statement below 2- why use the "\\n" after the username and password write method 3- why am i not getting the output on my bash terminal when the changes are infact committed and successful 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."

I am not sure as to why using the if statement above, typically once you input in the Username, you get the password prompt right afterward. 我不确定为什么要使用上述if语句,通常在输入用户名后,随后便会出现密码提示。 why cant we just type : 为什么我们不能输入:

tn.read_until("Username: ") 
tn.write(user + '\n')
tn.read_until("Password: ")
tn.write(password + "\n")

As for the second point, why use the '\\n' after the passwords and username in the write method if we going to hit enter after we add them anyway? 关于第二点,如果我们在添加了密码和用户名后仍要按Enter键,为什么还要在密码和用户名后使用'\\ n'?

1: the line 1:线

password = getpass.getpass()

asks you for you password, if you leave it empty, password will contain the empty string which, in an if statement, is the same as False 询问您密码,如果您将其保留为空,则密码将包含空字符串,该字符串在if语句中与False相同
the script doesn't know ahead of time if you have a password on your server or not, it simulates knowing by asking you first and if you don't input anything, it assumes it doesn't (otherwise it would get stuck on tn.read_until("Password: ") forever. 该脚本不会提前知道您是否在服务器上输入了密码,它会先询问您来模拟知道,如果您没有输入任何内容,它将假定您没有输入密码(否则它将卡在tn.read_until("Password: ")永远。

2: the '\\n' simulates you hitting the return key. 2: '\\n'模拟您按下返回键。 when you enter your password, for example 'password<RETURN>' the variable password will not contain a trailing newline (\\n), this is why it is manually appended 当您输入密码时,例如'password<RETURN>' ,变量密码将不包含结尾的换行符(\\ n),这就是为什么要手动附加它的原因

3: this one i dont know, possibly 5 seconds isn't enough time to wait 3:这个我不知道,可能5秒还不够时间等待

After execute tn = telnetlib.Telnet(HOST) you have created a telnet channel from your machine to HOST. 执行tn = telnetlib.Telnet(HOST)之后,您已经创建了从计算机到HOST的telnet通道。 But you still need to communicate with HOST to push/send your commands and receive the outputs. 但是您仍然需要与HOST进行通信以推送/发送命令并接收输出。

To push your commands to HOST, you need to execute tn.write("your_commands_or_input \\n"), \\n means newline/return, which tells your current commands need to be executed now. 要将命令推送到主机,您需要执行tn.write(“ your_commands_or_input \\ n”),\\ n表示换行/返回,它告诉您当前需要执行的命令。 After the execution, HOST return the result, which will be caught by your telnet object "tn" and saved in its "local cache", you can search any keywords you expected in this cache by using tn.read_until method, if the expected keyword has been found, read_until will stop(always stop on the 1st found), and you can do anything you need(It's your turn now), else the read_until will keep waiting the output from HOST(Haven't you turn yet). 执行后,HOST返回结果,该结果将由您的telnet对象“ tn”捕获并保存在其“本地缓存”中,如果期望关键字,则可以使用tn.read_until方法搜索此缓存中期望的任何关键字。已经找到,read_until将停止(总是在找到的第一个停止),您可以做所需的任何事情(现在轮到您了),否则read_until将继续等待HOST的输出(您尚未转动)。 Finally if you want to check all output have been cached, you can execute tn.read_all(). 最后,如果要检查是否已缓存所有输出,则可以执行tn.read_all()。

Remember some of the HOST using different login output, ie Username vs username or Password vs password, you better to use regular expression to match them. 记住一些使用不同登录输出的主机,即用户名vs用户名或密码vs密码,最好使用正则表达式来匹配它们。

There is a python library on github, specifically for telneting to cisco devices. github上有一个python库,专门用于telnet到cisco设备。

pip install git+https://github.com/sergeyzelyukin/cisco-telnet.git

import ciscotelnet
with ciscotelnet.CiscoTelnet(host, verbose = False) as cisco:
  if cisco.login(final_mode=CiscoTelnet.MODE_ENABLE, user="john", user_pass="12345678", enable_pass="cisco"):
  # if cisco.login(final_mode=CiscoTelnet.MODE_ENABLE, line_pass="abcdef", enable_pass="cisco"):
    print cisco.cmd("sh int status | inc Fa0/1") 
    print cisco.conf(["interface fast0/1", "descr blank", "load-interval 300"])  
    print cisco.wr() 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM