概述
使用SSH登錄服務器,通常分為兩步:
- #ssh username@hostname
- 系統(tǒng)提示輸入密碼,用戶輸入密碼;
登錄成功
今天要討論的如何讓SSH自動輸入密碼。
開始
-設置主機短名稱
#vim ~/.ssh/conf ,其內容如下:
- Host host1 HostName IP地址 User 帳號 ServerAliveInterval 300 -
--通過短名稱登錄
#ssh host1
此時還會提示輸入密碼。
-自動輸入密碼
利用expect完成自動輸入。
注:Expect是Linux實現(xiàn)與系統(tǒng)自動交互工具
--編寫expect腳本
# sudo vim login ,內容如下:
- 1 #!/usr/bin/expect 2 3 set channel [lindex $argv 0] 4 5 6 if { "$channel"=="host1" || "$channel" == "host2" || "$channel" == "host3" } { 7 set passwd "1234" 8 } else { 9 set passwd "6789" 10 } 11 12 spawn ssh $channel 13 14 15 expect { 16 password: { 17 send "$passwd\r" 18 exp_continue 19 } 20 PASSCODE: { 21 send "580928" 22 interact 23 } 24 timeout { 25 send_user "connection timed out\n" 26 exit 27 } 28 eof { 29 send_user "connection to host failed: $expect_out(buffer)" 30 exit 31 } 32 } -
--登錄服務器
#./login host1
應該可以成功登錄了^^
一條命令就可以登錄系統(tǒng)了。