telnetlib python example
Asked Answered
L

7

17

So I'm trying this really simple example given by the python docs:

import getpass
import sys
import telnetlib

HOST = "<HOST_IP>"
user = raw_input("Enter your remote account: ")
password = getpass.getpass()

tn = telnetlib.Telnet(HOST)

tn.read_until("login: ")
tn.write(user + "\n")
if password:
   tn.read_until("Password: ")
   tn.write(password + "\n")

tn.write("ls\n")
tn.write("exit\n")
print tn.read_all()

My issue is that it hangs at the end of the read_all()... It doesn't print anything out. I've never used this module before so I'm trying to get this really basic example to work before continuing. BTW, I'm using python 2.4 Thank you.

Laurenlaurena answered 8/6, 2012 at 16:18 Comment(0)
L
15

Okay, I found a solution. Before I entered ls and exit, I needed to first specify the terminal type. Adding

tn.write("vt100\n") 

before the "ls" fixed the problem for me.

Laurenlaurena answered 8/6, 2012 at 16:37 Comment(2)
You are encouraged to award yourself the answer, as that will help others who rea dthis question in the futtureDryly
why is it required ?Millepore
C
6

If you're using Windows, be sure to add carriage return (\r) before the new line character:

tn.write(user.encode('ascii') + "\r\n".encode('ascii'))
Cachucha answered 29/8, 2013 at 11:18 Comment(0)
C
5

I know this is late to post but may help others. I also struggled to get this right but here is my piece of code. My telnet would follow certain flow like it would ask for loginID and then Password and then you have to wait for a particular string to be displayed here,which for my case was "DB>" then you can proceed with the command and all. My output would be saved in "out" varible

import os,re,telnetlib
host = "10.xxx.xxx.xxx"
port = 23

telnet = telnetlib.Telnet()
telnet.open(host, port)
telnet.write('loginID\r\n')
telnet.write('Password\r\n')
out = telnet.read_until("DB>", 5)
telnet.write('show cable modem reg\r\n') #Mycommand
out = telnet.read_until("DB>", 5)
telnet.write('quit\r\n')
telnet.close()

For more variations and help, Check the website nullege

Cryoscope answered 15/3, 2017 at 11:48 Comment(0)
P
3

I don't have a telnet server to test against, but I think the issue is that you are not reading server responses up to the prompt, after each command you write.

PROMPT = ':~$'
tn = telnetlib.Telnet(HOST)
tn.read_until('login: ')
tn.write(user + '\n')
if password:
   tn.read_until('Password: ')
   tn.write(password + '\n')
tn.read_until(PROMPT)
tn.write('ls\n')
print tn.read_until(PROMPT)
tn.write('exit\n')

btw, telnetnetlib can be tricky and things varies depending on your FTP server and environment setup. you might be better off looking into something like pexpect to automate login and user interaction over telnet.

Pharyngeal answered 8/6, 2012 at 16:41 Comment(1)
Can you please explain why do we need to give the exit at last here tn.write('exit\n') ??Jehu
M
1

I struggled for a while trying to write to a SynAccess power strip. This is how I did it:

import sys
import telnetlib
HOST = < your SynAccess switch ip address >
user = < user name >
password = < password >

tn = telnetlib.Telnet(HOST, 23, 5)
tn.write("login\r\n")
tn.write(user + "\r\n")
tn.write(password + "\r\n")
tn.write("rb 3\r\n") # this reboots plug 3
tn.write("rb 1\r\n") # this reboots plug 1
tn.write("logout\r\n")
tn.close
Meathead answered 13/11, 2014 at 2:22 Comment(0)
M
0

use python 2.7 or use a higher version with"(" ,")" at last line

Mcpherson answered 30/10, 2017 at 4:50 Comment(0)
U
0

If none of these answers worked, you can also try:

 tn.read_until(b"Password: ")
 tn.write(password.encode('utf-8') + b"\n")
 tn.read_until(b"Terminal type?", 5)
 tn.write("vt100\n".encode('utf-8'))
 tn.read_until(b">", 5)

Even though I never saw the actual request for the terminal type being printed, this write seemed to work.

Underbelly answered 9/6, 2021 at 18:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.