升級MAC系統後,發現用於MAC終端ssh服務器的登陸腳本沒法正常執行了,表現爲:須要手動輸入密鑰密碼,因而從新整理一下,恢復正常,在此記錄一下:服務器
#!/usr/bin/expect -f
spawn ssh-add -D
expect "*removed."
spawn ssh-add -l
expect "*identities."
spawn ssh-add /Users/alsoalso/keys/RSA_Key01
expect "*RSA_Key01" {send "密鑰密碼\r";}
expect "Identity added:"
spawn ssh SSH用戶@IP地址 -pSSH端口 -i /Users/alsoalso/keys/RSA_Key01
expect "*yes/no*" { send "yes\n"; exp_continue }
interactssh
說明一下:ide
#!/usr/bin/expect -f 指定expect, 須要安裝加密
spawn ssh-add -D 刪除已add的密碼,mac終端對添加的條目有限制,因此每次都初始化一下spa
expect "*removed." 根據上一步的執行反饋,獲取關鍵字用於執行下一步的條件ip
spawn ssh-add -l 列出密鑰列表,非必須rem
expect "*identities." 根據上一步的執行反饋,獲取關鍵字用於執行下一步的條件,非必須it
spawn ssh-add /Users/alsoalso/keys/RSA_Key01 添加密鑰文件登錄
expect "*RSA_Key01" {send "密鑰密碼\r";} 輸入密鑰密碼終端
expect "Identity added:" 判斷密鑰是否登陸成功
spawn ssh SSH用戶@IP地址 -pSSH端口 -i /Users/alsoalso/keys/RSA_Key01 鏈接服務器
expect "*yes/no*" { send "yes\n"; exp_continue } 若是首次鏈接服務器,會有確認
interact 命令執行完成後保持登陸在遠程服務器上
20180322更新:直接切換爲root用戶
#!/usr/bin/expect -f
spawn ssh-add -D
expect "*removed."
spawn ssh-add -l
expect "*identities."
spawn ssh-add /Users/alsoalso/keys/RSA_Key00
expect "*RSA_Key00" {send "mima\r";}
expect "Identity added:" { spawn ssh sshusername@ip -pssh端口 -i /Users/alsoalso/keys/RSA_Key00 }
expect "sshusername@hostname ~" { send "su\r" }
expect "密碼" { send "rootmima\r" }
expect "*yes/no*" { send "yes\r"; exp_continue }
interact