Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
724 views
in Technique[技术] by (71.8m points)

python 3.x - Telnet.read.until function doesn't work

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.

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 + "
")
if password:
    telnet.read_until("Password: ")
    telnet.write(password + "
")

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


ERROR:

Traceback (most recent call last):
  File "C:UsersmilanDesktopelnetNew.py", line 13, in <module>
    telnet.read_until("Username :,3")
  File "C:Python33libelnetlib.py", line 299, in read_until
    return self._read_until_with_select(match, timeout)
  File "C:Python33libelnetlib.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#
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"
")
telnet.read_until(b"Password: ")
telnet.write(password.encode('ascii') + b"
")

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

--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

with the b" I get

[~] /usr/local/bin/python3.2 foo.py
b'

This computer system including  all related equipment, network devices
(specifically  including  Internet  access),  are  provided  only  for
authorized use.  All computer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...