-
Notifications
You must be signed in to change notification settings - Fork 1
/
dupessh
executable file
·52 lines (48 loc) · 1.17 KB
/
dupessh
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
#!/bin/bash
#
# Duplicate existing ssh session running on current machine.
#
# If there are multiple, display list and take a choice.
#
# Usage:
# dupessh [ <case insensitive pattern to match in command line> ]
#
tempfile=`mktemp /tmp/sshdupe.XXXXXX`
trap "rm $tempfile" exit
if [[ "${1:-}" ]]
then
pattern="$1"
else
pattern=.
fi
# grep out bastion host nc proxy connections (for example -
# ssh -q ssh1.vpc1.10gen.cc nc brs-staging-1.vpc1.10gen.cc 22)
filter="grep -v eahdroplet | grep -v ernienet | grep -v ssh.*ssh.*nc.*22 | grep -v ssh.*ssh_tunnel.*\ "
ps -o command -www -a | grep ^ssh | grep -i "$pattern" | bash -c "$filter" | sort -u > $tempfile
count=$(wc -l < $tempfile | tr -d \\t\ )
if [[ $count == 0 ]]
then
# try without filter
#
ps -o command -www -a | grep ^ssh | grep -i "$pattern" | sort -u > $tempfile
count=$(wc -l < $tempfile | tr -d \\t\ )
if [[ $count == 0 ]]
then
echo "No current sessions!"
exit 1
fi
fi
if [[ $count == 1 ]]
then
num=1
else
grep -n . $tempfile | sed 's/:/: /g'
echo -n "Choose session (1-$count): "
read num
fi
cmd=$(head -n $num $tempfile | tail -1 )
echo "COMMAND:"
echo "$cmd"
eval $cmd
echo "COMMAND:"
echo "$cmd"