免密登陆脚本(shell版)

本文介绍了一种在多个节点间快速配置SSH无密码登录的方法。通过两个脚本ssh.sh和ssh1.sh实现单节点及集群环境下的无密码登录设置。具体步骤包括生成密钥、分发公钥并配置授权等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 使用说明: 1.ssh.sh为单节点无密码登录脚本; 2.ssh1.sh为集群无密码登录脚本; 3.将ssh与ssh1放到同一节点上,运行ssh2脚本; 4.整体脚本中expect需要提前安装tcl-与expect-,目前此脚本在外网部署已经写好;在内网安装时,需要手动下载expect-*进行安装,命令为;yum install expect -y 5.所有节点需要有/root/Desktop文件夹

##ssh.sh

#!/bin/bash
#fn_install_ssh(){
#HOST_NAME="cloudera-08"
/usr/bin/expect <<-EOF
spawn ssh-keygen -t rsa -P ""
expect "*rsa):" { 
	send "\n" 
	expect "*y/n)?" {send "y\n"}
	}	
expect eof
EOF
     	sleep 1  
     	cd /root/.ssh/
      	cat id_rsa.pub >> authorized_keys
    	chmod 700 authorized_keys
#}

##ssh1.sh

#!/bin/bash
HOST_NAME=("ab-01" "ab-02" "ab-03" "ab-04" "ab-05" "ab-06" "ab-07")
user="root"
file_need_dir="/root/Desktop/"
file_ssh="ssh.sh"
PASSWORD="123456"
ssh_dir="/root/.ssh/"
author="authorized_keys"
keys="authorized_keysab*"
for hostname in ${HOST_NAME[@]}
do
yum install expect -y
sleep 2
/usr/bin/expect <<-EOF 
spawn scp /root/Desktop/ssh.sh $user@$hostname:/root/Desktop/
expect { 
 	"*s/no)?" {send "yes\r"
 		 expect "*ssword:" {send "$PASSWORD\r"}
	}	
	"*ssword:" {send "$PASSWORD\n\r"}
}
expect eof
EOF
done

for hostname in ${HOST_NAME[@]}
do
/usr/bin/expect <<-EOF
spawn ssh $hostname
expect {
	"*yes/no)?" {send "yes\n\r"
                 expect "*ssword:" {send "$PASSWORD\n\r"}
        }
        "*ssword:" {send "$PASSWORD\n\r"}
	}
expect "*#"
send "cd /root/Desktop\n\r"
expect "*#"
send "./ssh.sh\n\r"
expect eof
EOF
done


for hostname in ${HOST_NAME[@]}
do
/usr/bin/expect <<-EOF
spawn ssh $hostname
expect {
	"*yes/no)?" {send "yes\n\r"
                 expect "*ssword:" {send "$PASSWORD\n\r"}
        }
        "*ssword:" {send "$PASSWORD\n\r"}
	}
expect "*#" {send "scp -r $ssh_dir$author $user@${HOST_NAME[0]}:$ssh_dir$author${hostname}\n\r"
         expect {
              "*yes/no)?" {send "yes\n\r"
                expect "*ssword:" {send "$PASSWORD\n\r"}
                      }
          "*ssword:" {send "$PASSWORD\n\r"}
        }
        } 
expect eof
EOF
done
echo "*****************************·Ö·¢Íê³É**********************************"


/usr/bin/expect <<-EOF
spawn ssh ${HOST_NAME[0]}
expect {
         "*no)?" {send "yes\n"
                  expect "*word:" {send "$PASSWORD\n"}
                  }
         "*word:" {send "$PASSWORD\n\r"}
      }
expect "*#" 
send "cat $ssh_dir$keys >> $ssh_dir$author\n\r" 
interact
expect eof
EOF
sleep 3
echo "********************ÈÚºÏÍê³É***************************"


for hostname in ${HOST_NAME[@]}
do
/usr/bin/expect <<-EOF
spawn ssh ${HOST_NAME[0]}
expect {
         "*no)?" {send "yes\n\r"
                  expect "*word:" {send "$PASSWORD\n\r"}
                  }
         "*word:" {send "$PASSWORD\n\r"}
      }
expect {
       "*#" { send "scp -r $ssh_dir$author $user@$hostname:$ssh_dir\n\r"
       expect {
              "*no)?" 
                  {send "yes\n\r"
                  expect "*ssword:" {send "$PASSWORD\n\r"}
                   }
             "*ssword:" {send "$PASSWORD\n\r"}
      }
      }
      }   
expect eof
EOF
done

转载于:https://my.oschina.net/112612/blog/1560133

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值