一、創建文件abbr,用以保存目標服務器簡稱與全稱的對應關係,例如:bash
yang@fengtao ~ $ more abbr #product pp1 p1.prodcut1.server.com pp2 p2.prodcut1.server.com pp3 p3.prodcut1.server.com pp4 p4.prodcut1.server.com
二、創建可執行文件autologin.sh,例如:服務器
yang@fengtao ~ $ more autologin.sh #!/usr/bin/expect set target [lindex $argv 0] spawn ssh $target expect { "Enter passphrase for key '/home/q/home/fengtao.yang/.ssh/id_rsa':" { send "yourpassword\n" } } interact
其中,你本身須要替換的有如下兩處:ssh
Enter passphrase for key '/home/q/home/fengtao.yang/.ssh/id_rsa': 這裏須要替換爲提示你輸入密碼的提示語
yourpassword 你登陸服務器須要的密碼
三、創建可執行文件lg,用以接收參數,而後調用autologin.sh以實現自動登陸:spa
yang@fengtao ~ $ more lg #!/bin/bash target=$(grep $1 abbr | awk '{print $2}') echo $target ./autologin.sh $target
四、進行登陸:code
yang@fengtao ~ $ ./lg pp1