Board logo

标题: [求助]动态网关绑定的批处理实现方法 [打印本页]

作者: configwater     时间: 2008-2-29 17:31    标题: [求助]动态网关绑定的批处理实现方法

1,ipconfig 或者 ipconfig /all 提取网关信息
2,ping 网关IP 后ARP缓存里保存GW的MAC地址
3,arp -a  后提取MAC地址

将ARP -S >123.bat
将网关IP>>123.bat
将MAC>>123.bat

4运行 123.bat
最好能实现信息输出跳出个窗口绑定成功,
"变量"网关信息是变的,还一个就是MAC地址的信息


123.bat内容
=====================================
arp -s 192.168.1.2 00-19-e0-3d-ce-ce
作者: HAT     时间: 2008-2-29 18:14
CODE:  [Copy to clipboard]
@echo off

:gateip
for /f "tokens=13" %%a in ('ipconfig /all ^| findstr /i "Default Gateway"') do (
  set gwip=%%a
  goto :gatemac
)

:gatemac
>nul ping %gwip%
for /f "tokens=2" %%a in ('arp -a ^| findstr /i "%gwip%"') do (
  set gwmac=%%a
  goto :output
)

:output

>"123.bat" echo (arp -s %gwip% %gwmac%)^&^&(msg %%username%% 绑定成功)

作者: configwater     时间: 2008-2-29 18:54
这样做最后做出的123.bat内容是
(arp -s 192.168.1.1 192.168.1.101)&&(msg %username% 绑定成功)

而不正常得到的答案应该是
arp -s 192.168.1.1 00-19-e0-3d-ce-ce

我执行123.bat如何能够执行 以上程序执行的目的
作者: configwater     时间: 2008-2-29 19:02
1所输出的123.BAT内容没得到MAC地址
最终要实现的命令
arp -s 192.168.1.1 00-19-e0-3d-ce-ce
才可以实现绑定
作者: HAT     时间: 2008-2-29 19:14
我这里测试的时候是可以获取网关MAC和IP的,楼主是否介意把以下命令的结果贴出来看看?

ipconfig /all
作者: everest79     时间: 2008-3-1 00:57
ping -i 1 -n 1 1.1.1.1
作者: zhqw     时间: 2008-3-1 09:38
@Echo off && mode con cols=80 lines=25
set times=1
set c=2f
set ip=null
set GateIP=null
color %c%
arp -d > nul
cls
echo.
echo.
echo    正在绑定本机、网关的IP与MAC地址,请稍候......完成后本窗口自动关闭。
:TryAgain
For /f "delims=: tokens=2" %%i in ('ipconfig /all^|find /i "IP Address"') do set IP=%%i
For /f "delims=: tokens=2" %%m in ('ipconfig /all^|find /i "Physical Address"') do set MAC=%%m
For /f "delims=: tokens=2" %%g in ('ipconfig /all^|find /i "Default Gateway"') do Set GateIP=%%g
For /f "delims=: tokens=2" %%n in ('ipconfig /all^|find /i "DNS Servers"') do Set DNS=%%n
if %IP%==null goto errIP
if %GateIP%==null goto errIP

ping %GateIP% -n 1
For /f "tokens=2" %%n in ('arp -a^|find "%GateIP%"') do Set GateMAC=%%n
if Not Errorlevel==1 goto SetMAC

cls
echo.
echo.
set KEY=Exit
echo  ========================   第 %times% 次连接网关失败! ========================
echo  没有找到网关%GateIP%的MAC地址,重试请按【R】键,退出请按【Enter】键
if %c%==9f (set c=2f) else set c=9f
color %c%
set /a times=%times%+1
echo.
set /p KEY=   请选择按键:
if /i %KEY% == R goto TryAgain
Exit

:SetMAC
arp -s %IP% %MAC%
arp -s %GateIP% %GateMAC%
set TTL=null
For /f "tokens=6" %%t in ('ping -n 1 %DNS%^|find /i "Reply from"') do Set TTL=%%t
if %TTL%==null  goto errDNS
exit

