1. 程式人生 > >Mac配置跳板機自動登入

Mac配置跳板機自動登入

#!/usr/bin/expect
set machine [lindex $argv 0]
set kdauth [ exec oathtool --totp -b <這裡寫你的Google祕鑰>]
#solve the window size bug
trap {
        set rows [stty rows]
        set cols [stty columns]
        stty rows $rows columns $cols < $spawn_out(slave,name)
} WINCH
spawn ssh <這裡寫你的登入賬號>@<這裡寫你的跳板機IP>
expect {
        "Verification*" { send "$kdauth\r"; exp_continue }
        "Pass*" { send "<這裡寫你的登入密碼>\r"; exp_continue }
        "*choice*" { send "$machine\r"; interact }
}

1. 複製以上程式碼到 .exp 格式檔案,修改對應的配置,需要安裝一下 oath-toolkit

brew install oath-toolkit
* 如果你的 Mac 還沒有安裝 Homebrew,參考文件:https://brew.sh/index_zh-cn.html


完成以上步驟後,執行

expect <你的指令碼名稱>.exp <這裡是一個引數,登入跳板機後執行的第一條命令,可以為空>

為提高效率可以配置一下簡易命令

echo "alias serv='expect <你的指令碼名稱>.exp <這裡是一個引數,登入跳板機後執行的第一條命令,可以為空>'" >> ~/.bash_profile && source ~/.bash_profile

之後每次登入時使用簡易命令就可以了

serv