4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Macの複数無線APのネットワーク環境自動切り替え

Last updated at Posted at 2018-09-06

1.はじめに

MacOSXを使っていて、iOSのように、AP(AccessPoint)ごとにProxy設定を変更出来ないのかとさがしてたどり着いたのが、下記先生の記事でした。

先生記事:
Mac OS Xの標準機能のみで無線LANアクセスポイント毎にプロキシ設定を自動切り替えする方法

しかし、いくつかのAP名で切り替える必要が出てきたため、スクリプトを改造しました。

2.仕様

AP名とネットワーク環境名のリストを用意して、リストのAP名にマッチしたらそのネットワーク環境名に変更します。

要するに

  • 外部ファイルを読み込んで、AP名が合致するまで回す
  • 合致したらネットワーク環境を変更
  • 一件も合致しなかったらデフォルトのネットワーク環境を利用する
    という仕様に修正します

3.改造

3-1.LaunchAgentの設定

この辺りは先生の記事そのまま

3-2.シェルスクリプトの修正

以下のようにします。(下記コード中、自分のアカウント部分はご自身の環境に合わせて適宜変更ください)

NW_Prof_switch.sh
# !/bin/bash

# reference: https://qiita.com/kouichirou/items/13295fec71085e1e431c
# set AccessPoint uniqe name for switch profile.
prefdata=/Users/自分のアカウント/NWlist.txt
lastapfile=/tmp/last_airport.flg
# init
apname=""
networkprofile=""
defaultprofile=""
[ -e "$lastapfile" ] || touch "$lastapfile"
lastap=`cat $lastapfile`
device=`networksetup -listallhardwareports | grep -A 3 Wi-Fi | grep Device | awk '{print $2}'`
if [ -z "$device" ]; then
    #exit if no Wi-Fi device
    exit 0
fi
curap=`networksetup -getairportnetwork $device | grep Current | awk '{print substr($0, index($0, ":")+2)}'`

if [ "$curap" = "$lastap" -o -z "$curap" ]; then
    #exit if ap not changed or none ap
    exit 0
else
    echo $curap > $lastapfile
fi

for line in `cat ${prefdata} | grep -v "^#"`
do
    apname=`echo ${line} | cut -d ',' -f 1`
    networkprofile=`echo ${line} | cut -d ',' -f 2`
    if [ "*" = "$apname" ]; then
        defaultprofile=${networkprofile}
    fi
    if [ "$curap" = "$apname" ]; then
        scselect | grep "*" | grep $networkprofile > /dev/null
        if [ $? = 1 ]; then
            scselect $networkprofile  > /dev/null
            #osascript -e 'display notification "Network Changed to profile '$networkprofile'" with title "Network Config"'
        fi
        exit 0
    fi
done
if [ -n "$defaultprofile" ]; then
    scselect | grep "*" | grep $defaultprofile > /dev/null
    if [ $? = 1 ]; then
        scselect $defaultprofile  > /dev/null
        #osascript -e 'display notification "Network Changed to profile '$defaultprofile'" with title "Network Config"'
    fi
fi

3-3.AP接続リストの準備

自分のユーザーアカウントの場所に/Users/自分のアカウント/NWlist.txtを作ります。(直下はちょっと、、、という方は、場所を好きなように変更してみてください。)
CSVでデータを作成します。
AP名に*を指定するとデフォルトのネットワーク設定を指定できます。APが一覧にない場合に使われます(下記の例ではNOProxy)
他は、AP名と、それに対応するネットワーク環境で設定した名前を記入します。
下記サンプルでは、APがHomeはProxy1を、CompanyではProxy2を、SOHOではProxy3の設定に変更するようになります。
また、切り替え時同じネットワーク設定名になる場合は、改めてネットワーク環境をスイッチしないようにもしています。

NWlist.txt(sample)
# apname,network profile name
# apname "*" is default
*,NOProxy
Home,Proxy1
Company,Proxy2
SOHO,Proxy3

これでどうでしょうか

【9/7追記】
sleep復帰の時に再度ネットワーク環境の選択が実行されてしまい、接続→切断→接続みたいな動きになってしまっているようです。
MacBook Airですと、en0がWi-Fiなのですが、ほかの機種では保証されないとのことでしたので、en0かen1かといった情報を動的に取得するように変更しました。
また、Wi-Fiがoffの時に動作しないようにしました。これで接続→切断→接続という動きはしなくなります。

4.動作環境

MacOSX 10.13.6 High Sierra/10.14.1 Mojave
MacBook Air (13-inch,2017)

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?