Md at debian.org

tales of a debian maintainer

ssh-agent for unattended processes

This code allows unattended ssh(1) processes, like the ones started by cron(8), to access the SSH keys stored by a running ssh-agent(1) (which will probably have been started in a screen(1) instance).

Be sure to understand the security implications of persistent ssh-agent processes and passwordless keys.

#!/bin/sh -e

import_ssh_agent() {
  local pid
  for pid in $(pgrep -u "$LOGNAME" -x -f /bin/bash); do
    local var="$(grep -z '^SSH_AUTH_SOCK=' /proc/$pid/environ || true)"
    [ "$var" ] || continue
    eval "$var"
    export SSH_AUTH_SOCK
    break
  done
}

[ "$SSH_AUTH_SOCK" ] || import_ssh_agent

# now the script can run commands like:
ssh -o BatchMode=yes hostname command

About

This is the blog of Marco d'Itri.

S M T W T F S
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    

See also:

My blogroll:


W3C HTML 4.01
W3C CSS 2.0     

Powered by Bryar.pm.