linux 使用 expect 使用面交互登錄ssh 和 telnet

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   //退出自動化交互模式

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章