From 48bd2dee17a7293fe5329534afac31f913dc4b71 Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Sun, 1 Nov 2015 16:09:36 +0100 Subject: Initial commit --- README.md | 26 ++++++++++++++++++++++++++ addkey | 20 ++++++++++++++++++++ ssh-agent-setup.sh | 8 ++++++++ 3 files changed, 54 insertions(+) create mode 100644 README.md create mode 100755 addkey create mode 100644 ssh-agent-setup.sh diff --git a/README.md b/README.md new file mode 100644 index 0000000..a7ba1fd --- /dev/null +++ b/README.md @@ -0,0 +1,26 @@ +addkey +====== + +A simple set of scripts for facilitating SSH key management. Collectively, the result is to always have a single active instance of ssh-agent across shell instances, with the ability to easily add more keys by name. + +## Installation + +Source `ssh-agent-setup.sh` in your shell's config, and add `addkey` to your path. + +Note that you need to have OpenSSH installed, since this uses `ssh-agent`. + +## Usage + +Assuming that `ssh-agent` is running (which it should be if `ssh-agent-setup.sh` was sourced correctly), use `addkey` to add keys from `~/.ssh` to the agent: + +``` +addkey github +``` + +This will attempt to add `~/.ssh/github` to the agent, prompting for a passphrase if necessary. Optionally: + +``` +addkey github 3600 +``` + +Adds the key with a lifetime of 3600 seconds, or 1 hour. diff --git a/addkey b/addkey new file mode 100755 index 0000000..5f7a13a --- /dev/null +++ b/addkey @@ -0,0 +1,20 @@ +#!/usr/bin/env zsh +# +# Adds the specified key to the agent if one is running, optionally for the specified time. The default lifetime is forever if not specified. +# Usage is as such: addkey [time] +# where key is the name of the private key file, and time is an optional timeout in seconds + +if [ $# -eq 0 ]; then + echo "No arguments provided, usage: addkey [time]" + exit 1 +fi + +if [ -z "$(ssh-add -l | grep "$HOME/.ssh/$1")" ]; then + if [ -z $2 ]; then + ssh-add ~/.ssh/$1 + else + ssh-add -t $2 ~/.ssh/$1 + fi +else + echo "$1 is already in the agent" +fi diff --git a/ssh-agent-setup.sh b/ssh-agent-setup.sh new file mode 100644 index 0000000..8b2ab98 --- /dev/null +++ b/ssh-agent-setup.sh @@ -0,0 +1,8 @@ +# Starts the ssh-agent if it is not running, and either way sources the required environment variables. + +if [ -z "$(ps -e | grep ssh-agent)" ]; then + # ssh-agent does not seem to be running, start it + ssh-agent | sed -r '/echo Agent pid [0-9]+;/d' > ~/.sshsession +fi + +eval $(<~/.sshsession) -- cgit v1.2.3