12

I was playing around with python 3.1 when I came to a problem. I want to connect to a telnet server. Here is my code:

import sys
import telnetlib
tn = telnetlib.Telnet("10.0.0.138")
tn.read_until(b"Username :", 2)
tn.write(b"\n")
tn.read_until(b"Password :", 2)
tn.write(b"\n")
tn.read_until(b"=>", 2)
tn.write(b"exit\n")
tn.close

It works to read until "Username :". There is also no error message when writing an emty line. But when i read until "Password :" i get an empty string. I also get an empty string when i read all.

Please help me if you can.

EDIT: Here is the output when i connect to the server via putty.

 Willkommen am THOMSON TG787v
 Plattform:VDNT-D Firmware:8.2.5.0 Seriennummer:CP0919MT238
 Bitte identifizieren Sie sich mit Ihrem Benutzernamen und Kennwort
--------------------------------------------------------------------------------
Username :
Password :
------------------------------------------------------------------------
 ______ Thomson TG787v
 ___/_____/\
 / /\\ 8.2.5.0
 _____/__ / \\
 _/ /\_____/___ \ Copyright (c) 1999-2009, THOMSON
 // / \ /\ \
 _______//_______/ \ / _\/______
 / / \ \ / / / /\
 __/ / \ \ / / / / _\__
 / / / \_______\/ / / / / /\
 /_/______/___________________/ /________/ /___/ \
 \ \ \ ___________ \ \ \ \ \ /
 \_\ \ / /\ \ \ \ \___\/
 \ \/ / \ \ \ \ /
 \_____/ / \ \ \________\/
 /__________/ \ \ /
 \ _____ \ /_____\/
 \ / /\ \ /___\/
 /____/ \ \ /
 \ \ /___\/
 \____\/
------------------------------------------------------------------------
CP0919MT238=>

I pressed return after "Username :" and then after "Password :".

asked Dec 25, 2010 at 0:16
2
  • Perhaps there's a newline character you have to read after Username :? Can you show us a stdout dump of a manual telnet session? Commented Dec 25, 2010 at 0:22
  • The space before the : also looks suspicious. Perhaps it's not meant to be there? Commented Dec 25, 2010 at 0:28

5 Answers 5

10

Lol, i had pretty much the same router as you.

Try this, bit of my old code:

tn = telnetlib.Telnet(HOST)
tn.read_until('Username : ')
tn.write(user+ "\r")
tn.read_until("Password : ")
tn.write(password+ "\n")
tn.write("\r")

This is for Python 2, but try just adding the extra space after the semicolon. Also, if this does not work, use wireshark and see what the putty connection is doing and correct your code to match.

answered Dec 25, 2010 at 12:28
Sign up to request clarification or add additional context in comments.

1 Comment

Hmm ok this is Python 2.x but it works there. I installed Python 2 now and use this code. Thanks.
3
# Script to Telnet in to a host
# For now I have hardcoded the HOST that can be taken as input if required
#run as " python teli.py ""
import time
import telnetlib
HOST ="www.google.com"
tn=telnetlib.Telnet(HOST,"80")
tn.write("GET /index.html HTTP/1.1\nHost:"+HOST+"\n\n")
l=tn.read_all()
print l
answered May 6, 2015 at 8:30

Comments

1

The docs in this link: http://docs.python.org/library/telnetlib.html

It has a sample code at the end under the section "Telnet Example".

You can access the example via: http://docs.python.org/library/telnetlib.html#telnet-example

answered Dec 25, 2010 at 0:26

1 Comment

First this is Python 2.x and not 3.x. And i already found this but it didn't work.
1

Just use xtelnet:

import xtelnet
t = xtelnet.session()
ip = '192.168.0.32' #just an example
t.connect(ip, username='root', password='toor', p=23, timeout=5)
output1 = t.execute('echo ala_is_king')
print(output1)
t.close()
Tonechas
13.8k16 gold badges52 silver badges85 bronze badges
answered Oct 20, 2020 at 14:25

Comments

0

The simple python telnet program below will definitely work on python3.

import telnetlib
import sys
HOST = input("enter your host IP:")
tn=telnetlib.Telnet(HOST)
tn.write(b'admin\n')
tn.write(b'admin123\n')
tn.write(b"enable\n")
tn.write(b"cisco\n")
tn.write(b"conf t \n")
tn.write(b"hostname SIDDHARTH \n")
tn.write(b'interface loopback0\n')
tn.write(b'ip address 1.1.1.1 255.255.255.255\n')
tn.write(b'interface loopback1\n')
tn.write(b'ip address 2.2.2.2 255.255.255.255\n')
tn.write(b"end \n")
tn.write(b"exit \n")
x = tn.read_all()
print (x)
Sajan
1,2671 gold badge7 silver badges13 bronze badges
answered Apr 17, 2020 at 15:22

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.