0

I am writing a bash script to rsync multiple files from a remote host into my personal computer.

What I wrote so far works, one rsync command after the other (three in total), though I have to input my ssh password after each execution of rsync

#!/bin/bash
# create the command line argument, that will be PASSED when calling the script
while getopts r: option
do
case "${option}"
in
r) RUNNAME=${OPTARG};;
esac
done

# create the directory on the Toshiba hard Drive
mkdir -p /media/user/TOSHIBA\ EXT/novaseq/$RUNNAME/InterOp

mkdir -p /media/user/TOSHIBA\ EXT/novaseq/$RUNNAME/Unaligned/Stats

# rsync the files into the "base directory"
# the files that have to be rsynced have the extension .csv, .txt and .xml
rsync -avzh user@remote-host:/nexusb/Novaseq/$RUNNAME/*.* /media/user/TOSHIBA\ EXT/novaseq/$RUNNAME/ && rsync -avzh user@remote-host:/nexusb/Novaseq/$RUNNAME/InterOp/*.bin /media/user/TOSHIBA\ EXT/novaseq/$RUNNAME/InterOp && rsync -avzh user@remote-host:/nexusb/Novaseq/$RUNNAME/Unaligned/Stats/*.* /media/user/TOSHIBA\ EXT/novaseq/$RUNNAME/Unaligned/Stats

Is there a way to call the three rsynccommands while inputting the ssh password only once instead of thrice?

BCArg
  • 205

1 Answers1

0

As @Kusalananda suggested, use a ssh-agent. As you already login via ssh, simply instantiating a ssh-agent piror to executing the script should work fine.

eval $(ssh-agent)
ssh-add 

You can add multiple ssh keys to the same ssh-agent. ssh-add adds the one located in the default location. In case your public / private key pair are not located at the default location, add them to the ssh-add command.

It is also possible to start a ssh-agent when logging in if you use it outside of this script.

edit: I guess terminating the ssh-agent after the script terminates on the remote server is a good idea, esp. if someone else has got root access it is possible to read out your password. In case you want to have a solution that is completely secure, check out this article.