일반적인 작업을 위한 실용적인 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 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
파일을 저장하고 실행 가능하게 만듭니다.
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 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!"
파일을 저장하고 실행 가능하게 만듭니다.
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를 마스터하면 수동 개입이 필요한 복잡한 대화형 프로세스를 자동화하여 시간 절약 및 인적 오류 가능성을 줄일 수 있습니다.