expect는 자동화된 상호작용을 처리하는 스크립트 언어이다.
보통 SSH, FTP, Telnet, 패스워드 입력 등의 자동화에 사용된다.
spawn은 expect 스크립트에서 특정 프로그램을 실행하는 명령어로,
이 프로그램과의 상호작용을 관리할 수 있도록 해준다.
ex) SH 자동 로그인을 수행하는 expect 스크립트
#!/usr/bin/expect # SSH로 원격 서버 접속 자동화 예제
set timeout 10 # 타임아웃 설정 (초)
set user "your_user" # 사용자명
set host "your_host" # 접속할 호스트
set password "your_password" # 비밀번호
spawn ssh $user@$host expect "password:" send "$password\r" expect "$ " # 로그인 후 프롬프트 대기
send "echo 'Login Successful'\r"
send "exit\r"
expect eof
ex) sudo 명령어 실행 시 패스워드를 자동 입력
#!/usr/bin/expect
set timeout 5
set password "your_sudo_password"
spawn sudo ls /root expect "password for"
send "$password\r"
expect eof
ex) FTP 서버에 자동 로그인
#!/usr/bin/expect
set timeout 10 # expect Password의 문자열을 기다리는데 대기하는 시간
set user "ftp_user"
set password "ftp_password"
set host "ftp.example.com"
spawn ftp $host expect "Name*:"
send "$user\r"
expect "Password:"
send "$password\r"
expect "ftp>"
send "bye\r"
expect eof
| Linux - Shebang (0) | 2025.03.16 |
|---|---|
| Linux - crontab (0) | 2025.03.09 |
| Linux - find (0) | 2025.03.02 |
| Linux - grep (0) | 2025.03.02 |
| Linux - TOP (0) | 2025.01.12 |