ssh public-key sharing using only git bash

Viewed 130

I wan't to make a script that retrieve the public keys from two remote servers and share them each others. Problem is that i'm not allowed to install any tools but git bash hence i do not have access to things like sshpass...

here is my script

#!/bin/bash

clear

#Server 1 id
read -p "Usr name srv 1 (root if empty): " usrname1;
usrname1=${usrname1:-root}
read -p "Ip srv1 : " ip1;

#Server 2 id
read -p "Usr name srv 2 (root if empty)" usrname2;
usrname2=${usrname2:-root}
read -p "Ip srv2 : " ip2;


echo ""


read -p "(Re)generate key ? (y/n)" choice;

case $choice in
   y)
       ssh $usrname1@$ip1 'ssh-keygen -f ~/.ssh/id_rsa'
       ssh $usrname2@$ip2 'ssh-keygen -f ~/.ssh/id_rsa'
       ;;
       
   *)
       scp $usrname2@$ip2:~/.ssh/id_rsa.pub ~/
       ssh $usrname1@$ip1 'cat >>.ssh/authorized_keys' < ~/id_rsa.pub

       scp $usrname1@$ip1:~/.ssh/id_rsa.pub ~/
       ssh $usrname2@$ip2 'cat >>.ssh/authorized_keys' < ~/id_rsa.pub

esac

So this script is working but I litteraly have to write password 3 times per servers which might seems way too much. I'm fairly new in shell and I can't find a way which do not ask for password during keygen, retrieve the key and adding it to the authorized_keys file from each server. Any ideas ?

0 Answers
Related