20181225 Linux Shell Bash環境下自動化創建ssh互信腳本 1. 我的Blog 博客園 https://www.cnblogs.com/piggybaba/ 個人網站 http://piggybaba.cn GitHub https://github.com/AndyYHM/W ...
20181225-Linux Shell Bash環境下自動化創建ssh互信腳本
1. 我的Blog
博客園 https://www.cnblogs.com/piggybaba/
個人網站 http://piggybaba.cn
GitHub https://github.com/AndyYHM/Writing/
2. 簡介信息
摘要:Linux下,自動化創建SSH互信腳本
Author: [email protected]
Date: 20181225
關鍵字:Shell腳本, ssh, ssh trust ,auto,SSH互信,/bin/bash
3. 腳本輸出效果
單一節點上,用戶python,執行腳本後,輸入三台節點python用戶密碼,自動化創建SSH互信關係
$ sh SSH_Trust.sh
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
python@node11's password:
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
python@node12's password:
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
python@node13's password:
Transfer authorized_keys
authorized_keys 100% 1185 1.2KB/s 00:00
known_hosts 100% 537 0.5KB/s 00:00
authorized_keys 100% 1185 1.2KB/s 00:00
known_hosts 100% 537 0.5KB/s 00:00
4. 功能說明
- 預設支持3節點自動化創建SSH互信關係
- 支持多節點自動化創建SSH互信關係
5. 使用說明
- 需要提前編輯好/etc/hosts文件
- 用戶名所有主機設置為一致
- 使用前編輯腳本"config to do"部分,節點hostname和用戶名
- othernodes參數需以空格” “隔開;
- 執行腳本後,需逐一輸入節點用戶的密碼
- 若主機節點數規模龐大,建議使用
expect
工具,另行編輯腳本;
6. 腳本內容
#!/usr/bin/env bash
#########################################
# Author: [email protected]
# Date: 20181225
# Key Word : Shell腳本, ssh, ssh trust ,auto,SSH互信,/bin/bash
#########################################
#
## Config to do
#
node1=node11
node2=node12
node3=node13
othernodes=
user=test
#
## Please Don't edit content below
#
ssh-keygen -q -P "" -f $HOME/.ssh/id_rsa > /dev/null
for node in ${node1} ${node2} ${node3} ${othernodes}
do
if [ "`hostname`" == "$node" ]; then
ssh-copy-id -o stricthostkeychecking=no $user@$node > /dev/null
else
ssh-copy-id -o stricthostkeychecking=no python@$node > /dev/null
ssh $node 'ssh-keygen -q -P "" -f $HOME/.ssh/id_rsa' > /dev/null
scp -rp $node:$HOME/.ssh/id_rsa.pub ./auth.$node > /dev/null
fi
done
cat ./auth.* >> $HOME/.ssh/authorized_keys
rm -rf ./auth.*
echo "Transfer authorized_keys"
for node in ${node1} ${node2} ${node3} ${othernodes}
do
if [ "`hostname`" != "$node" ]; then
scp -rp $HOME/.ssh/authorized_keys $node:$HOME/.ssh/authorized_keys
scp -rp $HOME/.ssh/known_hosts $node:$HOME/.ssh/known_hosts
fi
done
exit 0