lazy ssh-add
authorpatrick nsukami <ndkpatt@gmail.com>
Sun, 22 Nov 2015 14:44:57 +0000 (14:44 +0000)
committerpatrick nsukami <ndkpatt@gmail.com>
Sun, 22 Nov 2015 14:44:57 +0000 (14:44 +0000)
.bash_function

index 00f7c23..1770d3d 100644 (file)
@@ -2,6 +2,57 @@
 # my weird function file
 # lots of this coming from the internets
 
+# _ssh_agent() {
+#     command -v ssh-agent >/dev/null || return
+#     [ "$SSH_CONNECTION" ] && return
+#     local info=$HOME/.cache/ssh-agent-info
+#     [ -f $info ] && . $info >/dev/null
+#     [ "$SSH_AGENT_PID" ] && kill -0 $SSH_AGENT_PID 2>/dev/null || {
+#         mkdir -p $(dirname $info)
+#         ssh-agent >$info
+#         . $info >/dev/null
+#         }
+# } _ssh_agent
+
+# http://uggedal.com/journal/lazy-ssh-add/
+_ssh_add() {
+    local key=$1
+
+    [ "$SSH_CONNECTION" ] && return
+    ssh-add -l >/dev/null || ssh-add $key
+}
+
+ssh() {
+    local remote=$1
+
+    case $remote in
+        "vm1") _ssh_add $HOME/.ssh/id_rsa_kratos ;;
+        "phobos") _ssh_add $HOME/.ssh/id_rsa_phobos ;;
+        *) echo "valid key not found" && exit 1 ;;
+    esac
+    command ssh "$@"
+}
+
+scp() {
+    local file=$1
+    local remote=$2
+
+    case $remote in
+        "vm1:") _ssh_add $HOME/.ssh/id_rsa_kratos ;;
+        "phobos:") _ssh_add $HOME/.ssh/id_rsa_phobos ;;
+    esac
+
+    command scp "$@"
+}
+
+
+# git() {
+#     case $1 in
+#         push|pull|fetch) _ssh_add ;;
+#     esac
+#     command git "$@"
+# }
+
 my_up(){
     ifup br0 &&
     echo "nameserver 80.67.169.12" | sudo tee /etc/resolv.conf