Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

System batch operation and maintenance management tool pexpect

Shulou Source: shulou.com Published: 2022-06-03 06:43:41 10月06日 Update

1. Introduce pexpect

Through pexpect, we can automatically interact with ssh,ftp,passwd,telnet and other commands without human intervention.

2. Introduction to the components of pexpect

2.1, spawn class

Function: start and control the sub-application, and its constructor is 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 timeout for waiting for the result, maxread is the maximum number of bytes read from the terminal at a time, and searchwindowsize is the position where the buffer string is matched.

Note: pexpect does not parse metacharacters (redirect >, pipe |, wildcard *) in shell commands, but calling commands with these three special metacharacters as arguments to / bin/bash can solve this problem. For example:

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

Child.expect (pexpect.EOF)

Replace the parameters of the command with a Python list:

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

Child=pexpect.spawn ('/ bin/bash', ['- Clearing Magnum shellflower cmd])

Child.expect (pexpect.EOF)

The debug code is that 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

The expect method defines a matching rule for the output of a subroutine:

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

Pattern represents a string, pexpect.EOF (pointing to the end of the buffer), pexpect.TIMEOUT, and regular expressions.

Send sends the corresponding command to the subroutine, which is equivalent to the standard input keyboard

Send (self,s) sends a command and does not enter

Sendline (self,s='') sends commands, enter

Sendcontrol (self,char) send control characters, such as child.sendcontrol ('c') is equivalent to ctrl+c

Sendeof () sends eof

2.2 > run function

Run is a function of external commands encapsulated with pexpect

Run (command,timeout=-1,withexitstatus=False,events=None,extra_args=None,logfile=None,cwd=None,env=None)

Import pexpect

Pexpect.run ('scp filename user@ip:~',events= {' password':mypassword})

3 、 pxssh

Pxssh encapsulates ssh session operations, providing a more direct mode of operation.

Pxssh common methods: login () establish ssh link, logout () disconnect, prompt () wait for the system prompt, the user waits for the command to finish execution.

Tags: Command function character method output system metacharacter parameter subroutine string mode buffer encapsulation control buffer input maximum special three manual Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology OPPO Reno Shulou Information Linux macOS