What's the easiest way to have a system script (running as root) execute remote commands over ssh?

I've written some scripts that execute commands remotely via ssh, and they work great when I run them as myself, as I've set up ssh-agent and keys for passwordless login. I'd like to call these when my laptop docks and undocks. I've been successful at running arbitrary scripts when docking/undocking, but since the ACPI event scripts run as root, trying to run my ssh script fails during authentication.

I tried using sudo with the -u and -i flags to simulate running the script as my user, e.g.:

sudo -u redmoskito -i /home/redmoskito/bin/remote_command

which successfully finds my private key and tries to use it, but the ssh-agent credentials are still missing, so it still needs my passphrase.

link|improve this question
possible duplicate of SSH: execute sudo command – Sathya Jun 14 '10 at 18:18
@Sathya The user in that post is trying to execute a remote command as root on the server, whereas I'm trying to execute a remote command as root on the client. – redmoskito Jul 3 '10 at 17:32
feedback

2 Answers

up vote 1 down vote accepted

Nothing wrong with the sudo part. To pick up an existing ssh agent, see these answers: http://superuser.com/questions/141044/sharing-the-same-ssh-agent-among-multiple-login-sessions

link|improve this answer
feedback

Did you try using su instead of sudo?

su - redmoskito -c "/home/redmoskito/bin/remote_command"

OR

edit your script to use: ssh -l redmoskito (host) (command) (assuming that redmoskito is the same username on remote machine) Then on the remote machine's redmoskito home directory $HOME/.ssh/authorized_keys, put in the contents of local machine's root $HOME.ssh/id_rsa.pub after creating a blank passphrase as root on local machine with ssh-keygen -t rsa Make sure $HOME or $HOME/.ssh or any of the files in $HOME/.ssh are not group or other writeable if your sshd_config file has StrictModes=yes

If none of this works, let me know. I will duplicate the problem on my machines.

link|improve this answer
That would certainly solve the issue, as it circumvents the "ssh agent needs authentication" issue, but blank passphrase is not an option for me. – redmoskito Jul 3 '10 at 17:33
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.