How to use Python Pexpect to Automate Linux Commands?
Pexpect is a Python library for spawning child processes and controlling them automatically. Pexpect can be used to automate interactive applications such as SSH, FTP, password, telnet, etc. Pexpect works by spawning child processes and responding to expected patterns.
Pexpect can be installed by using the following command.
pip3 install pexpect
Automating Linux commands:
It can be done in different ways:
- Using run() method
- Using spawn class
- Using sendline() method
After the installation let’s see how to automate the Linux commands.
Method 1: Using run() method
The pexpect.run() method can be called to execute a command and return its output. This function can be used as a replacement for os.system.
Method 2: Using spawn class
Spawn class is the main interface to start a new child process and control it. The string inside spawn can be replaced by the shell command that needs to be executed.
Syntax: pexpect.spawn(“rm ./dev”)
The important methods of pexpect.spawn class are expect().
Syntax: expect(pattern, timeout=-1, searchwindowsize=-1, async_= False)
This method waits for the child process to return a given string. The pattern specified in the except method will be matched all through the string. The timeout is used to raise pexpect.TIMEOUT. The searchwindowsize parameter is used to set the maxread attribute of the class. Set async_ = True when creating a non-blocking application.
The example prints the index that matches the child process.
Method 3: Using sendline(s = ” “)
This method writes the string to the child process and also returns the number of bytes written. It appears to the child process as someone is typing from the terminal.
Let’s see a complex example for better understanding. Here where will use the FTP client to login into ftp.us.debian.org and download welcome.msg file and print the file. Here we are going to use an FTP client to login and download files from a remote machine and then print.
- Import pexpect.
- spawn a child with pexpect.spawn(‘ftp ftp.us.debian.or’).
- FTP client asks for the user name. Match the pattern with expect(‘Name .*: ‘)
- Send the username to the child process with sendline method.
- Then the FTP client asks for the password. Match the pattern with expect(‘Password: ‘)
- Then the serve logged in. Then you can download files.
- Detect whether you can send the command to the FTP server by the presence of ‘ftp> ‘.
- Then send the command by sendline(‘get welcome.msg’), this just downloads the file to the local machine.
- Then close the FTP client by using the ‘bye’ command.
- Finally, print all the interactions with the child process with the child.before. It returns a byte string, decodes it before printing it.
- Use pexpect.run to print the content of the downloaded file from the FTP server.