集群免密
November 30, 2024
#!/bin/bash #==================================================== # Author: Mr.Song # Create Date: 2021-04-06 # Description: 集群自动免密,适用于集群初始化 # Notice: 只适用于centos系列 #==================================================== username=michael test_file="/path/to/test/file" host_list="/path/to/all-host.txt" #notice dest_dir end do not include /,may cause expect cat not understand directory dest_dir="/root" password="xxx" function allscp() { cat $host_list | while read line do host_ip=`echo $line | awk '{print $1}'` expect_test $1 done } #expect function function expect_test { expect <<- EOF set src_file [lindex $argv 0] set timeout 50 spawn scp -r $1 $username@$host_ip:$dest_dir expect { "(yes/no)?" { send "yes\n";exp_continue expect "*assword:" {send "$password\n"} } "*assword:" { send "$password\n" } } expect eof #notice <<- only support tab before EOF,do not support space,paste may cause problems EOF } if ! rpm -qa|grep expect; then echo "expect not installed,please install first." exit 1 else touch $test_file ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa mv ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys #in order to add every host's fingerprint to ~/.ssh/know_hosts,first meet all hosts allscp $test_file #copy rsa allscp ~/.ssh fi