LoginSignup
29
33

More than 5 years have passed since last update.

MacでVPN接続時に自動ルーティングする

Last updated at Posted at 2017-09-29

はじめに

この記事は OS X 10.11 までの環境を想定しています。
macOS Sierra (10.12) から、標準VPNクライアントにPPTPは搭載されていません。

サードパーティー製のPPTP接続ツールにも有効な手法ですが、Shimo のように独自のルーティング機構を備えたツールでは、そちらの機能を使う方がGUIからお手軽にルーティングを設定できます。

導入

Windowsほどではありませんが、Macでも標準VPNクライアントでのPPTPはルーティングの問題が悩ましいです。デフォルトゲートにすると全通信がVPNを通ってしまうのは同じなので、デフォルトゲートのチェックを外しつつ必要なルーティングを接続時に得るため /etc/ppp/ip-up を使います。

/etc/ppp/ip-upPPP接続が成功した時にシステムから実行される スクリプトです。VPN接続後に設定したいルーティング情報をここに記述することで、必要なルーティングを得ることができます。

作業の前に

/etc/ppp/ip-up の設定はGUIではできません。
エディタ等でも編集できるかもしれませんが、権限昇格の操作を伴うので以降の作業はすべてコンソール経由で行うことを推奨します。

iTerm 等のターミナルソフトを使用してください。

ip-up ファイルの生成

/etc/ppp/ip-up ファイルが存在しない場合は新規作成し、実行属性を付与します。

sudo touch /etc/ppp/ip-up
sudo chmod 755 /etc/ppp/ip-up

既に存在する場合は内容を確認の上、バックアップ後に新規作成または追記での設定など適宜対応してください。

ルーティング編集

以下の雛形をもとにルーティング情報を編集します。
「どのVPNに接続したか」の判断は remoteip 変数で行います。

remoteip = 接続先のデフォルトゲート

ですので気をつけてください(VPN接続先ではありません)。

デフォルトゲートのIPが不明な場合は Debug output の echo行のコメントを外した状態でVPN接続してみてください。ログ(DEBUGFILE)に接続情報が出力されます。

「5: ...」のIPが remoteip 相当です。

ルーティング自体は route コマンドによる追加となります。
詳細は route コマンドのマニュアルを参照してください。

#!/bin/sh
#
# Script which handles the routing issues as necessary for pppd,
# including for PPTP tunnels. Save this script as /etc/ppp/ip-up
# and make sure it is executable.
#
# When the ppp link comes up, this script is called with the following
# parameters
#   $1      the interface name used by pppd (e.g. ppp3)
#   $2      the tty device name
#   $3      the tty device speed
#   $4      the local IP address for the interface
#   $5      the remote IP address
#   $6      the parameter specified by the 'ipparam' option to pppd

#
# Config
#
ROUTE=/sbin/route
DEBUGFILE=/tmp/ip-up-debug.txt


#
# Debug output
#
##echo "1:$1 2:$2 3:$3 4:$4 5:$5 6:$6" >> $DEBUGFILE

#
# vars
#
ifname=$1
ttyname=$2
speed=$3
localip=$4
remoteip=$5
ipparam=$6  # the current IP address before connecting to the VPN

#
# routing
#
case "$remoteip" in
    # my office vpn
    192.168.100.1)
        $ROUTE add -net  192.168.200.0/24 -interface $ifname
        $ROUTE add -host 192.168.10.30 -interface $ifname
        ;;
esac

exit 0;

【参考情報】
Modify PPTP Routing Table
http://www.macfreek.nl/memory/Modify_PPTP_Routing_Table

実行

対象のVPN接続を実行します。
接続後に netstat -rn して指定のルーティングが追加されていることを確認します。

29
33
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
29
33