LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 01-24-2012, 11:55 PM   #1
the_gripmaster
Member
 
Registered: Jul 2004
Location: VIC, Australia
Distribution: RHEL, CentOS, Ubuntu Server, Ubuntu
Posts: 354

Rep: Reputation: 38
Question 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?
 
Old 01-25-2012, 01:35 AM   #2
ukiuki
Senior Member
 
Registered: May 2010
Location: Planet Earth
Distribution: Debian
Posts: 1,020

Rep: Reputation: 284Reputation: 284Reputation: 284
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
 
Old 01-25-2012, 02:44 AM   #3
jthill
Member
 
Registered: Mar 2010
Distribution: Arch
Posts: 209

Rep: Reputation: 65
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
 
Old 01-25-2012, 11:36 AM   #4
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 2,424

Rep: Reputation: 890Reputation: 890Reputation: 890Reputation: 890Reputation: 890Reputation: 890Reputation: 890
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.
 
Old 01-25-2012, 01:23 PM   #5
jthill
Member
 
Registered: Mar 2010
Distribution: Arch
Posts: 209

Rep: Reputation: 65
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.

Last edited by jthill; 01-25-2012 at 01:25 PM. Reason: suicide's factoring does 300 logfiles, I missed that.
 
  


Reply

Tags
ssh public key


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pfx certification file to extract the RSA private/public key fantasy1215 Linux - Newbie 2 11-15-2011 02:51 AM
SSH skips public key authentication for a key, but works with another key simopal6 Linux - General 1 07-06-2011 08:33 AM
Putty/SSH login failed when using RSA public key: 'Server refused our key' itsecx@gmail.com Linux - Server 10 10-04-2010 01:19 PM
ssh: is there any issue with adding my personal public key to authorized_keys? Meson Linux - Security 6 10-07-2008 12:27 AM
RSA public key encryption/private key decription koningshoed Linux - Security 1 08-08-2002 07:25 AM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration