21 lines
874 B
Bash
21 lines
874 B
Bash
#!/bin/bash
|
|
|
|
echo -e "${PURPLE}${BOLD}Enter the receiving server ip : ${RESET}"
|
|
read RECEIVING_IP
|
|
echo -e "${PURPLE}${BOLD}Enter the receiving server user : ${RESET}"
|
|
read RECEIVING_USER
|
|
echo -e "${PURPLE}${BOLD}Enter the receiving server port : ${RESET}"
|
|
read RECEIVING_PORT
|
|
|
|
if [ ! -f ~/.ssh/id_rsa ]; then
|
|
ssh-keygen -t rsa -b 2048 -N "" -f ~/.ssh/id_rsa
|
|
fi
|
|
ssh-copy-id -p ${RECEIVING_PORT} ${RECEIVING_USER}@${RECEIVING_IP}
|
|
|
|
hostname=$(hostname)
|
|
|
|
ssh -p $RECEIVING_PORT $RECEIVING_USER@$RECEIVING_IP << EOF
|
|
awk '/${hostname}$/ {last=\$0; count++} {lines[NR]=\$0} END {for (i=1; i<=NR; i++) if (lines[i] !~ /$(hostname)$/ || count-- == 1) print lines[i]}' /home/${RECEIVING_USER}/.ssh/authorized_keys
|
|
> /home/${RECEIVING_USER}/.ssh/authorized_keys.tmp
|
|
&& mv /home/${RECEIVING_USER}/.ssh/authorized_keys.tmp /home/${RECEIVING_USER}/.ssh/authorized_keys
|
|
EOF |