それマグで!

知識はカップより、マグでゆっくり頂きます。 takuya_1stのブログ

習慣に早くから配慮した者は、 おそらく人生の実りも大きい。

Mac でVPNを接続するコマンド。ついでに整理しておく

macVPNを接続する。

Macに標準のサービスを使って設定しているPPP系 LT2P over TLS のような設定だとnetworksetup から出来る

scutil もいいけど、networksetup でもいい。私はこっち。

macVPNに接続するには

networksetup -connectpppoeservice "$VPN_NAME"

切断するには、

networksetup -disconnectpppoeservice "$VPN_NAME"

接続状態を見るには。

networksetup -showpppoestatus "$VPN_NAME"

$VPN_NAME :サービス名を取得するには

覚えられないので、bashrcにした

networksetup はコマンドが多過ぎて、覚えられないで、bashrcに書くことにした。

#!/usr/bin/env bash



if ! [[ $plat_name =~ darwin ]] ; then
    return;
fi


function vpn.getFullName(){
  VPN_NAME=$(networksetup -listallnetworkservices  | \grep $1  );
  echo $VPN_NAME;
}

function vpn.isConnected(){
  VPN_NAME=$( vpn.getFullName $1 )
  [[ $(networksetup -showpppoestatus "$VPN_NAME" )  == connected ]];
}
function vpn.disconnect {
    vpn.menu_icon.conceal
    VPN_NAME=$( vpn.getFullName $1 )
    networksetup -disconnectpppoeservice "$VPN_NAME"
}
function vpn.menu_icon.conceal() {
    if type vpn_menu.py  2>&1 > /dev/null ;then
        vpn_menu.py -d
    fi
}
function vpn.menu_icon.display() {
    if type vpn_menu.py  2>&1 > /dev/null ;then
        vpn_menu.py -e
    fi
}

function vpn.connect(){
    vpn.menu_icon.display
    VPN_NAME=$( vpn.getFullName $1 )
    vpn.disconnect "$VPN_NAME"
    vpn.isConnected  "$VPN_NAME" || networksetup -connectpppoeservice "$VPN_NAME"
    cnt=0
    echo "conncting...";
    while ! vpn.isConnected "$VPN_NAME"
    do
    (( cnt++ ))
    printf "${cnt} time  .waiting...\r"
    sleep 1
    if (( $cnt > 30 )) ; then
      echo failed.
      return 1 ;
    fi
    done;
    printf "                                  \r"
    echo "connected"
    return 0;
}

function vpn.addRoute(){
  echo add route;
  for IP_ADDR; do
    echo ip route add ${IP_ADDR}/32 dev ppp0
    ip route add ${IP_ADDR}/32 dev ppp0
  done

}

関連資料