c223b83b43eef99ff69c042b7ee889dd85396f6c
Linux/ShellScript/Home.md
| ... | ... | @@ -2,3 +2,4 @@ |
| 2 | 2 | - [[ArgWithSpace]]
|
| 3 | 3 | - [[BackupWithReport]]
|
| 4 | 4 | - [[MakePasswd]]
|
| 5 | +- [[SshAgent]]
|
Linux/ShellScript/SshAgent.md
| ... | ... | @@ -0,0 +1,34 @@ |
| 1 | +[[_TOC_]]
|
|
| 2 | +
|
|
| 3 | +# 概要
|
|
| 4 | +- 再ログイン時に既存の SSH-Agent を再利用する。
|
|
| 5 | +- [Sharing the same `ssh-agent` among multiple login sessions - Super User](https://superuser.com/a/950479)
|
|
| 6 | +
|
|
| 7 | +# ソース
|
|
| 8 | +```bash
|
|
| 9 | +#!/bin/bash
|
|
| 10 | +# https://superuser.com/a/950479
|
|
| 11 | +
|
|
| 12 | +agent_out_file="$HOME/ssh-agent.out"
|
|
| 13 | +
|
|
| 14 | +function initialize {
|
|
| 15 | + pkill ssh-agent
|
|
| 16 | + ssh-agent > $agent_out_file
|
|
| 17 | + . $agent_out_file
|
|
| 18 | +}
|
|
| 19 | +
|
|
| 20 | +pgrep ssh-agent
|
|
| 21 | +if [ $? -eq 0 ]; then # ssh agent running
|
|
| 22 | + ssh-add -l > /dev/null 2>&1
|
|
| 23 | + status=$?
|
|
| 24 | + if [ $status -eq 0 ]; then # can connect to ssh agent and keys available
|
|
| 25 | + echo nothing to do
|
|
| 26 | + elif [ $status -eq 1 ]; then # can connect to ssh agent and no keys available
|
|
| 27 | + ssh-add ~/.ssh/id_rsa
|
|
| 28 | + elif [ $status -eq 2 ]; then # cannot connect to ssh agent
|
|
| 29 | + . $agent_out_file
|
|
| 30 | + fi
|
|
| 31 | +else # ssh agent not running
|
|
| 32 | + initialize
|
|
| 33 | +fi
|
|
| 34 | +```
|