Telnet.read.until 功能不起作用 [英] Telnet.read.until function doesn't work

查看:40
本文介绍了Telnet.read.until 功能不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试 telnet 到 Brocade 路由器时,python 脚本发出错误......不知道这里出了什么问题.已尝试调试但无法使其正常工作.我相信这是及时的问题.如果有人建议如何让它工作,我很感激.

Trying to telnet to a Brocade router and the python script is sending out error.... Not sure what is wrong here. Have tried a debug but cant make it working. I believe it's prompt issue. I appreciate if anyone has suggestion how to get it work.

注意:这是 Python 3.0

Note: This is Python 3.0

import getpass
import sys
import telnetlib

HOST = "1.1.1.1"
user = "admin"
password = "password"
port = "23"

telnet = telnetlib.Telnet(HOST)

telnet.read_until("sw0 login:,3")

telnet.write(admin + "\r")
if password:
    telnet.read_until("Password: ")
    telnet.write(password + "\n")

tn.write("term len 0" + "\n")
telnet.write("sh ver br\n")
telnet.write("exit\n")


ERROR:

Traceback (most recent call last):
  File "C:\Users\milan\Desktop\telnetNew.py", line 13, in <module>
    telnet.read_until("Username :,3")
  File "C:\Python33\lib\telnetlib.py", line 299, in read_until
    return self._read_until_with_select(match, timeout)
  File "C:\Python33\lib\telnetlib.py", line 352, in _read_until_with_select
    i = self.cookedq.find(match)
TypeError: Type str doesn't support the buffer API


This is my prompt after logging manually using telnet port 23 and this is what i expected command to work.
_________________________________________________________________________________



Network OS (sw0)
xxxxxxxxxx


sw0 login: xxxxx
Password:  xxxxx

WARNING: The default password of 'admin' and 'user' accounts have not been changed.

Welcome to the Brocade Network Operating System Software
admin connected from 10.100.131.18 using console on sw0
sw0# sh ver


sw0#

推荐答案

在查看文档时,似乎 telnetlib 想要一个 bytestr,而不是一个 Str.所以试试这个.,它应该将所有内容都转换为字节而不是 Str

In looking at the docs, it appears that telnetlib want a bytestr, not a Str. so try this., which should convert everything to bytes as opposed to Str

import sys
import telnetlib

HOST = "1.1.1.1"
user = "admin"
password = "password"
port = "23"

telnet = telnetlib.Telnet(HOST,port)
telnet.read_until(b"login: ")

telnet.write(admin.encode('ascii') + b"\n")
telnet.read_until(b"Password: ")
telnet.write(password.encode('ascii') + b"\n")

tn.write(b"term len 0\n")
telnet.write(b"sh ver br\n")
telnet.write(b"exit\n")

--edit-- 我安装了 python 并在我的一台路由器上进行了尝试.将用户名/密码更改为我的凭据,我可以正常登录.(我删除了密码检查和 getpass,因为它们没有被使用,因为您的代码对它们进行了硬编码).看起来你复制了 2.x 的例子,但 3.x 需要缓冲区兼容的那些没有 b"我得到这个

--edit-- I installed python and tried this against one of my routers. Changing the username/password to my credentials I was able to login fine. ( I removed the password check and the getpass as they where not being used, being that your code has them hardcoded). It looks like you copied the 2.x example, but the 3.x requires the buffer compatible ones without the b" i Get this

Traceback (most recent call last):
  File "foo.py", line 5, in <module>
    telnet.read_until("login: ")
  File "/usr/local/Cellar/python32/3.2.5/Frameworks/Python.framework/Versions/3.2/lib/python3.2/telnetlib.py", line 293, in read_until
    return self._read_until_with_poll(match, timeout)
  File "/usr/local/Cellar/python32/3.2.5/Frameworks/Python.framework/Versions/3.2/lib/python3.2/telnetlib.py", line 308, in _read_until_with_poll
    i = self.cookedq.find(match)
TypeError: expected an object with the buffer interface

用 b" 我得到

with the b" I get

[~] /usr/local/bin/python3.2 foo.py
b'\r\n\r\nThis computer system including  all related equipment, network devices\r\n(specifically  including  Internet  access),  are  provided  only  for\r\nauthorized use.  All computer

这表明它正在工作......你现在遇到了什么错误

which shows it is working.. what errors are you getting now

这篇关于Telnet.read.until 功能不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