測試系統版本:CentOS 6.4
作用:跳板機遠程被管理服務器,通常使用秘鑰登錄,實現此操作時,拷貝秘鑰仍然需要輸入一次登錄密碼,此腳本實現自動生成密鑰對并全程無交互批量拷貝公鑰到被管理服務器,并自動禁用被管理端的ssh密碼登錄,使其只允許秘鑰登錄(免密碼秘鑰登錄實現,參考:http://lingyi.blog.51cto.com/2837715/1763747)。
實現:借助 ssh-copy-id 腳本的拷貝公鑰功能和expect自動應答工具,實現無交互批量拷貝公鑰的功能。(附件帶ssh-copy-id腳本,expect工具需提前自行安裝)
使用:兩個腳本,一個是系統自帶的ssh-copy-id腳本,一個是用來實現自動應答和禁用ssh密碼登錄的腳本。 使用時需要提供一配置文件,文件包含遠程主機的 ip、用戶名和密碼信息。詳情見演示和代碼注釋。
演示:

配置文件為ssh_keygen.cfg(默認, 可修改腳本),提供ip 用戶名和密碼信息,使用完后腳本會自動刪除此文件。從上圖可見,1.60主機可SSH等錄但需要密碼。
由于之前已經存在一密鑰對,所以會提示是否重新生成,其他操作全程無需應答。

再次連接1.60主機便不再需要密碼,并且已經禁用了被管理主機的SSH密碼登錄功能,見下圖。


代碼專區:
#系統自帶腳本,僅僅注釋了部分顯示信息功能
#!/bin/sh
# Shell script to install your public key on a remote machine
# Takes the remote machine name as an argument.
# Obviously, the remote machine must accept password authentication,
# or one of the other keys in your ssh-agent, for this to work.
ID_FILE="${HOME}/.ssh/id_rsa.pub"
if [ "-i" = "$1" ]; then
shift
# check if we have 2 parameters left, if so the first is the new ID file
if [ -n "$2" ]; then
if expr "$1" : ".*\.pub" > /dev/null ; then
ID_FILE="$1"
else
ID_FILE="$1.pub"
fi
shift # and this should leave $1 as the target name
fi
else
if [ x$SSH_AUTH_SOCK != x ] ; then
GET_ID="$GET_ID ssh-add -L"
fi
fi
if [ -z "`eval $GET_ID`" ] && [ -r "${ID_FILE}" ] ; then
GET_ID="cat ${ID_FILE}"
fi
if [ -z "`eval $GET_ID`" ]; then
echo "$0: ERROR: No identities found" >&2
exit 1
fi
if [ "$#" -lt 1 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
echo "Usage: $0 [-i [identity_file]] [user@]machine" >&2
exit 1
fi
{ eval "$GET_ID" ; } | ssh $1 "exec sh -c 'umask 077; test -d ~/.ssh || mkdir ~/.ssh ; cat >> ~/.ssh/authorized_keys && (test -x /sbin/restorecon && /sbin/restorecon ~/.ssh ~/.ssh/authorized_keys >/dev/null 2>&1 || true)'" || exit 1
#cat <<EOF
#Now try logging into the machine, with "ssh '$1'", and check in:
#
# .ssh/authorized_keys
#
#to make sure we haven't added extra keys that you weren't expecting.
#
#EOF#======================
# LY
# ------------------
# Copyright 2016.4.14, LingYi (lydygly@163.com) QQ:1519952564
# "Free password login, batch copy keys"
#創建密鑰對,若有其他特殊需要,請自行生成,并跳過覆蓋操作或注釋掉此部分代碼
#create the secret key.
ssh-keygen -t rsa -f /root/.ssh/id_rsa -N ""
#定義配置文件,沒有對文件做存在檢查,請自行創建并按格式輸入相關信息
ssh_keygen_conf=ssh_keygen.cfg
ssh_keygen_conf_infor=$( grep -E -v '(^ *#|^$)' $ssh_keygen_conf )
#cofigure file should be like this:
# IP USER PWD
#拷貝秘鑰的函數,其調用ssh_copy_id.sh腳本,若此腳本已經改名或改變了路徑,請相應的修改此函數
function copy_secret_key()
{
#請自行安裝expect工具
/usr/bin/expect -c "
#調用ssh_copy_id.sh腳本
spawn bash ssh_copy_id.sh ${2}@$1
expect {
\"(yes/no)?\"
{
send \"yes\r\"
exp_continue
}
\"password:\"
{
send \"${3}\r\"
exp_continue
}
\"password:\"
{
send \"${3}\r\"
exp_continue
}
\"password:\"
{
send \"${3}\r\"
interact
}
}
"
}
for(( i=1; i<=$( echo "$ssh_keygen_conf_infor" | wc -l ); i++ ))
do
copy_secret_key $( echo "$ssh_keygen_conf_infor"|sed -n "${i}p" )
[[ $? -ne 0 ]] && continue
#禁用被管理服務器的SSH密碼登錄
ssh $(echo "$ssh_keygen_conf_infor"|sed -n "${i}p"|awk '{print $1}' ) \
"sed -i 's/PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config && \
echo Disable password login. && service sshd restart"
done
#刪除配置文件
rm -f $ssh_keygen_conf代碼下載鏈接
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。