中国DOS联盟

-- 联合DOS 推动DOS 发展DOS --

联盟域名:www.cn-dos.net 论坛域名:www.cn-dos.net/forum
DOS,代表着自由开放与发展,我们努力起来,学习FreeDOS和Linux的自由开放与GNU精神,共同创造和发展美好的自由与GNU GPL世界吧!

中国DOS联盟论坛
现在时间是 2026-08-07 16:55
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [求助]动态网关绑定的批处理实现方法 查看 1,238 回复 12
楼 主 [求助]动态网关绑定的批处理实现方法 发表于 2008-02-29 17:31 ·  中国 吉林 长春 联通
初级用户
积分 20
发帖 9
注册 2008-02-29 14:48
18年会员
UID 111717
性别 男
状态 离线
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
2 发表于 2008-02-29 18:14 ·  中国 上海 联通
版主
★★★★★
积分 9,023
发帖 5,017
注册 2007-05-31 19:39
19年会员
UID 89899
性别 男
状态 离线
@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%% 绑定成功)
3 发表于 2008-02-29 18:54 ·  中国 吉林 长春 联通
初级用户
积分 20
发帖 9
注册 2008-02-29 14:48
18年会员
UID 111717
性别 男
状态 离线
这样做最后做出的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如何能够执行 以上程序执行的目的
4 发表于 2008-02-29 19:02 ·  中国 吉林 长春 联通
初级用户
积分 20
发帖 9
注册 2008-02-29 14:48
18年会员
UID 111717
性别 男
状态 离线
1所输出的123.BAT内容没得到MAC地址
最终要实现的命令
arp -s 192.168.1.1 00-19-e0-3d-ce-ce
才可以实现绑定
5 发表于 2008-02-29 19:14 ·  中国 上海 联通
版主
★★★★★
积分 9,023
发帖 5,017
注册 2007-05-31 19:39
19年会员
UID 89899
性别 男
状态 离线
我这里测试的时候是可以获取网关MAC和IP的,楼主是否介意把以下命令的结果贴出来看看?

ipconfig /all
6 发表于 2008-03-01 00:57 ·  中国 广东 广州 天河区 电信
金牌会员
★★★★
一叶枝头,万树皆春
积分 2,564
发帖 1,127
注册 2006-12-25 22:57
19年会员
UID 74552
性别 男
状态 离线
ping -i 1 -n 1 1.1.1.1
49206C6F766520796F752067757973 54656C3A3133383238343036373837
7 发表于 2008-03-01 09:38 ·  中国 辽宁 营口 联通
新手上路
积分 18
发帖 6
注册 2006-03-29 21:50
20年会员
UID 53015
状态 离线
@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
8 发表于 2008-03-01 09:39 ·  中国 辽宁 营口 联通
新手上路
积分 18
发帖 6
注册 2006-03-29 21:50
20年会员
UID 53015
状态 离线
上面是自己写的,一直在用。
9 发表于 2008-03-01 11:48 ·  中国 吉林 长春 联通
初级用户
积分 20
发帖 9
注册 2008-02-29 14:48
18年会员
UID 111717
性别 男
状态 离线
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会有点问题存在无线网卡如何解决
10 发表于 2008-03-01 11:57 ·  中国 重庆 电信
版主
★★★★★
积分 9,023
发帖 5,017
注册 2007-05-31 19:39
19年会员
UID 89899
性别 男
状态 离线
我的电脑也是双网卡,不过我平时不用无线而已,我在for循环里面用goto跳转就是为了避免双网卡的情况,楼主可以根据自己的实际情况自行修改代码,就当学习批处理吧。
11 发表于 2008-03-01 12:01 ·  中国 吉林 长春 联通
初级用户
积分 20
发帖 9
注册 2008-02-29 14:48
18年会员
UID 111717
性别 男
状态 离线
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
12 发表于 2008-03-01 12:23 ·  中国 吉林 长春 联通
初级用户
积分 20
发帖 9
注册 2008-02-29 14:48
18年会员
UID 111717
性别 男
状态 离线
HAT
我也是新手呵呵,能不能介绍点比较有用的P处理常用的技巧
1如从以个输出22.txt中搜找需求的参数的
命令格式

2就是我定义的 这2个变量
如果运行
arp -s "变量1" "变量2"
我是存为脚本然后绑定启动
13 发表于 2008-03-04 00:24 ·  中国 河南 郑州 电信
新手上路
积分 12
发帖 6
注册 2006-11-18 23:29
19年会员
UID 70994
性别 男
状态 离线
look look
论坛跳转: