spawn與except組合可達到遠程登錄設備執行命令的作用
下面是登錄設備的一段代碼
#!/usr/bin/expect -f
user=root
host=1.1.1.1
password=root
spawn $user@$host
set timeout 60
except {
"(yes/no)?" {
send "yes\n"
expect "*assword:*"
send "$password\n"
}
"assword:" {
send "$password\n"
} timeout {
exit
} eof{
exit
}
}