In python, there is a telnetlib, which is used to create a physical Telnet connection to the host, and then send commands to the host (just as input on the keyboard) and receive data from the connection. With this function, we can change all the content of demonstration 1 from "human-machine" communication to "machine-Machine" communication, so that we can also handle POP3 mail. However, since we have already tried POP3, this time we can try telnet port 23 to do something fun.
Below is Code Fragment: 1 # telnetdo. py 2 #! /Usr/bin/ENV Python 3 4 def telnetdo (host = none, user = none, pass = none, command = none): # define a function, this will make it easy to use 5 import telnetlib, sys 6 7 if not host: # If the required information is not provided, the user is required to enter 8 Try: 9 host = sys. argv [1] # Remember, the sequence starts from 0, and SYS. argv [0] will be your program name 10 user = sys. argv [2] 11 pass = sys. argv [3] 12 command = sys. argv [4] 13 bytes T: 14 print "Usage: telnetdo. PY host user pass 'command' "15 return 16 17 MSG = ['debug Mesa Ges: \ n'] # This is used to store all the messages sent from the host. It is useful for division of errors when 18 19 Tn = telnetlib. telnet () # Prepare the object for a telnet connection. 20 try: 21 tn. open (host) # bind the connection end to the host to 22 bytes T: 23 print "cannot open host" 24 return 25 26 MSG. append (TN. CT (['login: '], 5) # Wait for the host to return a message containing the 'login:' character. The waiting time is 5 seconds and 27 tn. write (User + '\ n') # Send the string user +' \ n' to the host. If the user is 28 # 'pcheung ', it is equal to 'pcheung \ n' 29 If pass: # It's like typing in the keyboard. 30 MSG. append (TN. keep CT (['password: '], 5) # If a password is required, send the password string, 31 tn. write (pass + '\ n') # But first wait for the host to return a message containing the 'password:' 32 33 MSG. append (TN. login CT ([user], 5) # because the primary opportunity shows the login name after login, we will find the 34 # character in the host response, if yes, it indicates that the logon is successful for 35 tn. write (command + '\ n') # send the command 36 MSG to the host. append (TN. round CT (['%'], 5) # Wait for 5 seconds. If the program is completed, we will generally receive 37 # shell prompt, assuming it is '%' 38 tn. close () # Disconnect 39 del tn 40 return MSG [Len (MSG)-1] [2] # pass the received message Go back. 41 # (note that the first element in MSG is the real message. 42 # others are additional information. 43 44 if _ name _ = '_ main _' # This is a common Python technique: If telnetdo. PY program 45 # is triggered from command prompt 46 #, the content of _ name _ is _ main __, on the contrary, 47 # If you use import telnetdo from another program, 48 # _ name _ will be changed to 'telnetdo '49 print telnetdo () # The advantage of this writing is that telnetdo will become your extended 50 # module, and you can use telnetdo for 51 # In other programs. telnetdo (host, user, pass, command) to call it!
This program is used as follows:
The following code snippet:> chmod + x telnetdo. PY> telnetdo. PY 'somehost' 'glace ''xxxxxx''' LS-lf '(0, <sre_match object at 200f75a8>,' \ 015 \ 012 \ 015 \ 012 Linux (somehost) \ 015 \ 012 \ 015 \ 015 \ 012 \ 015 login: ') (0, <sre_match object at 20124848>, 'password:') (0, <sre_match object at 20103e08>, '\ 015 \ 012yup release 2.6 somehost \ 015 \ 012 last login: Wed Mar 6 18:21:01 GMT 2002 by UNKNOWN@xxx.xxx.xxx.xxxyou have mail \ 015 \ 012som Ehost: glace % ') total 320-RW-r -- 1 glace user 139788 Feb 8 pqr2.1.txt drwxr-XR-x 3 glace user 4096 Feb 10 mytts/drwxr-XR-x 3 glace user 4096 jan 29 sample/drwxr-XR-x 2 glace user 4096 Jan 6 Tex/drwxr-XR-x 2 glace user 4096 Sep 5 2001 tmp/drwxr-XR-x 2 glace user 29 Feb 23 2001 tools/drwxr-XR-x 2 glace user 26 Feb 6 trash/somehost: glace % shows the execution results and additional information. This is the remote execution program. Even if there is no RSH, you can use it. Ha, it's very convenient. However, you should have noticed that the program is only waiting for 5 seconds, that is, if you want to send a message like 'Find. -commands like-name xxx-print should be closed if the telnet session is not executed. But think about it carefully. Does it matter? What we can do now is not much different from the real human Telnet. Think about how you can solve the problem of long execution? That's right, it's 'nohup' and the background job. That is to say, you only need to change the program call to telnetdo. py 'apocal' 'pcheung ''xxxxxx' 'nohup myprogram_or_script. In this way, even if the shell prompt of the other host is '>' or '>', it does not matter.
(note that security is not the focus of such sample programs, so it is not recommended to use it in actual work.)