You can make ssh connections within a cron session. What you need is to setup a public key authentication to have passwordless access. For this to work, you need to have PubkeyAuthentication yes
in each remote server's sshd_config
.
You can create a private/public key pair with or without a passphrase. If you use a passphrase (recommented) you need to also start ssh-agent. Without a passphrase, you only need to add the parameter -i your_identity_file
to ssh
command line. ssh
will use $HOME/.ssh/id_rsa
as default.
I replicated your example by using a key pair with a passphrase. Here's how I did it.
1) Created the key pair with passphrase. Saved the private key as ~/.ssh/id_rsa_test
, which should have the correct permissions by default. We can enter an empty passphrase for not using one.
john@coffee:~$ ssh-keygen -N "somephrase" -f .ssh/id_rsa_test
Generating public/private rsa key pair.
Your identification has been saved in .ssh/id_rsa_test.
Your public key has been saved in .ssh/id_rsa_test.pub.
[snip]
2) Sent the public key to the servers, did the same for all of them. Remember they need to have PubkeyAuthentication
enabled.
john@coffee:~$ ssh-copy-id -i .ssh/id_rsa_test server1
The authenticity of host 'server1 (11.22.33.1)' can't be established.
RSA key fingerprint is 79:e8:0d:f5:a3:33:1c:ae:f5:24:55:86:82:31:b2:76.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'server1,11.22.33.1' (RSA) to the list of known hosts.
john@server1's password:
Now try logging into the machine, with "ssh 'server1'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
3) Run ssh-agent as service with -s
. This will not kill it if you logout. Its output is a valid shell script, setting the environment so ssh client will know how to connect to it. We save that to a file (only the first line is really needed).
john@coffee:~$ ssh-agent -s | head -n 1 > ssh-agent.cf
john@coffee:~$ cat ssh-agent.cf
SSH_AUTH_SOCK=/tmp/ssh-VhyKL22691/agent.22691; export SSH_AUTH_SOCK;
4) Loaded the above to our current environment so we can use ssh-add
to add our private key to ssh-agent
. the passphrase from above.
john@coffee:~$ source ssh-agent.cf
john@coffee:~$ ssh-add .ssh/id_rsa_test
Enter passphrase for .ssh/id_rsa_test:
Identity added: .ssh/id_rsa_test (.ssh/id_rsa_test)
5) Verified it is added.
john@coffee:~$ ssh-add -l
2048 96:58:94:67:da:67:c0:5f:b9:0c:40:9b:52:62:55:6a .ssh/id_rsa_test (RSA)
6) The script I used, slightly modified than yours. Notice that I did not enclose the ssh command in parentheses and not using backticks rather $()
, which is a better alternative for command substitution (this is bash
compatible, you didn't mention which shell you're using). I used the exact same ssh command as yours.
john@coffee:~$ cat foo.sh
#!/bin/bash
source /home/john/ssh-agent.cf
for server in server1 server2; do
usr=$(ssh -t -t -o ConnectTimeout=60 $server finger | tail -1 | awk '{print $1}')
date=$(ssh -o ConnectTimeout=60 $server date)
echo "$server - $date - $usr" >> /home/john/foo.log
done
7) My crontab (note that my sh
is actually bash
)
john@coffee:~$ crontab -l
# m h dom mon dow command
*/1 * * * * sh /home/john/foo.sh
8) The output
john@coffee:~$ tail -n 4 foo.log
server1 - Wed Mar 23 14:12:03 EET 2011 - john
server2 - Wed Mar 23 14:12:04 EET 2011 - john
server1 - Wed Mar 23 14:13:03 EET 2011 - john
server2 - Wed Mar 23 14:13:04 EET 2011 - john
The only problem with using a passphrase is that you need to enter it manually at least one time. So, the above will not automatically work after a reboot.