Skip to content

Commit

Permalink
Minor changes to make script more versatile
Browse files Browse the repository at this point in the history
  • Loading branch information
Fazle Arefin authored and Fazle Arefin committed Feb 22, 2015
1 parent e0ab2a5 commit 3ad19c7
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions ssh-copy-id-auto.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,39 @@

# TODO: change this path to the file containing your password
# the password can be in plain-text or base64 encoded; don't add any blank line
# if the password is base64 encoded, change the value of $BASE64_ENC
export PWDFILE='/home/fazle/.passwd/fazle'
# if the password is base64 encoded, change the value of $Base64_Enc
export Pwd_File='/home/fazle/.passwd/fazle'

# TODO: base64 encoding; 0 is off, 1 is on
export BASE64_ENC=0
export Base64_Enc=0

# TODO: number of hosts to copy your key in parallel
PARALLEL_COPY=10
Parallel_Copy=10

HOSTS=$@
Hosts=$@

# some basic checks
if [ "$1" == "" ]; then
if [[ "$1" == "" ]]; then
echo "No host(s) provided" 1>&2
exit 44
elif [ 0 -ne $(which sshpass &> /dev/null)$? ]; then
elif [[ 0 -ne $(hash sshpass &> /dev/null)$? ]]; then
echo "Install sshpass first" 1>&2
exit 55
elif ! [ -f $PWDFILE ]; then
elif ! [[ -f $Pwd_File ]]; then
echo "Password file missing" 1>&2
exit 66
fi

copy_ssh_id() {
host=$(echo -n $1) # strip the newline character from $1

if [ $BASE64_ENC -eq 1 ]; then
sshpass -p "$(base64 -d $PWDFILE)" ssh-copy-id -o ConnectTimeout=5 $host &> /dev/null
if [[ $Base64_Enc -eq 1 ]]; then
sshpass -p "$(base64 -d $Pwd_File)" ssh-copy-id -o ConnectTimeout=5 $host &> /dev/null
else
sshpass -p "$(cat $PWDFILE)" ssh-copy-id -o ConnectTimeout=5 $host &> /dev/null
sshpass -p "$(cat $Pwd_File)" ssh-copy-id -o ConnectTimeout=5 $host &> /dev/null
fi

if [ $? -eq 0 ]; then
if [[ $? -eq 0 ]]; then
echo "✔ Copied ssh public key to $host" 1>&2
else
echo "✘ Could not copy ssh public key to $host" 1>&2
Expand All @@ -47,5 +47,5 @@ copy_ssh_id() {

export -f copy_ssh_id

echo ${HOSTS[*]} | xargs -d' ' -n1 -P${PARALLEL_COPY} bash -c 'copy_ssh_id "$@"' _
echo ${Hosts[*]} | xargs -d' ' -n1 -P${Parallel_Copy} bash -c 'copy_ssh_id "$@"' _

0 comments on commit 3ad19c7

Please sign in to comment.