Overview
`expect` automates interactive sessions by waiting for predefined patterns (e.g., prompt strings) and then sending specific strings (e.g., user input). This allows complex tasks to be performed without manual intervention.
Key Features
- Tcl-based scripting language extension
- Automates interaction with interactive programs (e.g., ssh, ftp, telnet)
- Provides prompt pattern matching and response sending capabilities
- Useful for automating repetitive and manual tasks
Key Options
These options control the execution of the `expect` command itself, distinct from the `expect` and `send` commands used within scripts.
Execution Control and Debugging
Generated command:
Try combining the commands.
Description:
`expect` Executes the command.
Combine the above options to virtually execute commands with AI.
Installation
`expect` is not included by default in most Linux distributions and needs to be installed via a package manager.
Debian/Ubuntu
sudo apt update && sudo apt install expect
Install `expect` using the APT package manager.
RHEL/CentOS/Fedora
sudo dnf install expect
Install `expect` using the DNF (or Yum) package manager.
Usage Examples
`expect` scripts typically have a `.exp` extension and start with `#!/usr/bin/expect` to be executed by the `expect` interpreter.
Simple Prompt Auto-Response
#!/usr/bin/expect
set timeout 10
spawn sh -c "echo -n 'Proceed? (y/n) '
read response"
expect "Proceed? (y/n) "
send "y\r"
expect eof
A script that automatically answers 'y' to a prompt like `Proceed? (y/n)`. (Uses `sh` for demonstration)
SSH Login Automation (Security Warning)
#!/usr/bin/expect
set timeout 20
set username "your_user"
set hostname "your_host"
set password "your_password"
spawn ssh $username@$hostname
expect "password:"
send "$password\r"
expect "$"
send "ls -l\r"
expect "$"
send "exit\r"
expect eof
An example of automatically logging into a remote server via SSH and executing a command. **Including passwords directly in scripts is highly insecure and not recommended in production environments.**
passwd Command Automation (Security Warning)
#!/usr/bin/expect
set timeout 10
set old_pass "old_password"
set new_pass "new_password"
spawn passwd
expect "Current password:"
send "$old_pass\r"
expect "New password:"
send "$new_pass\r"
expect "Retype new password:"
send "$new_pass\r"
expect eof
An example of automatically changing a password using the `passwd` command. **Hardcoding passwords in scripts is extremely insecure.**
Tips & Precautions
`expect` scripts are powerful, but some precautions should be considered for security and stability.
Key Tips
- **Password Security**: Hardcoding passwords directly into scripts is very dangerous. Consider using environment variables, secure configuration files, or other security mechanisms like `ssh-agent`.
- **Timeout Setting**: Use the `set timeout` command to set the maximum time `expect` will wait for a specific pattern. The default is 10 seconds, and you can set it to infinite waiting with `set timeout -1`.
- **Accurate Pattern Matching**: `expect` supports regular expressions. Prompts may not match exactly, so use flexible patterns or leverage the `*` wildcard.
- **Debugging**: Running `expect -d your_script.exp` in debug mode allows you to see in detail which patterns `expect` is waiting for and which strings it is sending.
- **Using `interact`**: The `interact` command can be used to hand control back to the user at a specific point during script execution.