#!/usr/bin/env 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}; } } }