From: PHO Date: Tue, 24 Mar 2009 05:15:07 +0000 (+0900) Subject: Initial revision X-Git-Url: https://git.cielonegro.org/gitweb.cgi?a=commitdiff_plain;h=695ef3f0f34641191fae3c7ef8f4e01dd6d3210d;p=ssh-agent-manager.git Initial revision --- 695ef3f0f34641191fae3c7ef8f4e01dd6d3210d diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..154330d --- /dev/null +++ b/Makefile @@ -0,0 +1,10 @@ +prefix = /usr/local +bindir = $(prefix)/bin + +all: + +install: + install -d $(DESTDIR)$(bindir) + install ssh-agent-manager $(DESTDIR)$(bindir) + +.PHONY: all install diff --git a/ssh-agent-manager b/ssh-agent-manager new file mode 100755 index 0000000..8458522 --- /dev/null +++ b/ssh-agent-manager @@ -0,0 +1,85 @@ +#!/usr/bin/perl +# ----------------------------------------------------------------------------- +# Esta programa envuelve la ssh-agent. Devuelve los mismos pid y socket siemple +# durante la ssh-agent está viva. +# ----------------------------------------------------------------------------- +# No puede hacer `ssh-agent-manager '. +# ----------------------------------------------------------------------------- +use strict; +use warnings; +use IO::File; + +# Esta programa no dedice el tipo de shell usa usted. +# Lo fije siemple. +my $csh_mode; +my $bsh_mode; +foreach my $arg (@ARGV) { + if ($arg eq '-c') { + $csh_mode = 1; + } + elsif ($arg eq '-s') { + $bsh_mode = 1; + } +} +if ((!$csh_mode and !$bsh_mode) or + ($csh_mode and $bsh_mode)) { + print "Usage: ssh-agent-manager (-c | -s)\n"; + print "\n"; + print " -c csh mode\n"; + print " -s bsh mode\n"; + exit; +} + +# lee /tmp/ssh-agent-info si ése existe. +# $> es la uid efectiva. +my $infofile = IO::File->new("/tmp/ssh-agent-info.$>", 'r'); +if ($infofile) { + my @contents = <$infofile>; + my $info = &parse(join("\n", @contents)); + $infofile->close; + + my $pid = $info->{'SSH_AGENT_PID'}; + if ($pid and kill(0, $pid) == 1) { + # ssh-agent está aún viva. + &serialize($info); + exit; + } +} + +# comienza la ssh-agent... +my $info = &parse(scalar `ssh-agent -s`); + +$infofile = IO::File->new("/tmp/ssh-agent-info.$>", 'w'); +while (my ($key, $value) = each %$info) { + $infofile->print("$key=$value\n"); +} +$infofile->close; + +&serialize($info); +exit; + +sub parse { + my $lines = shift; + my %info; + foreach my $line (split /\n/, $lines) { + chomp $line; + # falte si el formato que la da `ssh-agent -s` cambie... + if ($line =~ m/^(.+?)=(.+?);/ or + $line =~ m/^(.+?)=(.+)$/) { + $info{$1} = $2; + } + } + \%info; +} + +sub serialize { + my $info = shift; + while (my ($key, $value) = each %$info) { + if ($bsh_mode) { + print qq{$key=$value; export $key;\n}; + } + elsif ($csh_mode) { + print qq{setenv $key $value;\n}; + } + } +}