Telnet using Python

Source: Internet
Author: User
Tags ibm developerworks

Recently to use Python to do a web-managed switch program, you need to telnet, to check the information, because the switch has not been reached, first Test with their own machine.

First of all, Python's standard library contains Telnet, which is handy for you to look at the document and write a small program:

[Python]View Plaincopy
  1. #!/usr/bin/env python
  2. #coding =utf-8
  3. Import Telnetlib
  4. Host = "127.0.0.1"
  5. UserName = ' root '
  6. Password = ' 123456 '
  7. Enter = ' \ n '
  8. t = telnetlib. Telnet (host)
  9. T.read_until ("Login:",1)
  10. T.write (UserName + Enter)
  11. T.read_until ("Password:",1)
  12. T.write (password + Enter)
  13. T.write ("ls" +enter)
  14. T.write ("Exit" +enter)
  15. Print T.read_all ()


Output Result:

[Plain]View Plaincopy < param name= "wmode" value= "Transparent" >
    1. Last login:wed Nov 2 14:51:36 on console
    2. Shi-kefumatoimac:~ root#. Cfusertextencoding. Subversion Library
    3. . Forward. Viminfo nat.sh
    4. . sh_history. VIMRC NOC
    5. shi-kefumatoimac:~ root# Logout

The program is simple, log in to your machine, execute the LS command, and then output the results.

Telnet is very useful, but there are always people who like the more powerful and better use of the program, so there is Pexpect,Pexpect is a Python implementation of the Expect language of Don Libes, is a use to start subroutines, and use regular expressions to make a specific response to the program output to implement the Python module that interacts with it automatically. Pexpect is widely used for automatic interaction with SSH, FTP, telnet and other programs, can be used to automatically copy software installation packages and automatically install on different machines, and can be used to automate the interaction with the command line in software testing.

Read the writing information, also with Pexpect wrote a small program, realize just the same function:

[Python]View Plaincopy
  1. #!/usr/bin/env python
  2. #coding =utf-8
  3. Import Pexpect
  4. Address = ' 127.0.0.1 '
  5. UserName = ' root '
  6. Password = ' 123456 '
  7. cmd = ' telnet ' + address
  8. prompt = ' [$#>] '
  9. Child = Pexpect.spawn (cmd)
  10. index = Child.expect ([' login ', Pexpect. Eof,pexpect. timeout],timeout=1)
  11. If index = = 0:
  12. Child.sendline (UserName)
  13. index = child.expect (' Password ', timeout=1)
  14. Child.sendline (password)
  15. Child.expect (prompt,timeout=1)
  16. Child.sendline (' ls ')
  17. Child.expect (' ls ', timeout=1)
  18. Child.expect (prompt,timeout=1)
  19. Print Child.before
  20. Else
  21. print ' expect ' login, but get EOF or TIMEOUT '
  22. Child.close ()

Output Result:

[Plain]View Plaincopy < param name= "wmode" value= "Transparent" >
    1. . Cfusertextencoding. Subversion Library
    2. . Forward. Viminfo nat.sh
    3. . sh_history. VIMRC NOC
    4. shi-kefumatoimac:~ Root


Of course, this program is just an instance of Pexpect, and its simple usage is not enough to explain the power of its T.


Resources:

Python Document: http://docs.python.org/library/telnetlib.html

IBM developerworks:http://www.ibm.com/developerworks/cn/linux/l-cn-pexpect1/

http://www.ibm.com/developerworks/cn/linux/l-cn-pexpect2/

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.