生成ssh key并分发到其他机器上

主要用到了sshpass,使得可以明文指定密码,用于分发key。

install.sh代码如下:

#!/bin/bash

FILEPATH='./filelist'
PRIVATEKEY="key"
PUBLICKEY="key.pub"
IDRSAKEY="id_rsa"
IDRSAKEYPUB="id_rsa.pub"
USERNAME="myusername"
USERPASS="mypassword"

function generate_key()
{
    if [ -e ./$PRIVATEKEY ];then
        rm -rf ./$PRIVATEKEY
    fi
    if [ -e ./$PUBLICKEY ];then
        rm -rf ./$PUBLICKEY
    fi

    ssh-keygen -t rsa -N "" -f ./$PRIVATEKEY
    echo "success generate key"
}

generate_key

if [ -e $FILEPATH ];then
    if [ -f $FILEPATH ];then
        for line in `cat $FILEPATH`
            do
                ./sshpass -p $USERPASS scp -o StrictHostKeyChecking=no ./$PUBLICKEY $USERNAME@$line:/home/$USERNAME/.ssh/$IDRSAKEYPUB
                ./sshpass -p $USERPASS scp -o StrictHostKeyChecking=no ./$PRIVATEKEY $USERNAME@$line:/home/$USERNAME/.ssh/$IDRSAKEY
                ./sshpass -p $USERPASS ssh -o StrictHostKeyChecking=no $USERNAME@$line "cat ~/.ssh/$IDRSAKEYPUB>~/.ssh/authorized_keys"
                echo "copy finished"
            done
    else
        echo "$FILEPATH is not file"
    fi
else
    echo "$FILEPATH not exist"
fi

sshpass下载:http://download.csdn.net/detail/prodigal_uestc/8076393

你可能感兴趣的:(生成ssh key并分发到其他机器上)