暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Linux 一键配置 ssh 互信

原创 Jeff 2023-11-15
568

Linux 一键配置 ssh 互信

配置各种数据库集群需要,总是不可避免需要配置ssh互信以方便操作,手动配置容易遗漏,做成脚本提升效率。

#!/bin/bash # ssh_trust.sh clusternodenum=2 host1=Jeff-Test01 host1ip=192.168.56.6 host2=Jeff-Test02 host2ip=192.168.56.7 host3= host3ip= rootpw='Jeff_Test01' # 1.2 定义功能 split1() { echo -e "\033[1;40;33m\n\n ===================================== $(date +'%F %T') =====================================\n$1\033[0m" } result_err() { echo -e "\033[31m\n (*@ο@*)(*@ο@*)(*@ο@*) $1 (*@ο@*)(*@ο@*)(*@ο@*)\n ===================================== $(date +'%F %T') =====================================\n\033[0m\n" } result_ok() { echo -e "\n\033[32m (*^_^*)(*^_^*)(*^_^*) $1 (*^_^*)(*^_^*)(*^_^*)\n ===================================== $(date +'%F %T') =====================================\n\033[0m\n" } # 3.2 配置集群间 ssh 互信 sleep 2 if [ $clusternodenum -ge 2 ] && [ -n $host2ip ]; then echo "# 3.2 配置集群间 ssh 互信" for host in $host1ip $host2ip $host3ip;do sed -i "/^$host\|trust/d" /etc/hosts done echo "# host ssh trust add on $(date +'%F %T') $host1ip $host1 $host2ip $host2 $host3ip $host3" >>/etc/hosts sed -i '/^\s*$/d' /etc/hosts if [ $(grep $host2 ~/.ssh/authorized_keys 2>/dev/null | wc -l) -lt 1 ]; then echo "# Use /tmp/sshkey.sh to setup ssh trust" cat >/tmp/sshkey.sh<<sshkey #!/bin/bash node1=\$1 node2=\$2 node3=\$3 # if [ -f ~/.ssh/id_rsa ];then rm -rf ~/.ssh/id_rsa ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys ~/.ssh/known_hosts fi ssh-keygen -q -P "" -f ~/.ssh/id_rsa for node in \${node1} \${node2} \${node3} do if [ "`hostname`" == "\$node" ]; then ssh-copy-id -o stricthostkeychecking=no \$node cat ~/.ssh/id_rsa.pub > ~/.ssh/authorized_keys else ssh \$node "if [ -f ~/.ssh/id_rsa ];then rm -rf ~/.ssh/id_rsa ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys ~/.ssh/known_hosts; fi" ssh-copy-id -o stricthostkeychecking=no \$node ssh \$node 'ssh-keygen -q -P "" -f ~/.ssh/id_rsa' scp -rp \$node:~/.ssh/id_rsa.pub ~/auth.\$node fi done if [ $(ls ~/auth.*|wc -l) -ge 1 ];then cat ~/auth.* >> ~/.ssh/authorized_keys rm -rf ~/auth.* else echo "# Not found file ~/auth.*" exit 3 fi cat ~/.ssh/authorized_keys echo "# Transfer authorized_keys" for node in \${node1} \${node2} \${node3} do if [ "`hostname`" != "\$node" ]; then scp -rp ~/.ssh/authorized_keys \$node:~/.ssh/authorized_keys scp -rp ~/.ssh/known_hosts \$node:~/.ssh/known_hosts fi done exit 0 sshkey chmod +x /tmp/sshkey.sh expect <<EOF set timeout 300 spawn sh /tmp/sshkey.sh $host1 $host2 $host3 expect { "(y/n)" {send "y\r"; exp_continue} "(yes/no)" {send "yes\r"; exp_continue} "*assword:" {send "$rootpw\r"; exp_continue} } expect eof EOF fi source ~/.bashrc fi # Test sshUserSetup if [ $clusternodenum -ge 2 ] && [ -n "$host2ip" ]; then echo "# Test sshUserSetup" for ip in $host1ip $host2ip $host3ip; do ssh -o StrictHostKeyChecking=no $ip date if [ $? -ne 0 ]; then result_err "# Test $ip sshUserSetup Maybe Failed" exit 3 else result_ok "# Test $ip sshUserSetup, successful" fi done fi
最后修改时间:2023-11-15 09:34:55
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

文章被以下合辑收录

评论