Automate the automated scripting of SSH mutual trust between Linux machines in bulk __linux

Source: Internet
Author: User
Tags chmod ssh

It takes several steps to build an SSH trust manually, and manual interaction (enter the password, etc.), if the number of machines is very cumbersome, in order to save their time, wrote an automated script for reference.

1. Create a new executable file on one of the machines (assuming the file name is ssh_auth.sh) and add the following to the file:

#!/bin/sh dest_user=$1 password=$2 hosts_file=$3 If [$#-ne 3]; 
Then echo "Usage:" echo "$ remoteuser remotepassword hostsfile" Exit 1 fi ssh_dir=~/.ssh script_prefix=./tmp Echo =========================== # 1. Prepare directory. SSH mkdir $SSH _dir chmod $SSH _dir # 2. Generat ssh key tmp_script= $SCRIPT _prefix.sh echo "#!/usr/bin/expect" > $TMP _script echo "Spawn Ssh-keygen-b 1024-t R SA ">> $TMP _script echo" expect *key* >> $TMP _script echo "send \ r" >> $TMP _script if [f $SSH _dir/id_rsa ]; Then echo "Expect *verwrite*" >> $TMP _script echo "Send Y\r" >> $TMP _script fi echo "expect *passphrase * ">> $TMP _script echo" send \ r ">> $TMP _script echo" expect *again: ">> $TMP _script echo" send \ r ">> $TMP _script echo "interact" >> $TMP _script chmod +x $TMP _script/usr/bin/expect $TMP _script rm $TMP _script # 3. Generat file Authorized_keys cat $SSH _dir/id_rsa.pub>> $SSH _dir/authorized_keys # 4. chmodFor file Authorized_keys chmod $SSH _dir/authorized_keys echo =========================== # 5. Copy all files to other hosts for IP in $ (cat $HOSTS _file) do if ["X$ip"!= "X"];
        Then echo-------------------------Tmp_script=${script_prefix}. $ip. SH # Check Known_hosts Val= ' ssh-keygen-f $ip ' if ["x$val" = = "X"];
            Then echo "$ip not in $SSH _dir/known_hosts, need to add" val= ' Ssh-keyscan $ip 2>/dev/null ' If ["x$val" = = "X"];
            Then echo "Ssh-keyscan $ip failed!"
                
        else echo $val >> $SSH _dir/known_hosts fi fi echo "copy $SSH _dir to $ip" echo "#!/usr/bin/expect" > $TMP _script echo "Spawn scp-r $SSH _dir $DEST _user@ $ip: ~/" & 
        gt;> $TMP _script echo "expect *assword*" >> $TMP _script echo "send $PASSWORD \ r" >> $TMP _script
  echo "Interact" >> $TMP _script      
        chmod +x $TMP _script #echo "/usr/bin/expect $TMP _script" > $TMP _script.do #sh $TMP _script                
    .do&/usr/bin/expect $TMP _script rm $TMP _script echo "Copy done."
 Fi done by Echo.

2, in the executable file under the same directory to create a new file named host, will establish an SSH trust machine name or IP address added to the file, each machine name or IP for one line, such as:

192.168.1.2
192.168.1.3
192.168.1.4
192.168.1.5


3, run executable script ssh_auth.sh file, ssh_auth.sh accept three parameters, remote machine username, password and host file name (relative path or absolute path can be).


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.