Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable telnet echo in python telnetlib?

Hi I known that I should send 'IAC DONT ECHO' message, but how may I do that using telnetlib?? Here is my test, but it doesn't work.

 #!/usr/bin/env python2
 u"""test"""
 # -*- coding: utf-8 -*-

 import sys
 import telnetlib
 import time

 HOST = "10.10.5.1"
 tn = telnetlib.Telnet(HOST, timeout=1)
 tn.read_until("login: ")
 tn.write("login\n")
 tn.read_until("Password: ")
 tn.write("pass\n")

 print "###########"
 time.sleep(0.5)
 print tn.read_very_eager()

 tn.write("ls /\n")
 time.sleep(0.5)
 print tn.read_very_eager()

 # diable echo here
 tn.write(telnetlib.IAC + "\n")
 tn.write(telnetlib.DONT + " " + telnetlib.ECHO + "\n")
 time.sleep(0.5)
 print tn.read_very_eager()

 tn.write("ls /\n")
 time.sleep(0.5)
 print tn.read_very_eager()

 print "########### exit"
 tn.write("exit\n")
 print tn.read_all()
like image 935
chmurli Avatar asked Feb 20 '23 03:02

chmurli


1 Answers

You are sending the sequence wrong:

# diable echo here
tn.write(telnetlib.IAC + "\n")
tn.write(telnetlib.DONT + " " + telnetlib.ECHO + "\n")

THE IAC DONT ECHO is sent as three bytes, without any padding, spaces or newlines. So try this instead:

tn.write(telnetlib.IAC + telnetlib.DONT + telnetlib.ECHO)

However, it might not be enough to turn off echo actually. The solution most commonly used is actually to say that you will do the echoing, which will make the other end stop doing echoing:

tn.write(telnetlib.IAC + telnetlib.WILL + telnetlib.ECHO)

Edit: After reading the telnetlib manual page I see that the write function will:

Write a string to the socket, doubling any IAC characters.

So using the Telnet object write function will not work sending these sequences, you have to get the socket and use that to write the sequence:

def write_raw_sequence(tn, seq):
    sock = tn.get_socket()
    if sock is not None:
        sock.send(seq)

write_raw_sequence(tn, telnetlib.IAC + telnetlib.WILL + telnetlib.ECHO)
like image 97
Some programmer dude Avatar answered Feb 27 '23 19:02

Some programmer dude