Python telnet code

Source: Internet
Author: User
There is a telnetlib in Python that works by setting up a Telnet entity to the host, then transmitting commands to the host (just as you would with a keyboard) and receiving data from that connection. Using it, we can turn all of the model 1 from "Man-machine" to "machine-machine" communication, so that we can also handle the work of POP3 mailbox. But since we've tried POP3, this time you can try out the real telnet 23 and do something fun.



Here is the code snippet: 1 # telnetdo.py 2 #!/usr/bin/env Python 3 4 def telnetdo (Host=none, User=none, Pass=none, Command=none): #定义一个函数 That's going to be easy with it. 5 Import telnetlib, sys 6 7 if not HOST: #如果没有给出所要的资料, requires the user to enter 8 try:9 HOST = sys.argv[1] #记得吧, the sequence is starting from 0, and the SY S.ARGV[0] will be your program's name (USER = sys.argv[2] One PASS = sys.argv[3] COMMAND = sys.argv[4] except:14 print "usage:telnetd o.py Host User Pass ' command ' "return" msg = [' Debug mesages:\n '] #这个用来存起所有从主机传回的讯息, it is useful to make a mistake when removing a wrong. tn = Telnetlib. Telnet () #准备一个 telnet connection entity try:21 Tn.open (host) #连接端绑定到主机 host to except:23 print "Cannot open host" return 25 Msg.append (Tn.expect ([' Login: '],5)] #等待主机传回含有 ' login: ' character message, wait time is 5 seconds tn.write (user+ ' \ n ') #向主机送出字串 USER + ' \ n ', such as use R is the # ' Pcheung ' is equal to ' pcheung\n ' if PASS: #就像是在键盘打入一样. Msg.append (Tn.expect ([' Password: '],5) ') #如果有 Password to send out a password string, tn.write (pass+ ' \ n ') #但首先要等主机传回含有 ' Password: ' The message of the word is msg.append (Tn.expect ([user],5)) #因为通常登入后主机会显示出登入者名称, we find this #样的字符 in the host response, if any, the representative loginSucceeded by Tn.write (command+ ' \ n ') #向主机发出指令 msg.append (tn.expect (['% '],5)) #等 5 seconds, if the program is completed generally we will receive a PNS # shell prompt, assuming '% ' Tn.close () #关闭连线 Msg[len del tn (msg) -1][2] #把收到的讯息通通传回去. 41 # (Note that the 2nd element in MSG is the true message, #其他是附加资讯. if __name__ = = ' __main__ ' #这是 python commonly used tricks: if the telnetdo.py program #是从 command prompt #引发的话则 __name__ content __main__, Instead #如果是从别的程式用 import Telnetdo, then the __name__ will become the ' telnetdo ' Print telnetdo () #这样写的好处是从此 telnetdo will be your extension #模组, you It can be #用telnetdo in another program. TELNETDO (Host,user,pass,command) to invoke it!
This program uses the following:



Here is the code snippet:> chmod +x telnetdo.py > telnetdo.py ' somehost ' glace ' xxxxxx ' (0, , ' \015\012\015\012linux (somehost) \015\012\015\015\012\015login: ') (0, , ' Password: ') (0, , ' \015\012yup Release 2.6 somehost\015\012last login:wed Mar 6 18:21:01 GMT 2002 by Unknown@xxx.xxx.xxx.xxxyou has mail \015\012somehost:glace% ') Total 320-rw-r--r--1 glace user 139788 Feb 8 17:54 pqr2.1.txt drwxr-xr-x 3 glace user 4096 Feb 16:45 mytts/drwxr-xr-x 3 glace user 4096 Jan 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 2001 Tools/drwxr-xr-x 2 glace user 6 18:43 t Rash/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, very convenient. However, you should be aware that the program is only waiting for 5 seconds, that is, if you want to send a "find" to the host. -name xxx-print ' Such a command should not wait until after execution this telnet session will be closed. But think about it, does it matter? The difference between what we can do now and the real human telnet is not very big, think about how you can solve long-time implementation problems? Yes, it's ' nohup ' and background homework. So just change the program call to: telnetdo.py ' apocal ' Pcheung ' xxxxxx ' nohup myprogram_or_script& ' on the line. This way, even if the host's shell prompt is ' > ' or ' >>> ' it doesn't matter.

(Note that security is not a priority for such paradigm programs, so it is not recommended to use it in real-world 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.