一般的なタスクに対する実用的な expect スクリプトの作成
このステップでは、システム管理者がよく自動化する必要がある一般的なタスクに対して、実用的な expect スクリプトを作成します。ファイル操作、ユーザーインタラクション、およびシステム監視に焦点を当てます。
expect を使用したファイル転送の自動化
scp コマンドを使用してファイルの転送を自動化する expect スクリプトを作成しましょう。この環境では実際のファイル転送を実行できないため、シミュレートします。
cd ~/project
nano file_transfer.sh
SCP のようなファイル転送をシミュレートするために、次の内容を入力します。
#!/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
ファイルを保存し、実行可能にします。
chmod +x ~/project/file_transfer.sh
次に、このファイル転送を自動化する expect スクリプトを作成しましょう。
cd ~/project
nano file_transfer.exp
次の内容を入力します。
#!/usr/bin/expect -f
## 変数を設定
set timeout 10
set source_file "local_file.txt"
set destination "user@remote:/path/to/destination/"
set password "transfer123"
## ダミーのソースファイルを作成
spawn bash -c "echo 'This is a test file' > $source_file"
expect eof
## ファイル転送シミュレーションを開始
spawn ./file_transfer.sh $source_file $destination
## パスワードプロンプトを処理
expect "Password: "
send "$password\r"
## 転送の進捗を監視
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!"
## ダミーファイルをクリーンアップ
spawn bash -c "rm $source_file"
expect eof
ファイルを保存し、実行可能にします。
chmod +x ~/project/file_transfer.exp
ファイル転送自動化スクリプトを実行します。
cd ~/project
./file_transfer.exp
出力例:
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
expect を使用したユーザー作成の自動化
次に、ユーザー作成を自動化する expect スクリプトを作成しましょう。ここでも、このプロセスをシミュレートします。
cd ~/project
nano create_user.sh
次の内容を入力します。
#!/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."
ファイルを保存し、実行可能にします。
chmod +x ~/project/create_user.sh
次に、ユーザー作成を自動化する expect スクリプトを作成しましょう。
cd ~/project
nano create_user.exp
次の内容を入力します。
#!/usr/bin/expect -f
## 変数を設定
set timeout 10
set username "testuser"
set password "P@ssw0rd123"
set add_sudo "yes"
## ユーザー作成ユーティリティを開始
spawn ./create_user.sh
## ユーザー名プロンプトを処理
expect "Please enter new username: "
send "$username\r"
## パスワードプロンプトを処理
expect "Please enter password for $username: "
send "$password\r"
## パスワード確認プロンプトを処理
expect "Please confirm password: "
send "$password\r"
## ユーザー作成の確認を待機
expect "User $username created successfully."
## sudo プロンプトを処理
expect "Do you want to add this user to the sudo group? (yes/no): "
send "$add_sudo\r"
## sudo に追加することを選択した場合、確認を待機
if {$add_sudo == "yes"} {
expect "User $username added to sudo group."
}
## 完了を待機
expect "User setup completed."
puts "\nUser creation automation completed successfully!"
ファイルを保存し、実行可能にします。
chmod +x ~/project/create_user.exp
ユーザー作成自動化スクリプトを実行します。
cd ~/project
./create_user.exp
出力例:
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!
実用的な expect スクリプトの理解
作成した実用的なスクリプトは、実際の自動化に関するいくつかの重要な概念を示しています。
- シーケンシャルインタラクション: 両方のスクリプトは、定義された一連のプロンプトと応答に従います。
- 進捗監視: ファイル転送スクリプトは進捗を監視し、ユーザーフレンドリーな更新を提供します。
- 条件付きロジック: ユーザー作成スクリプトは、sudo オプションを処理するために条件付きロジックを使用します。
- 環境のセットアップとクリーンアップ: ファイル転送スクリプトは、テストファイルを作成し、クリーンアップします。
これらのテクニックは、次のような多くの一般的なシステム管理タスクを自動化するために適用できます。
- リモートバックアップ
- ソフトウェアのインストール
- システム構成
- バッチ操作
expect を習得することで、手動での介入が必要になる可能性のある複雑な対話型プロセスを自動化し、時間を節約し、人的ミスの可能性を減らすことができます。