System batch operation and maintenance management tool Pexpect

Source: Internet
Author: User
Tags control characters python list

1. Introduction Pexpect

Through the pexpect we can achieve automatic interaction of ssh,ftp,passwd,telnet and other commands, without the need for manual intervention to achieve automatic purposes.

2. Introduction of Pexpect Components

2.1, Spawn class

Role: Start and control the sub-application with its constructor as follows:

Class Pexpect.spawn (command,args=[],timeout=30,maxread=2000,searchwindowsize=none,logfile=none,cwd=none,env= None,ignore_sighup=true)

Where command is any known system command, timeout is the time-out for waiting for the result, Maxread is the maximum number of bytes read at a time from the terminal, and Searchwindowsize is the location of the matching buffer string.

Note: Pexpect does not parse the metacharacters (redirect, pipe |, wildcard *) in the shell command, but it can be resolved by calling the command with these three special metacharacters as the parameter of the/bin/bash. Like what:

Child=pexpect.spawn ('/bin/bash-c ' ls-l | grep LOG > Logs.txt "')

Child.expect (Pexpect. EOF)

Replace the command's parameters with a python list:

Shell_cmd = ' Ls-l | grep LOG >logs.txt '

Child=pexpect.spawn ('/bin/bash ', ['-C ', Shell_cmd])

Child.expect (Pexpect. EOF)


Debug code Yes, Pexpect provides two ways to view input and output:1> write to log 2> output to stdout

Child=pexpect.spawn (' Some_cmd ')

Fout=file (' Mylog.txt ', ' W ')

Child.logfile = Fout

Or: Child.logfile=sys.stdout

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/73/1D/wKiom1X1QlfALg5pAAElB1g1LQI215.jpg "title=" 1.png " alt= "Wkiom1x1qlfalg5paaelb1g1lqi215.jpg"/>

Where the expect method defines a matching rule for a subroutine output:

The method defined is expect (pattern,timeout=-1,searchwindowsize=-1)

Pattern represents a string, a pexpect. EOF (pointing to the tail of the buffer), Pexpect. TIMEOUT, regular expression.


Send sends the appropriate command to the subroutine, equivalent to the standard input keyboard

Send (self,s) command, do not enter

Sendline (self,s= ") Send command, enter

Sendcontrol (Self,char) sends control characters, such as Child.sendcontrol (' C ') equivalent to CTRL + C

Sendeof () Send EOF


2.2>run function

Run is a function of external commands encapsulated using Pexpect

Run (Command,timeout=-1,withexitstatus=false,events=none,extra_args=none,logfile=none,cwd=none,env=none)

Import Pexpect

Pexpect.run (' SCP filename [email protected]:~ ', events={' (?) Password ': MyPassword})


3, Pxssh

Pxssh a layer of encapsulation for SSH session operations, providing a more straightforward way to operate

Pxssh Common method: Login () to establish an SSH link, logout () disconnect, prompt () wait for the system prompt, the user waits for the command to complete.


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/73/1D/wKiom1X1SsfTRA-IAAI21oGyV_o806.jpg "title=" 1.png " alt= "Wkiom1x1ssftra-iaai21ogyv_o806.jpg"/>

System batch operation and maintenance management tool Pexpect

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.