forked from rocco/osx-wifi-location-changer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlocationchanger
executable file
·136 lines (117 loc) · 7.08 KB
/
locationchanger
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
#!/bin/bash
# automatically change configuration of Mac OS X based on location
# author: Rocco Georgi <[email protected]>
# version: 0.3.1
# original author: Onne Gorter <[email protected]>
# url: http://tech.inhelsinki.nl/locationchanger/
# version: 0.4
# redirect all IO to a logfile
#exec &>/usr/local/var/log/locationchanger.log
# redirect all IO to /dev/null (comment this in if you don#t want to write to logfile)
#exec 1>/dev/null 2>/dev/null
# get a little breather before we get data for things to settle down
sleep 2
# get SSID
SSID=`/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I | grep ' SSID:' | cut -d ':' -f 2 | tr -d ' '`
echo `date` "New SSID found: $SSID"
# empty location var
LOCATION=
# LOCATIONS
# (use to be used Location name here)
# =============================================
Location_Automatic="Automatic"
Location_Canal="Canal+ Network"
Location_Voddev="Canal+ Network by Voddev"
# SSIDS
# =====
SSID_Voddev=NETGEAR15-5G
SSID_Air_Port=myAirPort
SSID_MyWifi=myWIFI
PARAM_Canal=canal
PARAM_Voddev=voddev
PARAM_Auto=auto
# ... add more there
# check parameter instead of detection if not defined use wifi ssid mapping
if [ -z "$1" ]
then
# SSID -> LOCATION mapping
case $SSID in
$SSID_Voddev ) LOCATION="$Location_Voddev";;
$SSID_Air_Port ) LOCATION="$Location_Voddev";;
$SSID_MyWifi ) LOCATION="$Location_Canal";;
# ... add more here
esac
REASON="SSID changed to $SSID"
else
case $1 in
$PARAM_Voddev ) LOCATION="$Location_Voddev";;
$PARAM_Canal ) LOCATION="$Location_Canal";;
$PARAM_Auto ) LOCATION="$Location_Automatic";;
esac
REASON="Force config param : $1 "
fi
# still didn't get a location -> use Location_Automatic
if [ -z "$LOCATION" ]; then
if nc -z bluecoatproxy.cpgrp.root.local 8080; then
LOCATION="$Location_Voddev"
REASON="Automatic Fallback but enabled proxy config"
else
LOCATION="$Location_Automatic"
REASON="Automatic Fallback"
fi
fi
# change network location
scselect "$LOCATION"
case $LOCATION in
$Location_Automatic )
launchctl setenv
launchctl unsetenv proxy_host
launchctl unsetenv proxy_port
launchctl unsetenv http_proxy
launchctl unsetenv https_proxy
launchctl unsetenv HTTP_PROXY
launchctl unsetenv HTTPS_PROXY
launchctl unsetenv no_proxy
launchctl unsetenv NO_PROXY
launchctl setenv SBT_OPTS "-Dfile.encoding=UTF8 -Dsbt.override.build.repos=true"
launchctl setenv CODE_REPO_HOST "voddev.canal-plus.io:8081"
;;
$Location_Voddev )
PROXY_HOSTNAME="bluecoatproxy.cpgrp.root.local"
PROXY_PORT="8080"
launchctl setenv proxy_host "$PROXY_HOSTNAME"
launchctl setenv proxy_port "$PROXY_PORT"
launchctl setenv http_proxy "http://$PROXY_HOSTNAME:$PROXY_PORT/"
launchctl setenv https_proxy "http://$PROXY_HOSTNAME:$PROXY_PORT/"
launchctl setenv HTTP_PROXY "$http_proxy"
launchctl setenv HTTPS_PROXY "$http_proxy"
launchctl setenv no_proxy "localhost,127.0.0.0/8,172.30.0.0/24,194.4.237.58,frcp03vin0013.cpdev.local,frcp03vdv0073.cpdev.local,fo1-pp.app.vod.cdn,fo2-pp.app.vod.cdn,fo3-pp.app.vod.cdn,db-pp.app.vod.cdn,db1-pp.app.vod.cdn,db2-pp.app.vod.cdn,db3-pp.app.vod.cdn,db-rec-1.app.vod.cdn,cvs1.app.vod.cdn,gesc0spacewlk01.msdb.coprod.cdn,gesc0report01.msdb.coprod.cdn,192.168.236.199,192.168.236.19,192.168.136.68,lb-crapaud-vabf.test.cdn,172.30.70.10,ldap.canal-bis.com:636,quova-pp.app.vod.cdn,cas01.app-g3.vod.cdn,dvssoft.app-g3.vod.cdn,lb-prdrm-vabf.backservices.coprod.cdn,lb-mdrm-vabf.backservices.coprod.cdn,10.251.84.11,192.168.139.1,10.100.0.0/8,10.200.0.0/8,10.210.0.0/8,10.220.0.0/8,10.230.0.0/8,10.240.0.0/8,10.250.0.0/8,172.19.0.0/8,172.23.32.0/20,172.23.103.192/26,172.23.38.161,172.23.38.192,c-infra-ns03.mg.vabf.coprod.eu-west-1.aws,172.30.5.17,172.30.5.16,/var/run/docker.sock,mg.prod.eu-west-1.aws,mg.int.coprod.eu-west-1.aws,*eu-west-1.elb.amazonaws.com"
launchctl setenv NO_PROXY "$no_proxy"
launchctl setenv SBT_OPTS "-Dfile.encoding=UTF8 -Dsbt.override.build.repos=true -Dhttp.proxyHost=bluecoatproxy.cpgrp.root.local -Dhttp.proxyPort=8080 -Dhttp.nonProxyHosts=localhost|*.mg.prod.eu-west-1.aws|*.mg.int.coprod.eu-west-1.aws|127.*|[::1]|172.30.95.*|172.23.*|atgbo-n7|172.17.*|172.30.5.16|172.30.5.17|lb-prdrm-vabf.backservices.coprod.cdn|lb-mdrm-vabf.backservices.coprod.cdn|coprod-der-repo01"
;;
$Location_Canal )
SERVICE_GUID=`printf "open\nget State:/Network/Global/IPv4\nd.show" | scutil | grep "PrimaryService" | awk '{print $3}'`
SERVICE_NAME=`printf "open\nget Setup:/Network/Service/$SERVICE_GUID\nd.show" | scutil | grep "UserDefinedName" | awk -F': ' '{print $2}'`
PROXY_HOSTNAME=`networksetup -getwebproxy "$SERVICE_NAME" | awk {'print $2'} | awk 'FNR == 2 {print}' | cut -d' ' -f2`
PROXY_PORT=`networksetup -getwebproxy "$SERVICE_NAME" | awk {'print $2'} | awk 'FNR == 3 {print}' | cut -d' ' -f2`
PROXY_USER=`security find-internet-password -s "$PROXY_HOSTNAME" | grep "acct" | cut -d '"' -f 4`
PROXY_PASS=`security 2>&1 >/dev/null find-internet-password -gs "$PROXY_HOSTNAME" | cut -d '"' -f 2`
launchctl setenv proxy_host "$PROXY_HOSTNAME"
launchctl setenv proxy_port "$PROXY_PORT"
if [[ $PROXY_USER == *"security"* ]]; then
launchctl setenv http_proxy "http://$PROXY_HOSTNAME:$PROXY_PORT/"
launchctl setenv https_proxy "http://$PROXY_HOSTNAME:$PROXY_PORT/"
else
launchctl setenv http_proxy "http://$PROXY_USER:$PROXY_PASS@$PROXY_HOSTNAME:$PROXY_PORT/"
launchctl setenv https_proxy "https://$PROXY_USER:$PROXY_PASS@$PROXY_HOSTNAME:$PROXY_PORT/"
fi
launchctl setenv HTTP_PROXY "$http_proxy"
launchctl setenv HTTPS_PROXY "$http_proxy"
launchctl setenv no_proxy "localhost,127.0.0.0/8,172.30.0.0/24,194.4.237.58,frcp03vin0013.cpdev.local,frcp03vdv0073.cpdev.local,fo1-pp.app.vod.cdn,fo2-pp.app.vod.cdn,fo3-pp.app.vod.cdn,db-pp.app.vod.cdn,db1-pp.app.vod.cdn,db2-pp.app.vod.cdn,db3-pp.app.vod.cdn,db-rec-1.app.vod.cdn,cvs1.app.vod.cdn,gesc0spacewlk01.msdb.coprod.cdn,gesc0report01.msdb.coprod.cdn,192.168.236.199,192.168.236.19,192.168.136.68,lb-crapaud-vabf.test.cdn,172.30.70.10,ldap.canal-bis.com:636,quova-pp.app.vod.cdn,cas01.app-g3.vod.cdn,dvssoft.app-g3.vod.cdn,lb-prdrm-vabf.backservices.coprod.cdn,lb-mdrm-vabf.backservices.coprod.cdn,10.251.84.11,192.168.139.1,10.100.0.0/8,10.200.0.0/8,10.210.0.0/8,10.220.0.0/8,10.230.0.0/8,10.240.0.0/8,10.250.0.0/8,172.19.0.0/8,172.23.32.0/20,172.23.103.192/26,172.23.38.161,172.23.38.192,c-infra-ns03.mg.vabf.coprod.eu-west-1.aws,172.30.5.17,172.30.5.16,/var/run/docker.sock,mg.prod.eu-west-1.aws,mg.int.coprod.eu-west-1.aws,*eu-west-1.elb.amazonaws.com"
launchctl setenv NO_PROXY "$no_proxy"
launchctl setenv SBT_OPTS "-Dfile.encoding=UTF8 -Dsbt.override.build.repos=true -Dhttp.proxyHost=bluecoatproxy.cpgrp.root.local -Dhttp.proxyPort=8080 -Dhttp.nonProxyHosts=localhost|*.mg.prod.eu-west-1.aws|*.mg.int.coprod.eu-west-1.aws|127.*|[::1]|172.30.95.*|172.23.*|atgbo-n7|172.17.*|172.30.5.16|172.30.5.17|lb-prdrm-vabf.backservices.coprod.cdn|lb-mdrm-vabf.backservices.coprod.cdn|coprod-der-repo01"
;;
esac
echo "--> Location Changer: $LOCATION - $REASON"
osascript -e "display notification \"$LOCATION - $REASON\" with title \"Network Change\""
exit 0