set-ssh-agent for Mac OS X

| | Comments (0)

This is a little script to start an ssh-agent for use globally under Mac OS X. I had been using SSHAgentServices, a login plugin, but it stopped working in 10.2. I think perhaps Apple disabled third-part login plugins. Maybe not. Anyway, so I wanted a workaround, and here it is.

The caveat is that it needs to be run before your login session begins, so that the environment can propogate down to all your various apps. So what I do is log in first as user ">console", which gives me a console, where I log in as pudge and run the script. Then I log out, get the login window back, and log in normally.

Along with SSHPasKey, this is used to create an ssh-agent and add an ssh key that I can use universally with all my login sessions.

It uses Mac::PropertyList by brian d foy.

#!/usr/bin/perl
# set-ssh-agent.plx
# pudge@pobox.com 2002.09.12
 
# Run this script before your login session begins, either by logging in via >console
# or logging in and logging back out normally.
 
# It is meant to be used in conjunction with SSHPassKey, and this little startup
# AppleScript, which executes SSHPassKey to add your ssh key to the agent:
 
# try
#    do shell script "/usr/bin/ssh-add"
# end try
 
# Note that I also add the environment to .bashenv, which is then source'd
# from .bash_profile, so that I can get the ssh-agent from all my login sessions.
 
use strict;
use File::Spec::Functions;
use Mac::PropertyList;
 
my $env = `/usr/bin/ssh-agent -s | grep -v echo`;
die "No ssh-agent: $?" unless $env;
 
my $file1 = catfile($ENV{HOME}, '.MacOSX', 'environment.plist');
my $file2 = catfile($ENV{HOME}, '.bashenv');
 
my $plist = Mac::PropertyList::parse_plist(do {
    local $/;
    open my $fh, $file1 or die "Can't read $file1: $!";
    <$fh>
});
die "No plist at $file1" unless $plist;  # file must already exist
 
for (qw(SSH_AUTH_SOCK SSH_AGENT_PID)) {
    $env =~ /^$_=([^;]+);/m;
    $plist->{value}{$_} = {
        value    => $1,
        type    => 'string'
    };
}
 
open my $fh1, "> $file1" or die "Can't write to $file1: $!";
print $fh1 Mac::PropertyList::plist_as_string($plist);
close $fh1;
 
open my $fh2, "> $file2" or die "Can't write to $file2: $!";
print $fh2 $env;
close $fh2;
 
exit(0);
 
__END__
 
`ssh-agent -s` format:
SSH_AUTH_SOCK=/tmp/ssh-XBkE5WTV/agent.241 07; export SSH_AUTH_SOCK;
SSH_AGENT_PID=24109; export SSH_AGENT_PID;

use.perl.org

Leave a comment

<pudge/*> (pronounced "PudgeGlob") is thousands of posts over many years by Pudge.

"It is the common fate of the indolent to see their rights become a prey to the active. The condition upon which God hath given liberty to man is eternal vigilance; which condition if he break, servitude is at once the consequence of his crime and the punishment of his guilt."

About this Entry

This page contains a single entry by pudge published on September 12, 2002 10:09 AM.

iCal's Faults was the previous entry in this site.

iCal Update is the next entry in this site.

Find recent content on the main index or look in the archives to find all content.