-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgetNwEnv
More file actions
executable file
·779 lines (677 loc) · 16.1 KB
/
Copy pathgetNwEnv
File metadata and controls
executable file
·779 lines (677 loc) · 16.1 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
#!/bin/zsh
#
# Copyright (c) Chris Jenkins 2019, 2020
#
# Licensed under the Universal Permissive License v 1.0 as shown
# at http://oss.oracle.com/licenses/upl
#
############################################################
# reset all shell options to default
emulate -LR zsh
# set required options
setopt nullglob
setopt shwordsplit
setopt ksharrays
############################################################
# reset PATH to system default
eval "$(/usr/libexec/path_helper)"
#
# Some constants
#
readonly tmpTemplate="/tmp/gne.XXXXXX"
readonly stNone="N"
readonly stEthernet="E"
readonly stWiFi="W"
readonly stUSB="U"
readonly stBluetooth="B"
readonly stVPN="V"
#
# The supported environments
#
readonly integer numEnvs=13
declare -a envName=( "" )
declare -a envSrvType=( "" )
declare -a envDesc=( "" )
#
# Environment names
#
envName[0]="None"
envName[1]="Home-LAN"
envName[2]="Home-WiFi"
envName[3]="Work-LAN"
envName[4]="Work-WiFi-Internal"
envName[5]="Work-WiFi-Public"
envName[6]="Home-Guest"
envName[7]="iPhone-WiFi"
envName[8]="iPad-WiFi"
envName[9]="iPhone-USB_BT"
envName[10]="Home-VPN"
envName[11]="Work-VPN"
envName[12]="Nord-VPN"
#
# Environment descriptions
#
envDesc[0]="No active network connections"
envDesc[1]="Wired connection to the Home network"
envDesc[2]="Connection to the Home WiFi network"
envDesc[3]="Wired connection to the Work network"
envDesc[4]="Connection to the Work Corporate WiFi network"
envDesc[5]="Connection to the Work Public WiFi network"
envDesc[6]="Connection to the Home Guest WiFi network"
envDesc[7]="Tethered to iPhone via WiFi"
envDesc[8]="Tethered to iPad via WiFi"
envDesc[9]="Tethered to iPhone or iPad via USB or Bluetooth"
envDesc[10]="Connection to Home VPN"
envDesc[11]="Connection to Work VPN"
envDesc[12]="Connection to Nord VPN"
#
# Environment types
#
envSrvType[0]=${stNone}
envSrvType[1]=${stEthernet}
envSrvType[2]=${stWiFi}
envSrvType[3]=${stEthernet}
envSrvType[4]=${stWiFi}
envSrvType[5]=${stWiFi}
envSrvType[6]=${stWiFi}
envSrvType[7]=${stWiFi}
envSrvType[8]=${stWiFi}
envSrvType[9]=${stUSB}
envSrvType[10]=${stVPN}
envSrvType[11]=${stVPN}
envSrvType[12]=${stVPN}
#
# macOS network service info
#
readonly integer numMatchEntries=9
declare -a matchString=( "" )
declare -a matchType=( "" )
declare -a matchIface=( "" )
matchString[0]="Ethernet"
matchType[0]="${stEthernet}"
matchIface[0]=""
matchString[1]="USB-C LAN"
matchType[1]="${stEthernet}"
matchIface[1]=""
matchString[2]="Wi-Fi"
matchType[2]="${stWiFi}"
matchIface[2]=""
matchString[3]="iPhone USB"
matchType[3]="${stUSB}"
matchIface[3]=""
matchString[4]="iPad USB"
matchType[4]="${stUSB}"
matchIface[4]=""
matchString[5]="Bluetooth PAN"
matchType[5]="${stBluetooth}"
matchIface[5]=""
matchString[6]="L2TP"
matchType[6]="${stVPN}"
matchIface[6]="ppp0"
matchString[7]="Cisco VPN"
matchType[7]="${stVPN}"
matchIface[7]="utun0"
matchString[8]="Nord VPN"
matchType[8]="${stVPN}"
matchIface[8]="ipsec0"
# Service data for Cisco AnyConnect VPN / OpenConnect VPN
readonly pathAnyConnect="/Applications/Cisco/Cisco AnyConnect Secure Mobility Client.app"
readonly pathOpenConnect="/Applications/OpenConnect VPN.app"
readonly infoAnyConnect="(1) AnyConnect VPN
(Hardware Port: Cisco VPN, Device: utun0)"
# Service data for Nord VPN
readonly pathNordVPN="/Applications/NordVPN.app"
readonly infoNordVPN="(1) Nord VPN
(Hardware Port: Nord VPN, Device: utun0)"
# Service data for Nord VPN IKE
readonly pathNordVPNIKE="/Applications/NordVPN IKE.app"
readonly infoNordVPNIKE="(1) Nord VPN IKE
(Hardware Port: Nord VPN IKE, Device: ipsec0)"
# Service data for Shimo VPN using OpenVPN
readonly pathShimo="/Applications/Shimo.app"
readonly infoShimo="(1) Shimo VPN
(Hardware Port: OpenVPN, Device: utun0)"
readonly localv6pfx="fd00::"
readonly integer minService=1
readonly integer maxService=9
integer numServices=0
declare -a srvType=( "" )
declare -a srvName=( "" )
declare -a srvIface=( "" )
declare -a srvActive=( 0 )
integer vpnConfigured=1
#
# IP matching environments
#
declare -a getIPcmd=( "" )
declare -a ipCheckPrefix=( "" )
declare -a ipEnvInd=( 0 )
readonly integer numIpItems=7
#
# IP Prefixes (v4 or v6)
#
# WorkVPN, v6 only
ipCheckPrefix[0]="2606:b400:8f0:"
# HomeLAN, v6 only
ipCheckPrefix[1]="2001:470:1f09:2df:"
# WorkLAN, v6 only
ipCheckPrefix[2]="2606:b400:"
# iPhone USB or BT, v4 only
ipCheckPrefix[3]="172.20.10."
# HomeVPN, v4 only
ipCheckPrefix[4]="10.10.200."
ipCheckPrefix[5]="10.50.200."
# NordVPN, v4 only
ipCheckPrefix[6]="10."
#
# Associated 'getmyip' commands
#
# WorkVPN, v6 only
getIPcmd[0]="getmyip vpn -6 -g"
# HomeLAN, v6 only
getIPcmd[1]="getmyip wired -6 -g"
# WorkLAN, v6 only
getIPcmd[2]="getmyip wired -6 -g"
# iPhone USB or BT, v4 only
getIPcmd[3]="getmyip wired -4"
# HomeVPN, v4 only
getIPcmd[4]="getmyip vpn -4"
getIPcmd[5]="getmyip vpn -4"
# NordVPN, v4 only
getIPcmd[6]="getmyip vpn -4"
#
# Map matched environments to defined environments
#
# WorkVPN, v6 only
ipEnvInd[0]=11
# HomeLAN, v6 only
ipEnvInd[1]=1
# WorkLAN, v6 only
ipEnvInd[2]=3
# iPhone USB or BT, v4 only
ipEnvInd[3]=9
# HomeVPN, v4 only
ipEnvInd[4]=10
ipEnvInd[5]=10
# NordVPN, v4 only
ipEnvInd[6]=12
#
# WiFi matched environments
#
declare -a getWIFIcmd
declare -a wifiNwName
declare -ai wifiEnvInd
declare -ir numWiFiItems=6
#
# Command to determine SSID
#
getWIFIcmd[0]="getwifinetworks"
getWIFIcmd[1]="getwifinetworks"
getWIFIcmd[2]="getwifinetworks"
getWIFIcmd[3]="getwifinetworks"
getWIFIcmd[4]="getwifinetworks"
getWIFIcmd[5]="getwifinetworks"
#
# Network name SSID
#
wifiNwName[0]="Home WiFi"
wifiNwName[1]="Work WiFi Corporate"
wifiNwName[2]="Work WiFi Public"
wifiNwName[3]="My iPhone"
wifiNwName[4]="My iPad"
wifiNwName[5]="Guest WiFi"
#
# Map SSID to environment
#
wifiEnvInd[0]=2
wifiEnvInd[1]=4
wifiEnvInd[2]=5
wifiEnvInd[3]=7
wifiEnvInd[4]=8
wifiEnvInd[5]=6
declare cmd=""
declare -i ret=1
declare -i doAll=1
declare -i foundMatch=1
usage()
{
echo
echo "Usage:"
echo
echo " getNwEnv help"
echo " getNwEnv show [-all]"
echo " getNwEnv list [-verbose]"
echo
echo "Displays the name(s) of the currently active recognised network environment(s)."
echo "If '-all' is specified, lists all active environments otherwise lists just"
echo "the primary one."
echo
if [[ $# -gt 0 ]]
then
echo "Recognised environments and their names are:"
echo
echo " None - No active network connections"
echo
echo " Home-VPN - Connected to Home VPN."
echo
echo " Work-VPN - Connected to Work VPN."
echo
echo " Nord-VPN - Connected to Nord VPN."
echo
echo " Home-LAN - A wired connection to the Home network."
echo
echo " Home-WiFi - A WiFi connection to the Home network."
echo
echo " Home-Guest - A WiFi connection to the Home guest network."
echo
echo " iPhone-USB_BT - Tethered to iPhone/iPad via USB or Bluetooth."
echo
echo " iPhone-WiFi - Tethered to iPhone via WiFi."
echo
echo " iPad-WiFi - Tethered to iPad via WiFi."
echo
echo " Work-LAN - A wired connection to the Oracle network."
echo
echo " Work-WiFi-Internal"
echo " - Connected to work corporate WiFi network"
echo
echo " Work-WiFi-Public"
echo " - Connected to work public WiFi network."
echo
echo "If at least one recognised environment is listed the exit code is 0 otherwise"
echo "it is > 0."
echo
fi
exit 2
}
listEnvs()
{
local prev=""
local -i n=0
local -i verbose=1
if [[ $# -gt 0 ]]
then
verbose=0
fi
while [[ ${n} -lt ${numEnvs} ]]
do
if [[ "${envName[${n}]}" != "${prev}" ]]
then
if [[ ${verbose} -eq 0 ]]
then
echo "${envName[${n}]}:${envDesc[${n}]}"
else
echo "${envName[${n}]}"
fi
fi
prev="${envName[${n}]}"
n=${n}+1
done
return 0
}
getV4Address()
{
local -i ret=0
local addr=""
if [[ $# -eq 1 ]]
then
addr=`ifconfig "$1" 2>/dev/null | grep "inet " | sed -e 's/.*inet //' -e 's/ netmask.*$//' -e 's/ --> .*//'`
fi
if [[ "${addr}" != "" ]]
then
echo ${addr}
else
ret=1
fi
return ${ret}
}
getV6Address()
{
local -i ret=0
local addr=""
if [[ $# -eq 1 ]]
then
addr=`ifconfig "$1" 2>/dev/null | grep "inet6 " | grep -v " scopeid " | grep -v "temporary" | grep -v "${localv6pfx}" | sed -e 's/.*inet6 //' -e 's/ prefixlen.*$//'`
fi
if [[ "${addr}" != "" ]]
then
echo ${addr}
else
ret=1
fi
return ${ret}
}
addService()
{
local sType
local sName
local sIface
local -i n=0
sType="${matchType[$1]}"
sName=`echo "$2" | head -n 1 | sed -e 's/^([0-9]*) //'`
if [[ "${matchIface[$1]}" != "" ]]
then
sIface="${matchIface[$1]}"
else
sIface=`echo "$2" | tail -n 1 | sed -e 's/^.* Device: //' -e 's/).*$//'`
fi
while [[ ${n} -lt ${numServices} ]]
do
if [[ "${sIface}" == "${srvIface[${n}]}" ]]
then
return 0
fi
n=${n}+1
done
srvType[${numServices}]="${sType}"
if [[ "${sType}" == "${stVPN}" ]]
then
vpnConfigured=0
fi
srvName[${numServices}]="${sName}"
srvIface[${numServices}]="${sIface}"
srvActive[${numServices}]=1
getV6Address "${srvIface[${numServices}]}" >& /dev/null
if [[ $? -eq 0 ]]
then
srvActive[${numServices}]=0
else
getV4Address "${srvIface[${numServices}]}" >& /dev/null
if [[ $? -eq 0 ]]
then
srvActive[${numServices}]=0
fi
fi
numServices=${numServices}+1
return 0
}
getServices()
{
local tmpf=""
local srvinfo
local -i n=${minService}
local -i i=0
tmpf=`mktemp -q "${tmpTemplate}"`
if [[ "${tmpf}" = "" ]]
then
return 1
fi
networksetup -listnetworkserviceorder 2>/dev/null | grep '^(' > "${tmpf}"
while [[ ${n} -le ${maxService} ]]
do
srvinfo=`sed -e "/^(${n})/,/^(H/!d" < "${tmpf}"`
if [[ "${srvinfo}" != "" ]]
then
i=0
while [[ ${i} -lt ${numMatchEntries} ]]
do
echo "${srvinfo}" | grep -q "Hardware Port: .*${matchString[${i}]}"
if [[ $? -eq 0 ]]
then
addService ${i} "${srvinfo}"
i=${numMatchEntries}
else
i=${i}+1
fi
done
fi
n=${n}+1
done
if [[ -d "${pathAnyConnect}" ]] || \
[[ -d "${pathOpenConnect}" ]]
then
i=${numMatchEntries}-2
srvinfo="${infoAnyConnect}"
addService ${i} "${srvinfo}"
fi
if [[ -d "${pathNordVPN}" ]]
then
i=${numMatchEntries}-2
srvinfo="${infoNordVPN}"
addService ${i} "${srvinfo}"
fi
if [[ -d "${pathNordVPNIKE}" ]]
then
i=${numMatchEntries}-1
srvinfo="${infoNordVPNIKE}"
addService ${i} "${srvinfo}"
fi
if [[ -d "${pathShimo}" ]]
then
i=${numMatchEntries}-3
srvinfo="${infoShimo}"
addService ${i} "${srvinfo}"
fi
rm -f "${tmpf}" >/dev/null 2>&1
return 0
}
prefixMatches()
{
local ip="$1"
local pfx="$2"
local -i ret
echo "${ip}" | grep -q "^${pfx}" >/dev/null 2>&1
ret=$?
return ${ret}
}
checkIPmatches()
{
local ipaddr
local -i ret=1
local -i vpn=$2
local -i n=0
while [[ ${n} -lt ${numIpItems} ]]
do
if [[ $# -gt 0 ]]
then
ipaddr="$1"
else
ipaddr=`${getIPcmd[${n}]}`
fi
if [[ $? -eq 0 ]]
then
if prefixMatches "${ipaddr}" "${ipCheckPrefix[${n}]}"
then
if [[ ${vpn} -eq 0 ]] && \
[[ ${envSrvType[${ipEnvInd[${n}]}]} == ${stVPN} ]]
then
foundMatch=0
ret=0
echo "${envName[${ipEnvInd[${n}]}]}"
n=${numIpItems}
elif [[ ${vpn} -eq 1 ]] && \
[[ ${envSrvType[${ipEnvInd[${n}]}]} != ${stVPN} ]]
then
foundMatch=0
ret=0
echo "${envName[${ipEnvInd[${n}]}]}"
n=${numIpItems}
fi
fi
fi
n=${n}+1
done
return ${ret}
}
checkWiFiMatches()
{
local wifinm
local -i ret=1
local -i n=0
while [[ ${n} -lt ${numWiFiItems} ]]
do
if [[ $# -gt 0 ]]
then
wifinm="$1"
else
wifinm=`${getWIFIcmd[${n}]}`
fi
if [[ $? -eq 0 ]]
then
if [[ "${wifinm}" = "${wifiNwName[${n}]}" ]]
then
foundMatch=0
ret=0
echo "${envName[${wifiEnvInd[${n}]}]}"
n=${numWiFiItems}
fi
fi
n=${n}+1
done
return ${ret}
}
checkVPNActive()
{
local -i n=0
local ipaddr=""
local vpnif=""
for vpnif in `getnwinterfaces vpn -active`
do
ipaddr=`getV6Address "${vpnif}"`
if [[ "${ipaddr}" == "" ]]
then
ipaddr=`getV4Address "${vpnif}"`
fi
if [[ "${ipaddr}" != "" ]]
then
checkIPmatches "${ipaddr}" 0
fi
if [[ ${foundMatch} -eq 0 ]] && [[ ${doAll} -eq 1 ]]
then
return 0
fi
done
return 0
}
checkActive()
{
local -i n=0
local -i done
local ipaddr=""
local nwname=""
if [[ ${foundMatch} -eq 1 ]] || [[ ${doAll} -eq 0 ]]
then
done=1
else
done=0
fi
while [[ ${n} -lt ${numServices} ]] && [[ ${done} -ne 0 ]]
do
if [[ ${srvActive[${n}]} -eq 0 ]]
then
case "${srvType[${n}]}" in
${stEthernet}|${stUSB}|${stBluetooth})
ipaddr=`getV6Address "${srvIface[${n}]}"`
if [[ "${ipaddr}" == "" ]]
then
ipaddr=`getV4Address "${srvIface[${n}]}"`
fi
if [[ "${ipaddr}" != "" ]]
then
checkIPmatches "${ipaddr}" 1
fi
;;
${stWiFi})
nwname=`networksetup -getairportnetwork "${srvIface[${n}]}" 2>/dev/null | grep '^Current Wi-Fi Network:' | sed -e 's/^Current Wi-Fi Network: //'`
if [[ "${nwname}" != "" ]]
then
checkWiFiMatches "${nwname}"
fi
;;
esac
fi
n=${n}+1
if [[ ${foundMatch} -eq 1 ]] || [[ ${doAll} -eq 0 ]]
then
done=1
else
done=0
fi
done
return 0
}
doShow()
{
local -i doAll=1
local -i ret=1
if [[ $# -gt 1 ]]
then
usage
elif [[ $# -eq 1 ]]
then
if [[ "$1" == "-all" ]]
then
doAll=0
else
usage
fi
fi
getServices
if [[ ${vpnConfigured} -eq 0 ]]
then
checkVPNActive
fi
if [[ ${foundMatch} -eq 1 ]] || [[ ${doAll} -eq 0 ]]
then
checkActive
fi
if [[ ${foundMatch} -eq 0 ]]
then
ret=0
fi
if [[ ${ret} -eq 1 ]]
then
if ! getnwinterfaces all -active >& /dev/null
then
echo "${envName[0]}"
ret=0
fi
fi
return ${ret}
}
doList()
{
local -i doVerbose=1
if [[ $# -gt 1 ]]
then
usage
elif [[ $# -eq 1 ]]
then
if [[ "$1" == "-verbose" ]]
then
doVerbose=0
else
usage
fi
fi
if [[ ${doVerbose} -eq 0 ]]
then
listEnvs Y
else
listEnvs
fi
return 0
}
if [[ $# -eq 0 ]]
then
usage
fi
cmd="$1"
shift
case "${cmd}" in
"show")
doShow "$@"
ret=$?
;;
"list")
doList "$@"
ret=$?
;;
"help")
usage Y
;;
*)
usage
;;
esac
exit ${ret}