Creating Practical expect Scripts for Common Tasks
In this step, we will create practical expect
scripts for common tasks that system administrators often need to automate. We will focus on file operations, user interactions, and system monitoring.
Automating File Transfer with expect
Let us create an expect
script that automates the transfer of a file using the scp
command. Since we cannot perform an actual file transfer in this environment, we will simulate it:
cd ~/project
nano file_transfer.sh
Enter the following content to simulate an SCP-like file transfer:
#!/bin/bash
echo "scp file transfer simulation"
echo "Source file: $1"
echo "Destination: $2"
echo "Password: "
read -s password
if [ "$password" == "transfer123" ]; then
echo "Transferring file..."
echo "0%"
sleep 1
echo "25%"
sleep 1
echo "50%"
sleep 1
echo "75%"
sleep 1
echo "100%"
echo "File transfer completed successfully."
else
echo "Authentication failed."
exit 1
fi
Save the file and make it executable:
chmod +x ~/project/file_transfer.sh
Now, let us create an expect
script to automate this file transfer:
cd ~/project
nano file_transfer.exp
Enter the following content:
#!/usr/bin/expect -f
## Set variables
set timeout 10
set source_file "local_file.txt"
set destination "user@remote:/path/to/destination/"
set password "transfer123"
## Create a dummy source file
spawn bash -c "echo 'This is a test file' > $source_file"
expect eof
## Start the file transfer simulation
spawn ./file_transfer.sh $source_file $destination
## Handle the password prompt
expect "Password: "
send "$password\r"
## Monitor the transfer progress
expect "0%"
puts "Transfer started..."
expect "25%"
puts "Transfer 1/4 complete..."
expect "50%"
puts "Transfer 1/2 complete..."
expect "75%"
puts "Transfer 3/4 complete..."
expect "100%"
puts "Transfer almost done..."
expect "File transfer completed successfully."
puts "File transfer automation completed!"
## Clean up the dummy file
spawn bash -c "rm $source_file"
expect eof
Save the file and make it executable:
chmod +x ~/project/file_transfer.exp
Run the file transfer automation script:
cd ~/project
./file_transfer.exp
Example output:
spawn bash -c echo 'This is a test file' > local_file.txt
spawn ./file_transfer.sh local_file.txt user@remote:/path/to/destination/
scp file transfer simulation
Source file: local_file.txt
Destination: user@remote:/path/to/destination/
Password:
Transferring file...
0%
Transfer started...
25%
Transfer 1/4 complete...
50%
Transfer 1/2 complete...
75%
Transfer 3/4 complete...
100%
Transfer almost done...
File transfer completed successfully.
File transfer automation completed!
spawn bash -c rm local_file.txt
Automating User Creation with expect
Now, let us create an expect
script that automates user creation. Again, we will simulate this process:
cd ~/project
nano create_user.sh
Enter the following content:
#!/bin/bash
echo "User creation utility"
echo "Please enter new username: "
read username
echo "Please enter password for $username: "
read -s password
echo "Please confirm password: "
read -s password_confirm
if [ "$password" != "$password_confirm" ]; then
echo "Error: Passwords do not match."
exit 1
fi
echo "Creating user $username..."
echo "User $username created successfully."
echo "Do you want to add this user to the sudo group? (yes/no): "
read sudo_choice
if [ "$sudo_choice" == "yes" ]; then
echo "Adding $username to sudo group..."
echo "User $username added to sudo group."
fi
echo "User setup completed."
Save the file and make it executable:
chmod +x ~/project/create_user.sh
Now, let us create an expect
script to automate user creation:
cd ~/project
nano create_user.exp
Enter the following content:
#!/usr/bin/expect -f
## Set variables
set timeout 10
set username "testuser"
set password "P@ssw0rd123"
set add_sudo "yes"
## Start the user creation utility
spawn ./create_user.sh
## Handle the username prompt
expect "Please enter new username: "
send "$username\r"
## Handle the password prompt
expect "Please enter password for $username: "
send "$password\r"
## Handle the password confirmation prompt
expect "Please confirm password: "
send "$password\r"
## Wait for the user creation confirmation
expect "User $username created successfully."
## Handle the sudo prompt
expect "Do you want to add this user to the sudo group? (yes/no): "
send "$add_sudo\r"
## If we chose to add to sudo, wait for confirmation
if {$add_sudo == "yes"} {
expect "User $username added to sudo group."
}
## Wait for completion
expect "User setup completed."
puts "\nUser creation automation completed successfully!"
Save the file and make it executable:
chmod +x ~/project/create_user.exp
Run the user creation automation script:
cd ~/project
./create_user.exp
Example output:
spawn ./create_user.sh
User creation utility
Please enter new username:
testuser
Please enter password for testuser:
Please confirm password:
Creating user testuser...
User testuser created successfully.
Do you want to add this user to the sudo group? (yes/no):
yes
Adding testuser to sudo group...
User testuser added to sudo group.
User setup completed.
User creation automation completed successfully!
Understanding Practical expect Scripts
The practical scripts we created demonstrate several important concepts for real-world automation:
- Sequential Interaction: Both scripts follow a defined sequence of prompts and responses.
- Progress Monitoring: The file transfer script monitors progress and provides user-friendly updates.
- Conditional Logic: The user creation script uses conditional logic to handle the sudo option.
- Environment Setup and Cleanup: The file transfer script creates and cleans up test files.
These techniques can be applied to automate many common system administration tasks, such as:
- Remote backups
- Software installations
- System configuration
- Batch operations
By mastering expect
, you can automate complex interactive processes that would otherwise require manual intervention, saving time and reducing the potential for human error.