Python remote logon code

Source: Internet
Author: User
Python has a built-in pop3 library, so we can use it directly to download and process emails. In fact, if we don't need poplib, we can still complete all the jobs in that example: it is through the telnet protocol. 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.



The following code snippet: 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 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, , '\ 015 \ 012 \ 015 \ 012 Linux (somehost) \ 015 \ 012 \ 015 \ 015 \ 012 \ 015 login:') (0, , 'Password: ') (0, , '\ 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 \ 012 somehost: 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 this type of sample program, so it is not recommended to use it in actual work .)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.