1. 개요
스크립트 실행시 원격 서버에 접속해서 명령어를 실행하고 싶을 때
2. Source Code
expect <<EOF
# timeout 시간
set timeout 1
# 원격 서버 접속
spawn ssh -o StrictHostKeyChecking=no $ID@$HOSTIP
expect {
"Name or service not known" { exit 1 }
"No route" { exit 1 }
"try again" { exit 1 }
"yes/no" { send "yes\r"; exp_continue }
"\'s password:" { send "$PSWD\r"; exp_continue }
"UNIX 암호:" { send "$PSWD\r"; exp_continue }
}
# 실행할 명령어 / 실행 결과는 내 창에 안 뜨기 때문에 결과를 보고 싶으면 Redirect
send "/root/test.sh > test.log\r"
# exit 전에 대기시간이 없으면 명령 실행 안 됨.
sleep 0.5
send "exit \r"
EOF
}