aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--README.md26
-rwxr-xr-xaddkey20
-rw-r--r--ssh-agent-setup.sh8
3 files changed, 54 insertions, 0 deletions
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 <key> [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 <key> [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)