Telnet.read.until函数不起作用

 haha20101030 发布于 2023-02-11 19:25

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

注意:这是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 
    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#

Doon.. 5

在查看文档时,似乎telnetlib需要bytestr而不是Str.所以尝试这个.,它应该将所有内容转换为字节而不是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"我得到这个

Traceback (most recent call last):
  File "foo.py", line 5, in 
    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"我明白了

[~] /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

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

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有