Friday 11 December 2015

Using ssh-agent


Using ssh-agent

ssh-agent -s > AmundSshAgent
source AmundSshAgent
ssh-add <key>


Sometime,  we need to perform some daily task, that need grant access. In these case, we can use the following few tricks to make use along with cronjobs.

[step1]

$ ssh-agent -s > sshagent_amit

[step2] [you should see something like following output. ]

$ source sshagent_amit

$ cat sshagent_amit

SSH_AUTH_SOCK=/tmp/ssh-iHFFf28257/agent.28257; export SSH_AUTH_SOCK;
SSH_AGENT_PID=28258; export SSH_AGENT_PID;
echo Agent pid 28258;

[step3]
ssh-add <your_keys>

NOTE: After the system got restared, you need to update your agent file, or you can automate this one too.

Now you can just logout, and login again and source the file [ example: sshagent_amund] and then, you should see your keys, when you type "ssh-add -L"

[step4]
The script that we have plans to add into the crontab.

#!/bin/bash
source sshagent_amund
ssh amit@remote_host "command" > output_file.txt


In cron entry:

* * * * * /bin/bash -l -c '/path/to/your/script'


-l        Make bash act as if it had been invoked as a login shell (see INVO-CATION below).
-c        Command


update the cron time as per your need.

No comments:

Post a Comment