现在的位置: 首页 > 综合 > 正文

让远程主机互相信任SSH连接(不需要密码)的半手动shell脚本

2013年09月09日 ⁄ 综合 ⁄ 共 883字 ⁄ 字号 评论关闭
#!/bin/bash
# trustme [remote user] [remote ip]
# example:
# trustme xxx 192.168.1.3

local_user=`whoami`
local_ip=`ifconfig|sed -n '/inet addr/s/^[^:]*:\([0-9.]\{7,15\}\) .*/\1/p'|sed -n 1p`
remote_user=$1@$2
key_path=/home/$1/

# check my public key
if [ ! -e ~/.ssh/id_rsa.pub ]; then
        echo "-creating public key for $local_user..."
        ssh-keygen -t rsa
fi

echo ""
echo "-copy public key to remote host"
scp ~/.ssh/id_rsa.pub $remote_user:$key_path
echo ""
echo "-make remote host trust me"
ssh $remote_user <<END
        cd /home/$1/
        if [ ! -e .ssh ]; then mkdir .ssh; fi
        cd .ssh
        touch authorized_keys
        # check if already trusted
        tee -a authorized_keys < /home/$1/id_rsa.pub
        chmod 744 authorized_keys
        ls -l |grep authorized_keys
        rm /home/$1/id_rsa.pub
END
scp $0 $remote_user:$key_path
ssh $remote_user "chmod 777 $key_path/$0"
echo ""
echo "this script has been copied to remote user"
echo "run [ $key_path/$0 $local_user $local_ip ] on remote host to trust back"


reference:https://blogs.oracle.com/jkini/entry/how_to_scp_scp_and

抱歉!评论已关闭.