public
Created — forked from danndalf/keyset.pl

This is a quick and dirty perl script for managing different keysets (ssh, eyrc, gitrc, etc)

  • Download Gist
keyset.pl
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
#!/usr/bin/perl -w
use strict;
 
###
# keyset script - a utility for managing multiple keysets within the same UNIX account
# Copyright (C) 2012 Dann Stayskal
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
###
# To use this, you'll want to create a ~/.keysets directory.
# Within that directory will need to be one directory for each of your keysets.
# Within each keyset directory, you'll want to have copies of the relevant keys
# to be rotated in and out, with the leading dos removed:
#
# ~/.keysets/dann
# ~/.keysets/dann/ssh
# ~/.keysets/dann/ssh/config
# ~/.keysets/dann/ssh/known_hosts
# ~/.keysets/dann/ssh/id_dsa
# ~/.keysets/dann/ssh/id_dsa.pub
# ~/.keysets/dann/gitconfig
# ~/.keysets/dann/eyrc
# ~/.keysets/copious/eyrc
# ~/.keysets/copious/ssh/config
# ... etc.
#
# Finally, you'll want to symlink 'current' in the .keysets directory to whichever keyset is currently loaded.
# Needless to say, CREATE BACKUPS OF YOUR KEYS BEFORE RUNNING THIS SCRIPT.
# Ahem. Thank you.
#
###
# This script serves three functions:
#
# `keset` - reports the current keyset, good for
# embedding in your bash prompt
# `keyset list` - lists all available keysets
# `keyset load <keyset_name> - loads the requested keyset into your home directory
#
###
# Sample output:
# $ keyset
# dann
#
# $ keyset list
# copious
# * dann
#
# $ keyset load copious
# Removing link to ssh/config
# Removing link to ssh/known_hosts
# Removing link to ssh/id_dsa
# Removing link to ssh/id_dsa.pub
# Removing link to gitconfig
# Setting link /home/dann/.keysets/copious/ssh/config ---> /home/dann/.ssh/config
# Setting link /home/dann/.keysets/copious/ssh/id_rsa.pub ---> /home/dann/.ssh/id_rsa.pub
# Setting link /home/dann/.keysets/copious/ssh/known_hosts ---> /home/dann/.ssh/known_hosts
# Setting link /home/dann/.keysets/copious/ssh/id_rsa ---> /home/dann/.ssh/id_rsa
# Setting link /home/dann/.keysets/copious/gitconfig ---> /home/dann/.gitconfig
# Identity added: /home/dann/.ssh/id_rsa (/home/dann/.ssh/id_rsa)
#
###
my $current_keyset = `readlink ~/.keysets/current`;
chomp $current_keyset;
 
if (scalar(@ARGV)){
 
my $command = shift @ARGV;
chomp $command;
 
if ($command eq 'list') {
 
my @keysets = `ls ~/.keysets`;
foreach my $keyset_name (@keysets){
chomp $keyset_name;
next if $keyset_name eq 'current';
if ($keyset_name eq $current_keyset) {
print " * $keyset_name\n";
} else {
print " $keyset_name\n";
}
}
 
} elsif ($command eq 'load') {
 
### Figure out which keyset they want to use
my $new_keyset = '';
$new_keyset = shift @ARGV if scalar @ARGV;
chomp $new_keyset;
unless ($new_keyset) {
print "Usage: keyset load [keyset_name]\n";
exit 1;
}
 
### Make sure the keyset they've selected actually exists
my $valid_new_keyset = `ls ~/.keysets/$new_keyset`;
chomp $valid_new_keyset;
unless ($valid_new_keyset) {
print "$new_keyset isn't a valid keyset. For a list of keysets available, use 'keyset list'\n";
exit 1;
}
 
### Go through and remove links to current keyset materials
if ($current_keyset) {
my @links = `find ~/.keysets/$current_keyset | awk -Fkeysets\/$current_keyset\/ '{print \$2}'`;
chomp @links;
foreach my $link (@links) {
chomp $link;
my $link_source = "$ENV{HOME}/.keysets/$current_keyset/$link";
my $link_target = "$ENV{HOME}/.$link";
 
### We're only symlinking source files
next unless -f $link_source;
 
### Before unlinking something, make sure it's a symlink
next unless -l $link_target;
 
# print "$link_source ---> $link_target\n";
print "Removing link to $link\n";
unlink($link_target);
}
}
 
### Go through and set links to new keyset materials
my @links = `find ~/.keysets/$new_keyset | awk -Fkeysets\/$new_keyset\/ '{print \$2}'`;
chomp @links;
foreach my $link (@links) {
chomp $link;
my $link_source = "$ENV{HOME}/.keysets/$new_keyset/$link";
my $link_target = "$ENV{HOME}/.$link";
 
### We're only symlinking source files
next unless -f $link_source;
 
print "Setting link $link_source ---> $link_target\n";
`ln -s $link_source $link_target`;
}
 
 
### Reset ~/.keysets/current
`rm ~/.keysets/current`;
`cd ~/.keysets/ && ln -s $new_keyset ~/.keysets/current`;
### Add the new key to ssh-agent if able
`killall -9 ssh-agent`;
`ssh-add`;
}
 
} else {
 
### Running with no arguments.
### If they have a keyset loaded, print its name and exit clean.
### If they don't have a keyset loaded, direct them to the keyset list and exit with error status.
if ($current_keyset) {
print "$current_keyset\n";
exit 0;
} else {
print "No keyset currently loaded.\nRun 'keyset list' for a list of availbale keysets\n";
exit 1;
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.