-
Notifications
You must be signed in to change notification settings - Fork 3
/
oazssh.plugin.zsh
242 lines (232 loc) · 7.07 KB
/
oazssh.plugin.zsh
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# vim: set tabstop=4 shiftwidth=4 expandtab softtabstop=4:
# Functions:
export OAZSSH_VERSION="0.7.2"
# azure ssh via azure bastion
function oazssh() {
oazssh_usage() {
echo "Usage: oazssh [-v] [-e <env>] [-k keyfile] [-s srv || -S servername]" 1>&2; return
}
local OPTIND env_val key_val srv_val lport_val rport_val change_srv_name VERBOSE
VERBOSE=0
env_val=dev
key_val=$HOME/.ssh/id_rsa
srv_val=ap0
server_name=
change_srv_name=0
while getopts "ve:k:s:S:l:r:" o; do
case "${o}" in
v)
VERBOSE=1
;;
e)
env_val="${OPTARG}"
;;
k)
key_val="${OPTARG}"
;;
s)
srv_val="${OPTARG}"
;;
S)
server_name="${OPTARG}"
change_srv_name=1
;;
*)
oazssh_usage
return
;;
esac
done
shift $((OPTIND-1))
SUBSCRIPTION=$(az account show --query 'id' --output tsv | tr -d '[\r\n]')
SUBSCRIPTIONNAME="SharedServices"
BASTION=bas-cus-ss-infra-bastion-1
BASTIONRG=rg-cus-ss-infra-network-1
case ${env_val} in
d*)
VMRG=rg-cus-nprod-dev-stibo-app-1
ev="d"
;;
t*|u*)
VMRG=rg-cus-nprod-test-stibo-app-1
ev="t"
;;
q*)
VMRG=rg-cus-nprod-qa-stibo-app-1
ev="q"
;;
x*)
VMRG=rg-cus-nprod-infra-platform-1
ev="x"
;;
pe*)
VMRG=rg-cus-nprod-perf-stibo-app-1
ev="r"
;;
au*)
VMRG=rg-cus-nprod-devops-stibo-app-1
ev="a"
;;
pr*)
VMRG=rg-cus-prod-stibo-app-1
ev="p"
SUBSCRIPTION=$(az account show --name "Prod" --query "id" --output tsv | tr -d '[\r\n]')
;;
ndr*)
VMRG=rg-eus2-nprod-perf-stibo-app-1
ev="f"
SUBSCRIPTION=$(az account show --name "DR-Non-Prod" --query "id" --output tsv | tr -d '[\r\n]')
SUBSCRIPTIONNAME="DR-SharedServices"
BASTION=bas-eus2-ss-infra-bastion-1
BASTIONRG=rg-eus2-ss-infra-network-1
;;
esac
if (( ${change_srv_name} )) ; then
:
# echo "server_name supplied in args"
# echo "server name is .${server_name}."
elif [ ${ev} = "f" ]; then
server_name="az3${ev}lepcmepc${srv_val}"
# echo "server name set to ${server_name}"
else
server_name="az1${ev}lepcmepc${srv_val}"
# echo "server name set to ${server_name}"
fi
if (( ${VERBOSE} > 0 )); then
set -x
fi
az network bastion ssh --name ${BASTION} \
--resource-group ${BASTIONRG} \
--subscription ${SUBSCRIPTIONNAME} \
--target-resource-id /subscriptions/${SUBSCRIPTION}/resourceGroups/${VMRG}/providers/Microsoft.Compute/virtualMachines/${server_name} \
--auth-type ssh-key \
--username adminuser \
--ssh-key ${key_val}
}
# azure ssh port forward via azure bastion
function oazssht() {
oazssht_usage() {
echo "Usage: oazssht [-e <env>] [-k keyfile] [-s srv || -S servername][ -w | -l]" 1>&2;
echo " [-v] [-l local_port] [-r remote_port]"
}
local OPTIND env_val key_val srv_val lport_val rport_val change_srv_name VERBOSE
VERBOSE=0
env_val=dev
key_val=$HOME/.ssh/id_rsa
srv_val=ap0
lport_val=5555
rport_val=22
server_name=
change_srv_name=0
while getopts "ve:k:s:S:l:r:" o; do
case "${o}" in
v)
VERBOSE=1
;;
e)
env_val="${OPTARG}"
;;
k)
key_val="${OPTARG}"
;;
s)
srv_val="${OPTARG}"
;;
S)
server_name="${OPTARG}"
change_srv_name=1
;;
r)
rport_val="${OPTARG}"
;;
l)
lport_val="${OPTARG}"
;;
*)
oazssh_usage
return
;;
esac
done
shift $((OPTIND-1))
SUBSCRIPTION=$(az account show --query 'id' --output tsv | tr -d '[\r\n]')
SUBSCRIPTIONNAME="SharedServices"
BASTION=bas-cus-ss-infra-bastion-1
BASTIONRG=rg-cus-ss-infra-network-1
case ${env_val} in
d*)
VMRG=rg-cus-nprod-dev-stibo-app-1
ev="d"
;;
t*|u*)
VMRG=rg-cus-nprod-test-stibo-app-1
ev="t"
;;
q*)
VMRG=rg-cus-nprod-qa-stibo-app-1
ev="q"
;;
x*)
VMRG=rg-cus-nprod-infra-platform-1
ev="x"
;;
pe*)
VMRG=rg-cus-nprod-perf-stibo-app-1
ev="r"
;;
au*)
VMRG=rg-cus-nprod-devops-stibo-app-1
ev="a"
;;
pr*)
VMRG=rg-cus-prod-stibo-app-1
ev="p"
SUBSCRIPTION=$(az account show --name "Prod" --query "id" --output tsv | tr -d '[\r\n]')
;;
ndr*)
VMRG=rg-eus2-nprod-perf-stibo-app-1
ev="f"
SUBSCRIPTION=$(az account show --name "DR-Non-Prod" --query "id" --output tsv | tr -d '[\r\n]')
SUBSCRIPTIONNAME="DR-SharedServices"
BASTION=bas-eus2-ss-infra-bastion-1
BASTIONRG=rg-eus2-ss-infra-network-1
;;
esac
if (( ${change_srv_name} )) ; then
echo "server name is ${server_name}"
elif [ ${ev} = "f" ]; then
server_name="az3${ev}lepcmepc${srv_val}"
echo "server name set to ${server_name}"
else
server_name="az1${ev}lepcmepc${srv_val}"
echo "server name set to ${server_name}"
fi
if (( ${VERBOSE} > 0 )); then
set -x
fi
set -x
az network bastion tunnel --name ${BASTION} \
--resource-group ${BASTIONRG} \
--subscription ${SUBSCRIPTIONNAME} \
--target-resource-id /subscriptions/${SUBSCRIPTION}/resourceGroups/${VMRG}/providers/Microsoft.Compute/virtualMachines/${server_name} \
--resource-port ${rport_val} \
--port ${lport_val}
}
# For Nexient Azure subscription
function nazssh() {
SUBSCRIPTION=$(az account show --query 'id' --output tsv)
az network bastion ssh --name ${AZENV}-bastion \
--resource-group ${AZENV}-rg \
--target-resource-id /subscriptions/${SUBSCRIPTION}/resourceGroups/${AZENV}-rg/providers/Microsoft.Compute/virtualMachines/${AZENV}-$1 \
--auth-type ssh-key \
--username adminuser \
--ssh-key ~/.ssh/id_rsa
}
function nazssht() {
SUBSCRIPTION=$(az account show --query 'id' --output tsv)
az network bastion tunnel --name ${AZENV}-bastion \
--resource-group ${AZENV}-rg \
--target-resource-id /subscriptions/${SUBSCRIPTION}/resourceGroups/${AZENV}-rg/providers/Microsoft.Compute/virtualMachines/${AZENV}-$1 \
--resource-port 22 \
--port 5555
}