Skip to content

Instantly share code, notes, and snippets.

@mnunberg
Created August 11, 2012 20:13
Show Gist options
  • Select an option

  • Save mnunberg/3326916 to your computer and use it in GitHub Desktop.

Select an option

Save mnunberg/3326916 to your computer and use it in GitHub Desktop.
Simple script for using git with alternate ssh keys
#!/usr/bin/perl
# This script makes dealing with alternate SSH keys. Rather than
# doing something retarded like mangling your git or ssh settings
# for automated tasks, you can simply use this script..
# It will create a temporary executable shell script (templated below)
# and delete it when it's done.
#
# It requires the GITREPO_KEYFILE environment variable to be set to the
# path of your ssh private key.
# Otherwise, this script will blindly pass arguments to git(1)
use strict;
use warnings;
use Cwd qw(getcwd);
use File::Temp qw(tempfile);
my $kfile = $ENV{GITREPO_KEYFILE} or
die "GITREPO_KEYFILE env must be set";
my $fstuff = <<"EOF";
#!/bin/sh
exec ssh -i $kfile \$\@
EOF
my ($fh,$fname) = tempfile(DIR => getcwd(), CLEANUP => 1);
print $fh $fstuff;
close($fh);
chmod(0700, $fname) or die "chmod $fname: $!";
$ENV{GIT_SSH}=$fname;
# use system instead of exec, so that the file gets cleaned up on exit
my $rv = system('git', @ARGV);
unlink($fname);
exit($rv);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment