Created
May 23, 2014 21:44
-
-
Save vibbow/d11f92806f7ea9fd39d6 to your computer and use it in GitHub Desktop.
Windows 路由表添加脚本生成工具
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Windows 路由添加脚本生成工具 | |
* 由路由列表文件,生成Windows适用的路由添加脚本 | |
* vibbow @ 2014/5/23 | |
* | |
* 参考路由列表文件: | |
* http://www.dnspod.cn/misc/iplist.txt | |
* 可从中截取出要生成的运营商路由表 | |
*/ | |
// 输入的路由列表文件路径 | |
// 支持的输入路由列表文件格式:IPADDR/MASK | |
// 举例:202.100.16.0/20 | |
define('INPUT_ROUTE_LIST', 'route.txt'); | |
// 输出的路由脚本文件路径 | |
// Windows BAT文件,可直接运行 | |
define('OUTPUT_ROUTE_BAT', 'add_route.bat'); | |
// 网关IP地址 | |
// 填写服务器的网关IP地址,而不是服务器IP地址。 | |
define('GATEWAY', '1.2.3.4'); | |
// 跃点数 | |
// 跃点数越低的路由表优先级越高 | |
// 默认为 1 即可 | |
define('METRIC', '1'); | |
// 永久路由 | |
// 如果网络信息不会变动,则设置为 TRUE | |
// 否则设置为 FALSE | |
define('PERMANENT_ROUTE', FALSE); | |
// 以下内容无需变动 | |
$route_list = file(INPUT_ROUTE_LIST); | |
$route_template = "route add %s mask %s %s metric %s"; | |
$route_template .= PERMANENT_ROUTE ? " -p\r\n" : "\r\n"; | |
$route_bat = ''; | |
foreach ($route_list as $each_route) { | |
$matches = NULL; | |
$match_count = preg_match('/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\/(\d{1,2})/', $each_route, $matches); | |
if ($match_count === 0) { | |
continue; | |
} | |
$ip = $matches[1]; | |
$mask = $matches[2]; | |
$mask_binary = str_repeat('1', $mask) . str_repeat('0', 32 - $mask); | |
$mask_ip_a = bindec(substr($mask_binary, 0, 8)); | |
$mask_ip_b = bindec(substr($mask_binary, 8, 8)); | |
$mask_ip_c = bindec(substr($mask_binary, 16, 8)); | |
$mask_ip_d = bindec(substr($mask_binary, 24, 8)); | |
$mask_ip = "{$mask_ip_a}.{$mask_ip_b}.{$mask_ip_c}.{$mask_ip_d}"; | |
$route_bat .= sprintf($route_template, $ip, $mask_ip, GATEWAY, 1); | |
} | |
file_put_contents(OUTPUT_ROUTE_BAT, $route_bat); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment