-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjump
executable file
·80 lines (60 loc) · 2.19 KB
/
jump
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
75
76
77
78
79
80
#!/bin/bash
opts=hbckn:
bashrc=false
knownhosts=false
function _usage {
cat << _USAGE_
`basename $0`: log on to a server through a jump host
Usage:
`basename $0` [-$opts] <dns>
Options:
-h you are here
-b port $HOME/.bashrc to the target machine (default: $bashrc)
-c run necessary config
-k remove dns entry from known_hosts file (default: $knownhosts)
-n <name> name the target machine (used only with \`-b\`)
_USAGE_
}
function setup {
[ -z $JUMP_HOST ] \
&& echo -n "jump host (config alias or dns): " \
&& read jhost \
&& echo "export JUMP_HOST=$jhost" >> $HOME/.bash_profile
[ -z $JUMP_USER ] \
&& echo -n "Jump Host Username: " \
&& read juser \
&& echo "export JUMP_USER=$juser">> $HOME/.bash_profile
[ -z $JUMP_KEY ] \
&& echo -n "absolute path to jump host key: " \
&& read jkey \
&& echo "export JUMP_KEY=$jkey">> $HOME/.bash_profile
[ -z $JUMP_PORT ] \
&& echo -n "jump port: " \
&& read jport \
&& echo "export JUMP_PORT=$jport">> $HOME/.bash_profile
}
while getopts $opts opt; do
case $opt in
h) _usage; exit ;;
b) bashrc=true ;;
c) setup; exit ;;
k) knownhosts=true ;;
n) name="$OPTARG" ;;
?|*) _usage; exit 2 ;;
esac
done
shift $(($OPTIND-1))
[ -z $JUMP_HOST ] && { echo "JUMP_HOST not set (try `basename $0` -c)" >&2; exit 1; }
[ -z $JUMP_USER ] && { echo "JUMP_USER not set (try `basename $0` -c)" >&2; exit 1; }
[ -z $JUMP_KEY ] && { echo "JUMP_KEY not set (try `basename $0` -c)" >&2; exit 1; }
: ${JUMP_PORT:=22 }
proxy="ssh -W %h:%p $JUMP_HOST"
[ -z $1 ] && { ssh -p $JUMP_PORT -i $JUMP_KEY $JUMP_USER@$JUMP_HOST; exit; }
$knownhosts && ssh-keygen -R $1
! $bashrc && { ssh -p $JUMP_PORT -i $JUMP_KEY -oProxyCommand="$proxy" $JUMP_USER@$1; exit; }
[ ! -f $HOME/.bashrc ] && { echo "$HOME/.bashrc doesn't exist" >&2; exit 1; }
t=$(mktemp /tmp/jump.bashrc.XXXX)
[ -n "${name#++([[:space:]])}" ] && echo "export HOSTNAME=$name" >> $t
cat $HOME/.bashrc >> $t
scp -p $JUMP_PORT -i $JUMP_KEY -oProxyCommand="$proxy" $t $JUMP_USER@$1:/home/$JUMP_USER/.bashrc \
&& { rm -f $t; ssh -p $JUMP_PORT -i $JUMP_KEY -oProxyCommand="$proxy" $JUMP_USER@$1; }