首页 > 解决方案 > Telnetlib 似乎没有传递 .write 命令

问题描述

我是这里的python新手,只是想学习一项新技能,所以请温柔:)

执行以下脚本后,我的脚本似乎创建了一个 telnet 会话,我可以看到 cisco 设备横幅,但在出现提示时未传递用户名/密码。

我尝试更改 tn.read_until() 和 tn.read_very_eager() 两者都不会触发脚本继续写入所需的输入。我还尝试强制将用户名和密码作为字节输入,并将 + '\n' 添加到我的写入函数中。我还使用 sleep 来通过一些额外的时间来等待横幅完成打印。

tldr:执行脚本时,我看到了用户名提示,但不能再进一步了。

欢迎在这里提供任何帮助。

'''
   from time import sleep
   import telnetlib
   from telnetlib import Telnet
   from getpass import getpass

   Username = input('please provide your username ')
   password = getpass()

   # f is the .txt document that lists the IP's we'll be using.
   f = open('devicess.txt')

   for line in f:
       print ('Configuring Device ' + (line))
       device = (line)

   #open connection to variable    
   tn = telnetlib.Telnet(device)
   #For those devices in the above list, connect and run the below commands
   for device in f:
       tn.expect('Username: ')
       tn.write(Username)
       tn.expect('Password: ')
       tn.write(password)
       tn.write('show ver')              
       print('connection established to ' + device)
       tn.write('logout')

   output = tn.read_all()
   print(output)
   print('script complete')
''' 

标签: telnetlib

解决方案


推荐阅读