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.
# 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.24107; export SSH_AUTH_SOCK;
SSH_AGENT_PID=24109; export SSH_AGENT_PID;
Re:An alternative
Theory on 2002-09-21T00:27:56
I'm glad to see this, as I've really missed SSHAgentServices. But neither of these solutions is as optimal as SSHAgentServices was, IMO. The ">console" login stuff is a PITA, and I hate the idea that I have to have the SSH Agent application running whenever I want to use SSH (which is to say, all day long!).
I wrote to Kevin Van Vechten, the creator of SSHAgentServices, to see if he'd be willing to update it for Jaguar, but I've not heard back from him. I tried to write him once before, several months ago, and din't hear back then, either. But I expect that something really simply needs to be changed to get it to work -- I just don't know what it is!
Anyway, thanks for the link. I'll be talking about this in my OSXConn Presentation.