Fish shell或bash shell下的ssh-agent现有会话探测脚本:修订间差异

来自三线的随记
无编辑摘要
无编辑摘要
 
(未显示同一用户的2个中间版本)
第1行: 第1行:
一个随便糊的用于发现设备上现有的ssh-agent会话脚本,方便在不同的terminal tab中复用一个ssh-agent
一个随便糊的用于发现设备上现有的ssh-agent会话脚本,方便在不同的terminal tab中复用一个ssh-agent


适配: macOS + fish shell
适配:  
 
=== macOS + fish shell ===
  #!/usr/bin/env fish
  #!/usr/bin/env fish
   
   
第8行: 第10行:
  # To find the exists ssh-agent connection or create
  # To find the exists ssh-agent connection or create
  # a new ssh-agent connection for macOS with fish shell
  # a new ssh-agent connection for macOS with fish shell
# version: 1.3
  #######################################################
  #######################################################
   
   
  #set base_dir "/var/folders/4w/hx1kgdwd2ll9chvbzv6y70r80000gn/T"
  #set base_dir "/var/folders/4w/hx1kgdwd2ll9chvbzv6y70r80000gn/T"
  set base_dir $TMPDIR
  # Linux
# /tmp/ssh-*/agent.*
   
   
  for i in (sh -c "ls -1 --color=never $base_dir/ssh-*/agent* 2>/dev/null")
# macOS
set base_dir "$TMPDIR/ssh-*/agent.*"
  for i in (sh -c "ls -1 --color=never $base_dir 2>/dev/null")
   set agent_file (echo -n $i | grep --color=never -Eo "agent.*")
   set agent_file (echo -n $i | grep --color=never -Eo "agent.*")
   echo "found: $agent_file"
   echo "found: $agent_file"
第27行: 第34行:
  echo "ssh agent sock file not found, creating"
  echo "ssh agent sock file not found, creating"
  eval (ssh-agent -c -t 4h)
  eval (ssh-agent -c -t 4h)
#ssh-add -t 0
ssh-add -t 360d


=== Linux bash: ===
#!/usr/bin/env bash
#######################################################
# Author: SanXian
# To find the exists ssh-agent connection or create
# a new ssh-agent connection for macOS with fish shell
# version: 1.5
#######################################################
#set base_dir "/var/folders/4w/hx1kgdwd2ll9chvbzv6y70r80000gn/T"
# Linux
base_dir=/tmp/ssh-*/agent.*
# macOS
#set base_dir $TMPDIR/ssh-*/agent.*
agent_paths=`sh -c "ls -1 --color=never $base_dir 2>/dev/null"`
for i in $agent_paths
do
  agent_file=`echo -n $i | grep --color=never -Eo "agent.*"`
  echo "found: $agent_file"
  PID=`echo -n $agent_file | awk -F . '{print $2 + 1}'`
  echo PID: $PID
  export SSH_AGENT_PID=$PID
  export SSH_AUTH_SOCK=$i
  env | grep -i ssh_
done
if <nowiki>[[ -z $agent_paths ]]</nowiki>
then
  # ssh agent sock not found
  echo "ssh agent sock file not found, creating"
  eval `ssh-agent -t 4h`
  #ssh-add -t 0
  ssh-add -t 360d
fi


Linux bash:
=== 食用方法 ===
<pre><nowiki>
执行:
#!/usr/bin/env bash
source ssh-search
 
#######################################################
. ssh-search
# Author: SanXian
# To find the exists ssh-agent connection or create
# a new ssh-agent connection for macOS with fish shell
# version: 1.5
#######################################################


#set base_dir "/var/folders/4w/hx1kgdwd2ll9chvbzv6y70r80000gn/T"
# Linux
base_dir=/tmp/ssh-*/agent.*


# macOS
扩展阅读:
#set base_dir $TMPDIR/ssh-*/agent.*
agent_paths=`sh -c "ls -1 --color=never $base_dir 2>/dev/null"`
for i in $agent_paths
do
  agent_file=`echo -n $i | grep --color=never -Eo "agent.*"`
  echo "found: $agent_file"
  PID=`echo -n $agent_file | awk -F . '{print $2 + 1}'`
  echo PID: $PID
  export SSH_AGENT_PID=$PID
  export SSH_AUTH_SOCK=$i
  env | grep -i ssh_
done


if [[ -z $agent_paths ]]
[[SSH-add脚本化载入带密码(passphrase)的私钥]]
then
  # ssh agent sock not found
  echo "ssh agent sock file not found, creating"
  eval `ssh-agent -t 4h`
  #ssh-add -t 0
  ssh-add -t 360d
fi
</nowiki>
</pre>
[[分类:Linux]]
[[分类:Linux]]
[[分类:MacOS]]
[[分类:MacOS]]
[[分类:SSH]]
[[分类:SSH]]

2022年7月17日 (日) 17:14的最新版本

一个随便糊的用于发现设备上现有的ssh-agent会话脚本,方便在不同的terminal tab中复用一个ssh-agent

适配:

macOS + fish shell

#!/usr/bin/env fish

#######################################################
# Author: SanXian
# To find the exists ssh-agent connection or create
# a new ssh-agent connection for macOS with fish shell
# version: 1.3
#######################################################

#set base_dir "/var/folders/4w/hx1kgdwd2ll9chvbzv6y70r80000gn/T"
# Linux
# /tmp/ssh-*/agent.*

# macOS
set base_dir "$TMPDIR/ssh-*/agent.*"

for i in (sh -c "ls -1 --color=never $base_dir 2>/dev/null")
  set agent_file (echo -n $i | grep --color=never -Eo "agent.*")
  echo "found: $agent_file"
  set PID (echo -n $agent_file | awk -F . '{print $2 + 1}')
  echo PID: $PID
  export  SSH_AGENT_PID=$PID
  export  SSH_AUTH_SOCK=$i
  env | grep -i ssh_
  exit
end

# ssh agent sock not found
echo "ssh agent sock file not found, creating"
eval (ssh-agent -c -t 4h)
#ssh-add -t 0
ssh-add -t 360d

Linux bash:

#!/usr/bin/env bash

#######################################################
# Author: SanXian
# To find the exists ssh-agent connection or create
# a new ssh-agent connection for macOS with fish shell
# version: 1.5
#######################################################

#set base_dir "/var/folders/4w/hx1kgdwd2ll9chvbzv6y70r80000gn/T"
# Linux
base_dir=/tmp/ssh-*/agent.*

# macOS
#set base_dir $TMPDIR/ssh-*/agent.*
agent_paths=`sh -c "ls -1 --color=never $base_dir 2>/dev/null"`
for i in $agent_paths
do
  agent_file=`echo -n $i | grep --color=never -Eo "agent.*"`
  echo "found: $agent_file"
  PID=`echo -n $agent_file | awk -F . '{print $2 + 1}'`
  echo PID: $PID
  export SSH_AGENT_PID=$PID
  export SSH_AUTH_SOCK=$i
  env | grep -i ssh_
done

if [[ -z $agent_paths ]]
then
  # ssh agent sock not found
  echo "ssh agent sock file not found, creating"
  eval `ssh-agent -t 4h`
  #ssh-add -t 0
  ssh-add -t 360d
fi

食用方法

执行:

source ssh-search

. ssh-search


扩展阅读:

SSH-add脚本化载入带密码(passphrase)的私钥