Original link: IT book - itbook.com
purpose
One-click configuration between cluster nodes password-free login. Provided that the passwords on all nodes is the same!
achieve
I come ~ / nonpassword
Script Content:
#!/bin/sh # all node names NODES=() # all node password PASS=$1 ###### Validation args length nodes_length=$(($# - 1)) if [[ ss -gt 0 ]];then echo "Exit: At least two parameters, eg: your_password, node1" exit 2 fi ###### Get all node name i=0 for node in $* do if [[ i -gt 0 ]]; then j=$((i - 1)) NODES[j]=$node fi let i++ done MASTER=`hostname` yum -y install expect ################################### function ssh_cmd0(){ user_and_host=$1 password=$2 cmd=$3 /usr/bin/expect <<-EOF set timeout 5 spawn ssh $user_and_host expect { "yes/no" { send "yes\r" -Ev '(^start_mark|^end_mark)' } ###### Create all nodes authorized_keys, And collection to master. echo "" > ~/.ssh/authorized_keys echo "" > ~/.ssh/known_hosts for s in ${NODES[@]} do echo -e "\nSTART:: Slave node ($s) generates id_rsa.pub and sends master node ($MASTER)" ssh_cmd "root@$s" "$PASS" "rm -f ~/.ssh/id_rsa; ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa" ssh_cmd "root@$s" "$PASS" "cat ~/.ssh/id_rsa.pub" >> ~/.ssh/authorized_keys echo "DONE:: Slave node ($s) generates id_rsa.pub and send to master node ($MASTER)" done ###### Copy collectioned authorized_keys to all nodes. echo -e "\n######################### SENDING authorized_keys TO ALL NODE #########################" for s in ${NODES[@]} do echo -e "\nSTART:: Master node ($MASTER) send ALL id_rsa.pub(authorized_keys) to slave node ($s)" ssh_cmd "root@$s" "$PASS" "echo '`cat ~/.ssh/authorized_keys`' > ~/.ssh/authorized_keys" echo -e "DONE:: Master node ($MASTER) send ALL id_rsa.pub(authorized_keys) to slave node ($s)\n" done echo "Complete!"
chomd +x ~/nonpassword
usage
Script Usage:
~/nonpassword <password> <node1> <node2> <node...>
password: All nodes must be the same password.
Specific examples:
~/nonpassword 'mypassword' node1 node2 node3
After executing successful, we can then node1, between node2.node3 password-free login, and such has been boarded node1, then log on to node2, can execute commands directly on node1:
ssh node2
It can be.