Have cron parse SSH config file for server addresses and options

2

How does one configure cron to parse ~/.ssh/config to get the IP addresses and other options (such as IdentityFile) for SSH? Obviously adding source /home/user/.ssh/config does not help.

I do not want to add the options in the script run from cron as it calls over twenty servers which have varying options. I keep the ~/.ssh/config file updated when IP addresses or PEM keys change, and I do not want to duplicate that effort to another file to maintain.

Cron does not inherit the environment of the user configures it. Thus, for a given script:

$ cat .ssh/config
Host alpha
    Hostname 1.2.3.4
    User dotan
    IdentityFile ~/.ssh/Dotan.pem
    IdentitiesOnly yes

$ cat script.sh
#!/bin/bash
echo "Alpha"
ssh alpha dmesg

This works:

$ ./script.sh > output.log
$ cat output.log
Alpha
[73542728.181371] type=1400 audit(1397118713.764:13): apparmor="STATUS" operation="profile_replace" name="/usr/lib/connman/scripts/dhclient-script" pid=710 comm="apparmor_parser"

However, this cronjob does not work:

0 0 * * * /home/dotan/script.sh > /home/dotan/output.log

The output is:

Alpha

That proves that the script ran, but SSH failed to connect.

EDIT: I just discovered that ssh has the option -F /home/dotan/.ssh/config to specify a config file, but this did not resolve the issue.

dotancohen

Posted 2014-05-12T07:17:42.160

Reputation: 9 798

I do not see how this has anything to do with cron. Can you give some more explanation of what you are trying to achieve and what your .ssh/config might look like? – Cameron Kerr – 2014-05-12T08:02:32.553

I have edited the question with more information. – dotancohen – 2014-05-12T08:16:23.343

Does ssh -v not give any information? – user1686 – 2014-05-12T09:28:40.823

@grawity: Discovering that ssh -v did not give any information was key to finding the problem, see my comment to Cameron's answer below. Thank you! – dotancohen – 2014-05-12T11:25:30.157

Answers

2

Your environment will be different. Compare the output of env when run with/without cron.

Also, where are you putting this in cron? Is it running as your account, or is it running as root (can't tell by looking at your cron job)

Cameron Kerr

Posted 2014-05-12T07:17:42.160

Reputation: 968

The issue was that the script called another script which did not use the full path. What an easy thing to overlook! – dotancohen – 2014-05-12T10:40:36.720

It happens quite freqently. You may find it useful to set things like SHELL and PATH in your crontab. Remember, if called as sh, don't expect Bash-specific behaviour to work. Also, there is no need to put ".sh" on the end of a script (or any extension for an executable file; that gives you the freedom to reimplement in a different language if needed. – Cameron Kerr – 2014-05-12T10:53:42.187

Thanks. Actually the real scripts don't have the extension, but I like to normalize my SO posts to be more readable. My PS1 is a mess, too! – dotancohen – 2014-05-12T11:12:14.557