PS:前提可以正常联网
~连接方
vi ssh.sh #!/bin/bash IPADDRESS1="$1" IPADDRESS2="$2" USER="$3" PASSWORD="$4" systemctl restart network && yum -y install wget && wget -O /etc/yum.repos.d/CentOs-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo && wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo && yum clean all && yum makecache && rm -rf ~/.ssh/{known_hosts,id_rsa*} && ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa &> /dev/null && sed -i '/StrictHostKeyChecking/c StrictHostKeyChecking no' /etc/ssh/ssh_config && rpm -q sshpass &> /dev/null || yum -y install sshpass &> /dev/null && sshpass -p ${PASSWORD} ssh-copy-id -i ${USER}@${IPADDRESS2} &> /dev/null && echo "success" chmod +x ssh.sh ./ssh.sh [连接方IPADDRESS1] [被连接方IPADDRESS2] [USER] [PASSWORD]
~被连接方
vi ssh.sh #!/bin/bash IPADDRESS1="$1" IPADDRESS2="$2" USER="$3" PASSWORD="$4" systemctl restart network && yum -y install wget && wget -O /etc/yum.repos.d/CentOs-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo && wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo && yum clean all && yum makecache && rm -rf ~/.ssh/{known_hosts,id_rsa*} && ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa &> /dev/null && sed -i '/StrictHostKeyChecking/c StrictHostKeyChecking no' /etc/ssh/ssh_config && rpm -q sshpass &> /dev/null || yum -y install sshpass &> /dev/null && sshpass -p ${PASSWORD} ssh-copy-id -i ${USER}@${IPADDRESS2} &> /dev/null && echo "success" chmod +x ssh.sh ./ssh.sh [被连接方IPADDRESS1] [连接方IPADDRESS2] [USER] [PASSWORD]