Centos下 自动化配置SSH免密码登陆
生活随笔
收集整理的這篇文章主要介紹了
Centos下 自动化配置SSH免密码登陆
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
hosts文件,存儲要部署的節點IP地址,其中以#開頭表示注釋掉
192.168.101.52 192.168.101.53 192.168.101.54 192.168.101.55 192.168.101.56?
start.sh文件 在hosts文件中存儲的所有節點上,生成默認的公鑰和私鑰,其中單個節點生成的腳本在keygen文件中
#!/bin/bash# ssh-keygen every node hosts="hosts"if [ -f hosts ] thenecho "Start ssh free" elseecho "Please add hosts file"exit 1 fiif [ $# != 2 ] thenecho "USAGE:$0 user password!"exit 1 elseusername=$1pawdname=$2#login every node and ssh-keygenfor x in `cat hosts | sed "/^#.*/d"`do#echo ${x}if [ -f keygen ]thenexpect keygen ${username} ${x} ${pawdname}elseecho "ssh-keygen not exists"echo "Please check it"exit 1fidone fikeygen文件 ?在單個節點上生成公鑰和私鑰
#!/usr/bin/expect -fset timeout 2set name [lindex $argv 0] set node [lindex $argv 1] set pawd [lindex $argv 2]spawn ssh ${name}@${node} expect {"*yes/no*" {send "yes\n";exp_continue}"*password:" {send "$pawd\r"} }expect "*${name}@${node}*" send "ssh-keygen -t rsa -P ''\r" expect "*ssh/id_rsa):" send "\r" expect {"Overwrite (y/n)?" {send "y\n";exp_continue}"*${name}@${node}*" {send "exit\r"} } expect eof exit備注:請注意,不同版本的提示輸出不一樣,可能需要讀者手動更改keygen 文件中,expect后面的語句
上述幾個文件,就可以完成在指定的節點上,生成密鑰對了,下面來看下后續處理生成密鑰的過程,代碼如下:
?
author.sh ??將start.sh腳本中生成的公鑰寫入到authorized_keys中,并將其上傳到各個節點上,最后更改該文件的權限
?
#!/bin/bash TMP="tmps" rm -rf ${TMP} mkdir ${TMP}TMP=`cd ${TMP};pwd` HOST="hosts" USER="" PAWD="111111"if [ $# == 1 ] thenUSER=$1 elif [ $# == 2 ] thenUSER=$1PAWD=$2 elseecho "USAGE:$0 username"echo "USAGE:$0 username password"exit 1 fiecho ${USER}" "${PAWD}#download id_rsa.pub from every node to the tmp dir count=1 for x in `cat ${HOST}| sed "/^#.*/d"` doexpect download ${USER} ${x} ${PAWD} "${TMP}/${count}"count=`expr $count + 1` done#let all id_rsa.pub into authorized_keys count=1 for x in `ls ${TMP}/*` doif [ count == 1 ]thencat ${x} > /home/${USER}/.ssh/authorized_keyselsecat ${x} >> /home/${USER}/.ssh/authorized_keysficount=`expr $count + 1` done#upload the authorized_keys to every node for x in `cat ${HOST}| sed "/^#.*/d"` doexpect upload ${USER} ${x} ${PAWD} done#chmod 600 to authorized_keys for x in `cat ${HOST}| sed "/^#.*/d"` doexpect priority ${USER} ${x} ${PAWD} done?
download 和upload腳本比較簡單,就當作給讀者的練習題吧,如果需要完整的代碼,歡迎從這里下載:https://github.com/nashiyue/fssh.git
?
轉載于:https://www.cnblogs.com/nashiyue/p/5003276.html
總結
以上是生活随笔為你收集整理的Centos下 自动化配置SSH免密码登陆的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mac osx安装mysql5.7.9
- 下一篇: 自我总结 (三) --(Java Web