:errDNS
cls
color ce
echo.
echo.
echo.
echo    DNS服务器为:%DNS%
echo.
echo    访问域名服务器错误,局域网可访问,互联网不可访问,按任意键关闭程序窗口。
PAUSE >NUL && Exit

:errIP
cls
color ce
echo.
echo.
echo.
echo    IP地址/网关地址设置错误,请修正后再试......,按任意键关闭程序窗口。
PAUSE >NUL && Exit
作者: zhqw     时间: 2008-3-1 09:39
上面是自己写的,一直在用。
作者: configwater     时间: 2008-3-1 11:48
Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.

C:\Documents and Settings\Administrator>ipconfig -all

Windows IP Configuration

        Host Name . . . . . . . . . . . . : sophya
        Primary Dns Suffix  . . . . . . . :
        Node Type . . . . . . . . . . . . : Unknown
        IP Routing Enabled. . . . . . . . : No
        WINS Proxy Enabled. . . . . . . . : No

Ethernet adapter 本地连接:

        Media State . . . . . . . . . . . : Media disconnected
        Description . . . . . . . . . . . : Broadcom 440x 10/100 Integrated C
roller
        Physical Address. . . . . . . . . : 00-19-B9-68-56-8B

Ethernet adapter 无线网络连接:

        Connection-specific DNS Suffix  . :
        Description . . . . . . . . . . . : Dell 无线 1390 WLAN Mini-Card
        Physical Address. . . . . . . . . : 00-19-7E-19-49-0A
        Dhcp Enabled. . . . . . . . . . . : Yes
        Autoconfiguration Enabled . . . . : Yes
        IP Address. . . . . . . . . . . . : 192.168.0.101
        Subnet Mask . . . . . . . . . . . : 255.255.255.0
        Default Gateway . . . . . . . . . : 192.168.0.1
        DHCP Server . . . . . . . . . . . : 192.168.0.1
        DNS Servers . . . . . . . . . . . : 202.98.0.68
                                            192.168.0.1
        Lease Obtained. . . . . . . . . . : 2008年3月1日 8:10:46
        Lease Expires . . . . . . . . . . : 2008年3月8日 8:10:46

C:\Documents and Settings\Administrator>
我觉得用ipconfig或者用ipconfig/all会有点问题存在无线网卡如何解决
作者: HAT     时间: 2008-3-1 11:57
我的电脑也是双网卡,不过我平时不用无线而已,我在for循环里面用goto跳转就是为了避免双网卡的情况,楼主可以根据自己的实际情况自行修改代码,就当学习批处理吧。
作者: configwater     时间: 2008-3-1 12:01
Default Gateway . . . . . . . . . : 192.168.0.1
我们必须先把ipconfig/all的输出做过滤找到以上着行信息,提取IP部分
以这个 IP为第2个 ping 后面的IP地址(目的为ARP缓存取得网管mac地址)
===========================================
C:\Documents and Settings\Administrator>arp -a

Interface: 192.168.0.101 --- 0x3
  Internet Address      Physical Address      Type
  192.168.0.1           00-15-e9-e2-f2-1b     dynamic

C:\Documents and Settings\Administrator>
从上面提取网关MAC

arp -s "第一变量" "第二变量"
如:
arp -s 192.168.0.1           00-15-e9-e2-f2-1b
作者: configwater     时间: 2008-3-1 12:23
HAT
我也是新手呵呵,能不能介绍点比较有用的P处理常用的技巧
1如从以个输出22.txt中搜找需求的参数的
命令格式

2就是我定义的 这2个变量
如果运行
arp -s "变量1"   "变量2"   
我是存为脚本然后绑定启动
作者: cificap     时间: 2008-3-4 00:24
look look




欢迎光临 中国DOS联盟论坛 (http://cndos.fam.cx/forum/) Powered by Discuz! 2.5