首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Telnet.read.until函数不起作用

Telnet.read.until函数不起作用
EN

Stack Overflow用户
提问于 2013-12-10 10:36:25
回答 4查看 16.6K关注 0票数 0

尝试telnet到Brocade路由器时,python脚本发出错误...不确定这里出了什么问题。我已经尝试了调试,但不能使其工作。我相信这是当务之急。如果任何人有如何让它工作的建议,我将不胜感激。

注意:这是Python 3.0

代码语言:javascript
运行
复制
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#
EN

回答 4

Stack Overflow用户

发布于 2013-12-10 11:37:45

在查看文档时,telnetlib似乎需要一个bytestr,而不是Str。所以试试这个.,它应该把所有东西都转换成字节,而不是字符串

代码语言:javascript
运行
复制
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“I Get this

代码语言:javascript
运行
复制
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“I get

代码语言:javascript
运行
复制
[~] /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

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

票数 6
EN

Stack Overflow用户

发布于 2014-05-30 15:58:22

代码语言:javascript
运行
复制
import telnetlib , socket


class TELNET(object):

    def __init__(self):
        self.tn = None
        self.username = "root"
        self.password = "12345678"
        self.host = "10.1.1.1"
        self.port = 23
        self.timeout = 5
        self.login_prompt = b"login: "
        self.password_prompt = b"Password: "

    def connect(self):
        try :
            self.tn = telnetlib.Telnet(self.host,self.port,self.timeout)
        except socket.timeout :
            print("TELNET.connect() socket.timeout")

        self.tn.read_until(self.login_prompt, self.timeout) 
        self.tn.write(self.username.encode('ascii') + b"\n")

        if self.password :
            self.tn.read_until(self.password_prompt,self.timeout)
            self.tn.write(self.password.encode('ascii') + b"\n")

    def write(self,msg):
        self.tn.write(msg.encode('ascii') + b"\n")
        return True


    def read_until(self,value):
        return self.tn.read_until(value)


    def read_all(self):
        try :
            return self.tn.read_all().decode('ascii')
        except socket.timeout :
            print("read_all socket.timeout")
            return False

    def close(self):
        self.tn.close()
        return True


    def request(self,msg):
        self.__init__()
        self.connect()
        if self.write(msg) == True :
            self.close()
            resp = self.read_all()
            return resp
        else :
            return False

telnet = TELNET()

#call request function 

resp = telnet.request('ps www') # it will be return ps output
print(resp) 

该代码可以在python3上运行

票数 4
EN

Stack Overflow用户

发布于 2015-08-19 05:02:08

这些答案中的大多数都是很好的深入解释,我发现它们有点难以理解。简单的答案是Python3x站点上的示例代码是错误的。

要解决此问题,请使用‘telnet.read_until(b“用户名:,3")’而不是‘telnet.read_until(”用户名:,3")’

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20484984

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档