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
- #!/usr/bin/env python
- #coding =utf-8
- Import Telnetlib
- Host = "127.0.0.1"
- UserName = ' root '
- Password = ' 123456 '
- Enter = ' \ n '
- t = telnetlib. Telnet (host)
- T.read_until ("Login:",1)
- T.write (UserName + Enter)
- T.read_until ("Password:",1)
- T.write (password + Enter)
- T.write ("ls" +enter)
- T.write ("Exit" +enter)
- Print T.read_all ()
Output Result:
[Plain]View Plaincopy < param name= "wmode" value= "Transparent" >
- Last login:wed Nov 2 14:51:36 on console
- Shi-kefumatoimac:~ root#. Cfusertextencoding. Subversion Library
- . Forward. Viminfo nat.sh
- . sh_history. VIMRC NOC
- 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
- #!/usr/bin/env python
- #coding =utf-8
- Import Pexpect
- Address = ' 127.0.0.1 '
- UserName = ' root '
- Password = ' 123456 '
- cmd = ' telnet ' + address
- prompt = ' [$#>] '
- Child = Pexpect.spawn (cmd)
- index = Child.expect ([' login ', Pexpect. Eof,pexpect. timeout],timeout=1)
- If index = = 0:
- Child.sendline (UserName)
- index = child.expect (' Password ', timeout=1)
- Child.sendline (password)
- Child.expect (prompt,timeout=1)
- Child.sendline (' ls ')
- Child.expect (' ls ', timeout=1)
- Child.expect (prompt,timeout=1)
- Print Child.before
- Else
- print ' expect ' login, but get EOF or TIMEOUT '
- Child.close ()
Output Result:
[Plain]View Plaincopy < param name= "wmode" value= "Transparent" >
- . Cfusertextencoding. Subversion Library
- . Forward. Viminfo nat.sh
- . sh_history. VIMRC NOC
- 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/