The pexpect
module in Python is used for controlling and automating interactive applications. It allows you to spawn child processes, send input to them, and expect specific outputs. This module is particularly useful for automating command-line interactions and testing scripts that require interaction with external programs.
1. Installing Pexpect
To use pexpect
, you need to install it. You can do so using pip:
pip install pexpect
2. Basic Usage of Pexpect
The core functions of pexpect
involve spawning a process, sending commands, and waiting for specific outputs. Here’s a basic example of how to use pexpect
:
2.1 Spawning a Process
import pexpect
# Spawn a child process (e.g., starting a shell)
child = pexpect.spawn('bash')
# Send a command to the process
child.sendline('echo Hello, World!')
# Expect the output and print it
child.expect('Hello, World!')
print(child.before.decode()) # Output before the expected pattern
In this example, pexpect.spawn()
starts a new Bash shell. The sendline()
method sends a command to the shell, and expect()
waits for the output to match the specified pattern.
3. Handling Prompts and Passwords
One common use of pexpect
is automating interactions with programs that require user input, such as SSH login. Here’s an example of handling a password prompt:
import pexpect
# Spawn an SSH process
child = pexpect.spawn('ssh user@hostname')
# Expect a password prompt
child.expect('password:')
child.sendline('your_password')
# Expect the shell prompt after login
child.expect('$')
print(child.before.decode())
In this example, pexpect
waits for a password prompt and sends the password. It then waits for the shell prompt to confirm a successful login.
4. Working with Multiple Patterns
Sometimes, you need to handle multiple patterns or conditions. pexpect
supports this using regular expressions:
import pexpect
child = pexpect.spawn('some_command')
# Expect one of multiple patterns
index = child.expect([r'Pattern1', r'Pattern2', pexpect.EOF, pexpect.TIMEOUT])
if index == 0:
print("Matched Pattern1")
elif index == 1:
print("Matched Pattern2")
elif index == 2:
print("End of File")
elif index == 3:
print("Timeout")
In this example, expect()
looks for either of two patterns or handles EOF and timeout situations.
5. Using Timeout and Custom Expect Patterns
pexpect
allows setting a timeout for waiting for patterns and using custom regular expressions:
import pexpect
import re
child = pexpect.spawn('some_command', timeout=10)
# Expect a pattern with a custom regular expression
child.expect(r'CustomPatternd+', timeout=15)
# Use a regex to match complex patterns
custom_regex = re.compile(r'd+ errors')
child.expect(custom_regex)
6. Summary
The pexpect
module is a powerful tool for automating interactions with command-line applications and scripts. It allows for spawning processes, sending commands, and handling outputs and prompts. By using pexpect
, you can streamline tasks that involve interactive command-line operations and automate testing scenarios effectively.