Skip to content

Instantly share code, notes, and snippets.

@sasrai
Last active August 29, 2015 14:21
Show Gist options
  • Save sasrai/5fe1fa842960149a24ad to your computer and use it in GitHub Desktop.
Save sasrai/5fe1fa842960149a24ad to your computer and use it in GitHub Desktop.
VPSの固定ルートをグループ単位で自動的に追加するスクリプト(簡易型)
#!/bin/sh
#
# OSX 10.10.3(Yosemite)で動作確認取ってる固定ルート自動追加スクリプト。
# 各VPNに合わせて下のVPN設定を増やしてください。
# 条件分岐一つ一つが一つのVPN用設定になりますが、Softetherの場合など、
# 複数のVPNで同一条件になる物が出てくると思います。
# その場合は適当に改造して条件分岐とインターフェース指定を頑張ってください!
#
# add_routesの後ろの部分がグループ名になります。
#
# 追加するルートは /etc/ppp/ip-up.d/[グループ名].routes になります。
# サンプルは別のgistを参照してください。
#
# $1 interface-name
# $2 tty-device
# $3 speed
# $4 local-IP-address
# $5 remote-IP-address
# $6 ipparam
date > /tmp/ipup.log
echo '$0=', "$0" >> /tmp/ipup.log
echo '$1=', "$1" >> /tmp/ipup.log
echo '$2=', "$2" >> /tmp/ipup.log
echo '$3=', "$3" >> /tmp/ipup.log
echo '$4=', "$4" >> /tmp/ipup.log
echo '$5=', "$5" >> /tmp/ipup.log
echo '$6=', "$6" >> /tmp/ipup.log
ROUTE=/sbin/route
ROUTES_DIR=$(dirname $0)/ip-up.d
######################################################
# ルーティング追加
add_routes() {
local gateway=$1
local routes_conf=$2
local routes_file=${ROUTES_DIR}/${routes_conf}.routes
if [ -f ${routes_file} ]; then
echo "add routes group => $routes_conf" >> /tmp/ipup.log
else
echo "$routes_conf: No such routes file" >> /tmp/ipup.log
return
fi
for route in $(awk -v ORS='' '{sub(/#.*/,"",$0);sub(/ +/," ");if($0!=" "&&$0!=""){print;print " "}}' ${routes_file})
do
echo " add \`${route}\`" >> /tmp/ipup.log
eval $ROUTE add $route $gateway
done
}
######################################################
# vpn設定
if [ "${5:-}" = "10.0.0.2" ]
then
remote=$5
add_routes $remote sample1
add_routes $remote sample2
fi
################################################
#
# Sample configuration
# 一応#でコメント使えるようになってます。
#
################################################
# develop server
10.10.10.100
# live server
10.9.8.80
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment