There is a telnetlib in Python, which is to create a Telnet connection entity to the host, and then send commands to the host (like keyboard input) and receive data from that line. By using it, we can turn all the content of model 1 from "human-machine" communication to "machine-machine" communication, which can also handle the work of POP3 mailboxes. But now that we've tried POP3, this time we can try the real Telnet port 23 to do something fun.
The following code fragment: 1 # telnetdo.py 2 #!/usr/bin/env python 3 4 def telnetdo (HOST= None, user=none, pass=none, command=none): #定义一个函数, This is going to be easy to use 5 import telnetlib, sys 6 7 if not HOST: #如果没有给出所要的资料, the user is required to enter 8 try: 9 host = sys.argv[1] # Remember, the, sequence starts with 0, and Sys.argv[0] is the name of your program 10 USER = sys.argv[2] 11 PASS = sys.argv[3] 12 COMMAND = sys.argv[4] 13 except: 14 print "usage: telnetdo.py host user pass ' Command ' " 15 return 16 17 msg = [' Debug mesages:\n '] #这个用来存起所有从主机传回的讯息, is useful for debugging 18 19 tn = Telnetlib. Telnet () #准备一个 telnet Connection Entities 20 try: 21 tn.open (HOST) #连接端绑定到主机 HOST Go 22 except: 23 print "Cannot open host" 24 return 25 26 &nbSp;msg.append (Tn.expect ([' Login: '],5)) #等待主机传回含有 ' login: ' Character messages, waiting time is 5 sec 27 tn.write (user+ ' \ n ') #向主机送出字串 USER + ' \ n ', such as USER is 28 # ' Pcheung ' is equal to ' pcheung\n ' 29 if PASS: #就像是在键盘打入一样. 30 msg.append (Tn.expect ([' Password: '],5)] #如果有 password If you want to call, send out the password string, 31 tn.write (pass+ ' \ n ') #但首先要等主机传回含有 ' Password: ' Message 32 33 msg.append (Tn.expect ([USER],5)) #因为通常登入后主机会显示出登入者名称, we find this in the host response 34 #样的字符, if any, the login is successful &nbsP; 35 tn.write (command+ ' \ n ') #向主机发出指令 36 msg.append (tn.expect (['% '],5)) #等 5 seconds, if the program is complete, we will receive 37 # shell prompt , assumed to be '% ' 38 tn.close () #关闭连线 39 del tn 40 Return msg[len (msg) -1][2] #把收到的讯息通通传回去. 41 # (Note msg The first  2 element is the true message, 42 #其他是附加资讯. 43 44 if __name__ == ' __main__ ' #这是 python Common skills: if the telnetdo.py program 45 #是从 command prompt 46 #引发的话则 __name__ the content is __main__, opposite 47 #如果是从别的程式用 import telnetdo the words 48 # __name__ will become ' Telnetdo ' 49 Print telnetdo () #这样写的好处是从此 telnetdo will become your extension 50 #模组, you can in other programs 51 #用telnetdo. Telnetdo (Host,user,pass,command) To call it!
This program uses the following:
The following is a code fragment:> chmod +x telnetdo.py > telnetdo.py ' Somehost ' ' glace ' ' xxxxxx ' ' LS&NBSP;&NBSP;&NBSP;-LF ' (0, <sre_match object at 200f75a8>, ' \015\012\015\012linux (somehost) \015\012\015\ 015\012\015login: ') (0, <sre_match object at 20124848>, ' Password: ') (0, <sre_match object at 20103e08>, ' \015\012yup release 2.6 somehost\015\012last login: Wed Mar 6 18:21:01 GMT 2002 by Unknown@xxx.xxx.xxx.xxxyou have mail\015\012somehost:glace% ') total 320 -rw-r--r-- 1 glace user &Nbsp; 139788 feb 8 17:54 pqr2.1.txt drwxr-xr-x 3 glace user 4096 Feb 10 16:45 mytts/ drwxr-xr-x 3 glace user 4096 jan 29 19:03 sample/ drwxr-xr-x 2 glace user 4096 Jan 6 16:38 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 18:43 trash/ somehost:glace% can see the results of the execution and some additional information. This is the remote execution program. Even if there is no rsh, replicable can also. Ha, it's very convenient. But you should be aware that the program is only waiting for 5 seconds to execute, that is, if you want to send a ' find . -name xxx -print ' to the host Such a command should not wait until the execution of this telnet session will be closed. But think about it, does it matter? The difference between what we can do right now and the real staff telnet is not very large, think about how you will solve the problem of long time implementation? Yes, it's ' nohup ' and background work. That is, just change the program call to: telnetdo.py ' apocal ' ' Pcheung ' ' xxxxxx ' ' nohup myprogram_or_script& ' on the line. This way, even if the other host's shell prompt is ' > ' or ' >>> ' does not matter.
(Note that security is not the focus of such paradigm programs, so it is not recommended for practical work.)
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