#!/bin/bash read -p "what is your name:" NAME read -p "How old are you:" AGE read -p "which object:" OBJ read -p "Are you OK:" OK echo$NAME is $AGE is old study $OBJ feel $OK
应答脚本 auto_answer.sh
#!/usr/bin/expect settimeout 1 ##通过索引的方式获取所传的参数 set NAME [ lindex $argv 0 ] set AGE [ lindex $argv 1 ] set OBJ [ lindex $argv 2 ] set FEEL [ lindex $argv 3 ] ##监控的问题脚本 spawn /mnt/expect.sh ##向问题脚本发送参数 expect{ "name"{ send "$NAME\r";exp_continue } "old"{ send "$AGE\r";exp_continue } "objective"{ send "$OBJ\r";exp_continue } "ok"{ send "$FEEL\r"} } expect eof
auto_answer.sh zx 23 123 happy
脚本练习:自动连接目标主机当目标主机网络不通时报错
vim auto_ssh.sh
#!/usr/bin/expect ##通过索引的方式获取所传的参数 set ip [ lindex $argv 0 ] set password [ lindex $argv 1 ] ##开启一个进程 spawn ssh root@$ip expect{"yes/no"{ send "yes\r";}"password"{ send "$password\n"} } #持久连接远程服务器 interact