-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssh_helper2.sh
executable file
·74 lines (68 loc) · 1.5 KB
/
ssh_helper2.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/bash
# set -x
unset='-'
first=${1}
if [[ ${first: :1} == '-' ]]
then
# switch
switch=${1}
shift
fi
server=${1}
shift
command=${@}
########## FUNCTIONS
function log() {
msg=${@}
timestamp=$(date '+%F %T')
if [[ -n ${debug} ]]
then
printf "%18s |%s| %s\n" "${timestamp}" "${version}" "${msg}" | tee -a ${logfile}
else
printf "%18s |%s| %s\n" "${timestamp}" "${version}" "${msg}" >> ${logfile}
fi
}
function pick() {
printf "Not implemented yet"
}
function set_tmux() {
if [[ -n ${TMUX} ]]
then
title="${1}"
session=${TMUX/,*}
tmux -S ${session} rename-window "${title}"
fi
}
function run_ssh_command() {
check_sock
ssh -o BatchMode=yes -o StrictHostKeyChecking=no -o ConnectTimeout=10 ${switch} ${server} "${command[@]}"
}
function run_ssh() {
check_sock
set_tmux ${server/.dap*}
ssh -o BatchMode=yes -o StrictHostKeyChecking=no -o ConnectTimeout=10 ${server}
ssh_result=$?
set_tmux ${unset}
}
function check_sock() {
csock=$(find /tmp -name 'agent*' -printf '%Ts %p\n' 2> /dev/null | sort | tail -n1 | cut -d' ' -f2)
if [[ -z ${SSH_AUTH_SOCK} ]] || [[ ${csock} != ${SSH_AUTH_SOCK} ]]
then
printf "Renewing SOCK %s\n" "${csock}"
export SSH_AUTH_SOCK=${csock}
else
printf "Sock not changed: %s\n" "${csock}"
fi
}
if [[ -n ${server} ]]
then
if [[ -z ${command} ]]
then
run_ssh
else
run_ssh_command
fi
else
pick
fi
# set +x