expect 【簡單小白例子】
作用:實現制動交互功能,在一些需求中使用expect 可以省去配置遠程框架的麻煩
下面是登錄H3C路由器通過telnet 的例子 :
#!/usr/bin/expect -f
set timeout 30
spawn telnet xxxxx
expect "*login"
send "xxxxx\r"
expect "Password"
send "xxxxx\r"C
expect "<H3C>"
send "?\r"
expect "<H3C>"
send "q\r"
interact
下面是登錄ssh的的例子:
#!/usr/bin/expect -f
set timeout30
spawn ssh [email protected] -p8898
expect "password"
send "password199503zxc123...\r"
expect "#"
send "ls\r"
expect "#"
send "exit\r"
interact
解釋:
set timeout 30 等待超時
#/usr/bin/expect -f #使用這個解析器區執行下面代碼 並且帶上-f這個參數給這個解析器
spawn ssh [email protected] #使用expect 的spawn 打開一個窗口這個窗口的輸入輸出在expect 中可以操作他
expect "password" //等待輸入密碼
send "test\r" //模仿鍵盤輸入 \r 這個時換行
interact //退出自動化交互模式