#!/bin/bash# trustme [remote user] [remote ip]# example:# trustme xxx 192.168.1.3local_user=`whoami`local_ip=`ifconfig|sed -n '/inet addr/s/^[^:]*:\([0-9.]\{7,15\}\) .*/\1/p'|sed -n 1p`remote_user=$1@$2key_path=/home/$1/# check my public keyif [ ! -e ~/.ssh/id_rsa.pub ]; then echo "-creating public key for $local_user..." ssh-keygen -t rsafiecho ""echo "-copy public key to remote host"scp ~/.ssh/id_rsa.pub $remote_user:$key_pathecho ""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.pubENDscp $0 $remote_user:$key_pathssh $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