LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   Parallelizing Copying public key to authorized_keys file (https://www.linuxquestions.org/questions/linux-networking-3/parallelizing-copying-public-key-to-authorized_keys-file-925569/)

the_gripmaster 01-24-2012 11:55 PM

Parallelizing Copying public key to authorized_keys file
 
Here's a script I am using to copy my public key to the ~/.ssh/authorized_key on the 300+ servers I have at work:

Code:

# allservers.txt file contains all servers
# pass.txt file contains user's password
for i in $(cat allservers.txt); do
  echo "Trying $i"
  ping -c 1 -W 1 $i > /dev/null # to see if server is online
  if [ $? -eq 0 ]; then
    sshpass -f pass.txt ssh-copy-id $i
    if [ $? -eq 0 ]; then
      echo "Done $i"
    fi
  else
    echo "ping failed"
  fi
  echo -e "\n"
done

The problem is that it is trying each of the servers in turn and taking a long time. Any trick to make it parallel so that it does, say, 10 servers at a time?

ukiuki 01-25-2012 01:35 AM

What if your script split allservers.txt in 2 parts and then work with each half and then split each half and then split all 4 parts so it will be 8 parts and again and again, recursively it could keep spliting until there is nothing to split, at the same time it can pass the keys for each server.

I hope this make sense to you.

Regards

jthill 01-25-2012 02:44 AM

Why bother throttling it?
Code:

# allservers.txt file contains all servers
# pass.txt file contains user's password
for i in $(cat allservers.txt); do
  (
  echo "Trying $i"
  ping -c 1 -W 1 $i > /dev/null # to see if server is online
  if [ $? -eq 0 ]; then
    sshpass -f pass.txt ssh-copy-id $i
    if [ $? -eq 0 ]; then
      echo "Done $i"
    fi
  else
    echo "$i ping failed"
  fi
  echo -e "\n"
  ) &
done


suicidaleggroll 01-25-2012 11:36 AM

Split it into two scripts

script1:
Code:

echo "Trying $1"
ping -c 1 -W 1 $1 > /dev/null # to see if server is online
if [ $? -eq 0 ]; then
  sshpass -f pass.txt ssh-copy-id $1
  if [ $? -eq 0 ]; then
    echo "Done $1"
  fi
else
  echo "ping failed"
fi
echo -e "\n"

script2:
Code:

for i in $(cat allservers.txt); do
  nohup script1 $i &> $i.log &
done

If you don't want all 300 to blast your network at once, you can put a sleep statement and a counter into script2, so it launches say 10, then waits 3 seconds before launching the next 10, and so on.

jthill 01-25-2012 01:23 PM

In suicidaleggroll's factoring you're going to want `&>>` not `&>`, or put the `&>` after the `done`. It's a pretty safe bet you're using bash, but if not, `&>` is a bashism, `1>>logfile 2>>&1` is the portable spelling.

EDIT: Oops, sorry, that script makes 300 logfiles, that'll work. Need. Less. Coffee.


All times are GMT -5. The time now is 07:37 AM.