On Mon, Dec 17, 2018 at 10:42:23AM +0000, Mark Rogers wrote:
I need to write a script that runs several tasks against a remote server via SSH & rsync. To be keep it simple, imagine just an rsync followed by a command via SSH.
I don't want this to be passwordless - I want the script to require the remote password before it runs. But I don't want to have to enter it again until the script ends.
Is there a good way to do this?
Yes, use ssh's 'ControlMaster' and related commands. Then do something like my script below:-
#!/bin/bash # # # Get new pictures from camera, send to esprimo, import into Digikam and update sigal # ssh -fN esprimo # # # copy the files from the camera # echo -e "\nCopying files from camera" rsync -a /media/chris/6465-3837/DCIM/*PANA/* esprimo:dld/tz60/ # # # run picImport.py on esprimo to copy the files into ~/pictures # echo -e "\nRunning picImport.py" ssh esprimo /home/chris/bin/picimport.py -s /home/chris/dld/tz60 # # # run sigal on esprimo # echo -e "\nRunning sigal" ssh esprimo /usr/local/bin/sigal build -c /home/chris/bin/sigal.conf.py # # # copy sigal files back to t470 # echo -e "\nCopying sigal files back to t470" rsync -a esprimo:/srv/sigal/ /srv/sigal/
This only asks for the password once and leaves the initial ssh running in the background so all the other rsync/ssh commands use the connection set up by that initial ssh. It uses the 'ControlMaster' setting because my ~/.ssh/config file has:-
ControlMaster auto ControlPath ~/.ssh/%r@%h:%p
You can of course do this bit from the command line oif you want.