概述
使用SSH登錄服務(wù)器屹耐,通常分為兩步:
- #ssh username@hostname
- 系統(tǒng)提示輸入密碼境氢,用戶(hù)輸入密碼轮洋;
登錄成功
今天要討論的如何讓SSH自動(dòng)輸入密碼枉氮。
開(kāi)始
-設(shè)置主機(jī)短名稱(chēng)
#vim ~/.ssh/conf 邀杏,其內(nèi)容如下:
- Host host1 HostName IP地址 User 帳號(hào) ServerAliveInterval 300 -
--通過(guò)短名稱(chēng)登錄
#ssh host1
此時(shí)還會(huì)提示輸入密碼术荤。
-自動(dòng)輸入密碼
利用expect完成自動(dòng)輸入。
注:Expect是Linux實(shí)現(xiàn)與系統(tǒng)自動(dòng)交互工具
--編寫(xiě)expect腳本
# sudo vim login ,內(nèi)容如下:
- 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 } -
--登錄服務(wù)器
#./login host1
應(yīng)該可以成功登錄了^^
一條命令就可以登錄系統(tǒng)了。