1. 程式人生 > >ssh自動登入的shell,用expect

ssh自動登入的shell,用expect

ssh連線遠端伺服器,涉及到輸入密碼過程,因此就需要互動,要用到expect.
1.MAC機器上安裝expect,是用homebrew安裝。
          brew  install  expect
2.明確expect命令的路徑
     which expect  ( /usr/local/bin/expect)
3.這個expect搞定了,就可以寫指令碼,指令碼由expect解釋執行




#!/usr/local/bin/expect
set time 30

#讀取第一個引數,置為AAA
set AAA [lindex $argv 0]

if {$AAA=="test"} {
                send_user "請求連線的是騰訊雲-測試伺服器\n"

                spawn ssh
[email protected]

                expect "password:"
                send "Q1w2e3r4\r"
                interact
} elseif {$AAA == "pro"} {
                send_user "請求連線的是騰訊雲-生產伺服器\n"

                spawn ssh [email protected]
                expect "password:"
                send "Xnf93xRFWe9HA\r"
                interact
} else {
                send_user "引數必須是 pro 或者 test,指令碼結束\n"
}