C:\>ipconfig Windows IP Configuration Ethernet adapter 本地连接: Connection-specific DNS Suffix . : IP Address. . . . . . . . . . . . : 192.168.1.100 Subnet Mask . . . . . . . . . . . : 255.255.255.0 Default Gateway . . . . . . . . . : 192.168.1.101 Ethernet adapter {5982BEEE-FC3F-43B1-9262-F2C4BF4AD972}: Connection-specific DNS Suffix . : IP Address. . . . . . . . . . . . : 10.255.255.253 Subnet Mask . . . . . . . . . . . : 255.255.255.224 Autoconfiguration IP Address. . . : 169.254.19.67 Subnet Mask . . . . . . . . . . . : 255.255.0.0 Default Gateway . . . . . . . . . : PPP adapter ADSL: Connection-specific DNS Suffix . : IP Address. . . . . . . . . . . . : 118.147.61.160 Subnet Mask . . . . . . . . . . . : 255.255.255.255 Default Gateway . . . . . . . . . : 118.147.61.160 PPP adapter 铁通去网通加速: Connection-specific DNS Suffix . : IP Address. . . . . . . . . . . . : 172.29.23.169 Subnet Mask . . . . . . . . . . . : 255.255.255.255 Default Gateway . . . . . . . . . : 172.29.23.169 PPP adapter 铁通去电信加速: Connection-specific DNS Suffix . : IP Address. . . . . . . . . . . . : 172.28.7.174 Subnet Mask . . . . . . . . . . . : 255.255.255.255 Default Gateway . . . . . . . . . : 172.28.7.174要求是将上面“ADSL”这个拨号连接的网关、“铁通去网通加速”和“铁通去电信加速”这两个VPN连接的网关,分别赋值给批处理中的%ctt%、%cnc%、%tel%这3个变量 只需要帮我做完上面的操作即可,我的最终目的是用route命令来改路由表了,写批处理避免重复劳动,但IP都是动态的,用到for命令我就搞不掂了
use strict;
use warnings;
my $infile = shift || "ipconfig.txt" || die "Can't open the ipconfig.txt file : $!\n";
open OK,$infile;
undef $/;
my $text=<OK>;
my($ctt,$cnc,$tel);
if ($text =~ /PPP adapter ADSL:.*?Default Gateway.+?:\s*([\d.]+)/s) {
$ctt=$1;
}
if ( $text =~ /PPP adapter 铁通去网通加速:.*?Default Gateway.+?:\s*([0-9\.]+)/s) {
$cnc=$1;
}
if ( $text =~ /PPP adapter 铁通去电信加速:.*?Default Gateway.+?([\d.]+)$/s) {
$tel=$1;
}
close $infile;
print "\$ctt is $ctt\n\$cnc is $cnc\n\$tel is $tel\n";
@echo off&setlocal enabledelayedexpansion
for /f "delims=" %%i in (gw.txt) do (
set a=%%i
if "!a:~,3!"=="118" set ctt=!a!
if "!a:~,6!"=="172.28" set tel=!a!
if "!a:~,6!"=="172.29" set cnc=!a!
)
Originally posted by zw19750516 at 2008-4-12 02:09 PM: [code]@echo off&setlocal enabledelayedexpansion for /f "delims=" %%i in (gw.txt) do ( set a=%%i if "!a:~,3!"=="118" set ctt=!a! if "!a:~,6! ...真聪明。。。。
Originally posted by zw19750516 at 2008-4-12 14:09:有点问题,gw.txt每行前面都有个空格,改成这样就行了(暂时用了echo来代替set)@echo off&setlocal enabledelayedexpansion for /f "delims=" %%i in (gw.txt) do ( set a=%%i if "!a:~,3!"=="118" set ctt=!a! if "!a:~,6! ...
@echo off&setlocal enabledelayedexpansion
if exist gw.txt del gw.txt
for /f "tokens=1* delims=:" %%i in ('ipconfig^|find /i "Default Gateway"') do @echo %%j>>gw.txt
for /f "delims=" %%i in (gw.txt) do (
set a=%%i
if "!a:~1,3!"=="118" echo ctt=!a:~1!
if "!a:~1,6!"=="172.28" echo tel=!a:~1!
if "!a:~1,6!"=="172.29" echo cnc=!a:~1!
)
pause