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()
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With