# dependant variables login = 'your_login' password = 'your_password' host = 'your_host' newline = '\r\n' from telnetlib import Telnet tn = Telnet(host, 23) # login procedure, may vary between telnet servers print tn.read_until('login:',2), tn.write(login+newline) print tn.read_until('password:',2), tn.write(password+newline) print tn.read_until('C:\\>') # especially this line!! # generate a continuous stream for 20 seconds tn.write('ping -n 20 -w 1000 1.1.1.1'+newline) print 'This should only take 5 seconds.' # test either read_until... print tn.read_until('nott found!', 5) # hangs # .. or expect #print tn.expect(['stat'], 5) # hangs