Board logo

标题: [解决]如何用批处理获取不同网络连接的网关 [打印本页]

作者: feifeiyucn     时间: 2008-4-12 00:12    标题: [解决]如何用批处理获取不同网络连接的网关



  Quote:
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命令我就搞不掂了

先行感谢大家的帮忙

[ Last edited by feifeiyucn on 2008-4-12 at 04:54 PM ]
作者: ThinKing     时间: 2008-4-12 13:21    标题: 不知道你有没有Perl环境


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";

作者: feifeiyucn     时间: 2008-4-12 13:53
回复楼上的:没有。。。我只希望用批处理解决这个问题

手滑了,不小心将这里内容删了,简要重新写一下吧

将网关都写进gw.txt里
暂时ADSL拨号连接都是118开头,铁通到电信加速都是172.28开头,铁通到网通加速都是172.29开头
如何分别读取出来赋值给%ctt%,%tel%,%cnc%?

[ Last edited by feifeiyucn on 2008-4-12 at 02:32 PM ]
作者: bat-zw     时间: 2008-4-12 14:09

@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!
)

作者: abcd     时间: 2008-4-12 14:25


  Quote:
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! ...

真聪明。。。。
作者: feifeiyucn     时间: 2008-4-12 14:40


  Quote:
Originally posted by zw19750516 at 2008-4-12 14:09:
@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! ...

有点问题,gw.txt每行前面都有个空格,改成这样就行了(暂时用了echo来代替set)
@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
[ Last edited by feifeiyucn on 2008-4-12 at 02:56 PM ]