ssh免密脚本

#!/bin/sh

if [ "$1"x = ""x ]; then
    echo "usage:/opt/bin/auto-ssh.sh user server"
else
    user=$1
    server=$2
    pubkey=`cat ~/.ssh/id_rsa.pub`
    if [ "$pubkey"x = ""x ]; then
       ssh-keygen
       pubkey=`cat ~/.ssh/id_rsa.pub`
    fi
    cmd="mkdir -p ~/.ssh && chmod 700 ~/.ssh && [ ! -e \'~/.ssh/authorized_keys\' ] && touch ~/.ssh/authorized_keys&&echo \"$pubkey\" >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
    ssh $user@$server \`$cmd\`
fi

 

posted @ 2017-09-11 15:46  wangmo  阅读(158)  评论(0编辑  收藏  举报