Setup bulk password-less ssh

Following script will help in setting up password-less ssh in one shot to multiple machines,

Usage sample: $ remote-keyless.sh <file_of_hosts.txt> [password]

#!/usr/bin/expect -f
# remote-keyless.sh, v0.1, 2016-05-03, [email protected]
#                    v0.2, 2016-09-28, [email protected] - added to make it compatible with pdsh ;)
# Setup password less ssh
# Arg #1 - File containing list of hosts
# Arg #2 - The common password for all hosts
# Sample: remote-keyless.sh <file_of_hosts.txt> [password]


set host_file [lindex $argv 0]
set password [lindex $argv 1]


# Get password
if { $password == "" } {
    stty -echo
    send_user -- "Input common password for all hosts: "
    expect_user -re "(.*)\n"
    send_user "\n"
    stty echo
    set password $expect_out(1,string)
}

# Get hosts from file
set f [open $host_file]
set hosts [split [read $f] "\n"]
close $f

# Magic here
foreach host $hosts {
  if { $host != "" } {
    spawn ssh-copy-id $host
    expect {
      "continue connecting (yes/no)?" {
        send "yes\r"
        expect "list of known hosts."
        exp_continue
      }
      "$::env(USER)@$host's password:" {
        send "$password\r"
        expect eof
      }
    }
  }
}

 

 

Additional scripts

Copy existing setup to multiple remote nodes using scp

for dest in $(<~/.robin/hosts-all); do
  scp -r ~/.robin ${dest}:~
done

Copy existing setup to remaining multiple remote nodes using scp

for dest in $(<~/.robin/stage-hosts-all); do
if [ "$(hostname)" != ${dest} ]; then
  # echo ${dest}
  scp -r ~/.robin ${dest}:~
fi
done

 

Add custom bash_profile to .bash_profile on multiple hosts using pdsh

pdsh -w ^.robin/hosts-all echo ". ~/.robin/bash_profile" >> ~/.bash_profile

 

Enjoy!

 

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *