-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathport-forward.sh
executable file
·132 lines (118 loc) · 2.62 KB
/
port-forward.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/bin/zsh
# bash is stupid here for arrays
# if you are using bash, DO NOT have "space" in other_options
verbose=0
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
--verbose)
verbose=1
;;
--port-config)
source "$2" || exit 1
shift
;;
--forward)
forward="$2"
shift # past argument
;;
--local_port)
local_port="$2"
shift # past argument
;;
--local_addr)
local_addr="$2"
shift # past argument
;;
--remote_port)
remote_port="$2"
shift # past argument
;;
--remote_addr)
remote_addr="$2"
shift # past argument
;;
--identity_file)
identity_file="$2"
shift # past argument
;;
--target_host)
target_host="$2"
shift # past argument
;;
--target_port)
target_port="$2"
shift # past argument
;;
*)
other_cmdlines+=($key)
;;
esac
shift # past argument or value
done
info() { if [ $verbose -eq 1 ]; then >&2 echo "$@"; fi }
warning() { >&2 echo $@; }
dump() {
local arg_string="$1"
shift
for arg in "$@"; do arg_string+=" *$arg*"; done
info "$arg_string"
}
dump "other arguments:" ${other_arguments[@]}
dump "other options:" ${other_options[@]}
dump "other cmdline:" ${other_cmdlines[@]}
for other_argument in ${other_arguments[@]}; do
args+=($other_argument)
done
for other_option in ${other_options[@]}; do
args+=(-o $other_option)
done
for other_cmdline in ${other_cmdlines[@]}; do
args+=($other_cmdline)
done
if [[ "$forward" == "remote" ]]; then forward=-R; fi
if [[ "$forward" == "local" ]]; then forward=-L; fi
if [[ "$forward" == "-R" ]]; then
if [[ "$local_port" == "" ]]; then
warning "local_port not set when forward is remote"
exit 1
fi
if [[ "$remote_port" == "" ]]; then
remote_port="$local_port"
fi
if [[ "$local_addr" == "" ]]; then
local_addr=localhost
fi
if [[ "$remote_addr" != "" ]]; then
remote_addr+=":"
fi
args+=($forward $remote_addr$remote_port":"$local_addr":"$local_port)
fi
if [[ "$forward" == "-L" ]]; then
if [[ "$remote_port" == "" ]]; then
warning "remote_port not set when forward is remote"
exit 1
fi
if [[ "$local_port" == "" ]]; then
local_port="$remote_port"
fi
if [[ "$remote_addr" == "" ]]; then
remote_addr=localhost
fi
if [[ "$local_addr" != "" ]]; then
$local_addr+=":"
fi
args+=($forward $local_addr$local_port":"$remote_addr":"$remote_port)
fi
if [[ "$identity_file" != "" ]]; then
args+=(-i $identity_file)
fi
if [[ "$target_host" != "" ]]; then
args+=($target_host)
fi
if [[ "$target_port" != "" ]]; then
args+=(-p $target_port)
fi
dump "would run " "${args[@]}"
ssh "${args[@]}"