6

I have a remote machine, with which I have set up public-key ssh auth - that is, I can ssh to it without typing in password each time. And I have a set of actions in a script, which use that ssh connection.

But when I run that script via cron, it keeps asking passphrase for each action - and since I have more that ten of them, it gets quite boring.

I tried running ssh-add before those actions and got:

Could not open a connection to your authentication agent.

What can I do to reduce number of passphrase retypes?

Rogach
  • 6,303
  • Did you add the cron entry to the crontab of the user that owns the key? That is, you didn't do something silly like add the script call to the global crontab, right? – Warren Young Feb 27 '12 at 17:59
  • @WarrenYoung - actually, that's exactly what I did - I just put in the name of the user that owns the key as the user to run it from :) What is wrong with it? Crontab treats these two cases different ways? – Rogach Feb 27 '12 at 18:02
  • Try running eval $(ssh-agent -s) before ssh-add (and ssh-agent -k at end). – enzotib Feb 27 '12 at 18:08
  • @enzotib - Thanks, it works! I assume that ssh-agent -k should also be placed into eval? – Rogach Feb 27 '12 at 18:14
  • There is a thing called "necroposting". Can we call what happened today "necromodding"? Come on, the question is 4 (!) years old, don't you have something better to do with your time? – Rogach Sep 28 '16 at 10:17

1 Answers1

5

Try running

eval $(ssh-agent -s) 

before ssh-add, to export the environment variables that refer to the just started agent, then run

ssh-agent -k

(without eval) at end of script to kill the agent.

enzotib
  • 51,661
  • When I run a last statement without eval, it gives me back two unset statements and an echo statement. Isn't it meant to be put into eval? – Rogach Feb 27 '12 at 18:21
  • @Rogach: it is useful to run it into eval when your script keep doing things and you want to unset the variables, but it is not required. – enzotib Feb 27 '12 at 18:30