Board logo

标题: 多网卡下如何实现读出板载网卡MAC [打印本页]

作者: yinjie1755     时间: 2008-5-22 11:23    标题: 多网卡下如何实现读出板载网卡MAC
多网卡(一个板载网卡,多个外接网卡)下如何实现读出板载网卡MAC(并且每个网卡有设IP号,网卡蕊片不一样!板载网卡上网!其他网卡不接网线)(WINDOWS98/2000/XP下测试用)

我写了段代码,但同时读出来三个.你看一下能不能实理那个效果!
@echo off
setlocal Enabledelayedexpansion
ipconfig /all >ipconfig.txt
find "Physical Address" ipconfig.txt >phyaddr.txt
for /f "skip=1 tokens=12" %%a in (phyaddr.txt) do (
set var=%%a
if not exist \\192.168.94.27\t\testdate\winpass.log goto fail
find /i /c "!var!" \\192.168.94.27\t\testdate\winpass.log
if not errorlevel 1 goto mac
echo !var!>>\\192.168.94.27\t\testdate\winpass.log
)
endlocal
end
:mac
cls
echo. The mas is used!
echo. The mas is used!
echo. The mas is used!
echo. The mas is used!
echo. The mas is used!
pause

作者: bat-zw     时间: 2008-5-22 11:35    标题: 请测试后反馈:
如主板网卡默认是第一网卡:

@echo off
for /f "tokens=2 delims=:" %%i in ('ipconfig /all^|findstr /c:"Physical Address"') do set mac=%%i&goto echo
:echo
set mac=%mac:~1%
echo %mac%
pause>nul


Last edited by zw19750516 on 2008-5-22 at 12:36 PM ]

作者: yinjie1755     时间: 2008-5-22 11:39    标题: 谢谢
谢谢!!我们这里是做测试的,其实我的最终功能是要判断服务器上的日志文件里面有没有被测试板的板载MAC数据来判断出它是不是被测试过的主板.呵呵!有劳了,谢谢!

作者: bat-zw     时间: 2008-5-22 12:55
如板载网卡不是默认第一网卡就麻烦点:

@echo off&setlocal enabledelayedexpansion
for /f "delims=" %%i in ('ipconfig /all') do (
set /a n+=1
set _!n!=%%i
echo %%i|findstr /c:"IP Address">nul&&set m=!n!
)
set /a m-=2
set str=!_%m%!
set mac=%str:~-18%
echo %mac%
pause>nul

----------------------------------------------------------------------------------------------------------
&&我还是解释下这段代码吧:
首先是将ipconfig /all所有的行都赋于数值变量,即用for读取一行就将n值加
1(初始n值为0),然后把这一行所有的字符赋值给_!n!;
然后再在所有的数值变量也就是ipconfig /all所有行中逐行查找IP Address字
符串,如找到则将这个行号赋值给m;
在ipconfig /all中可以看到有mac地址的行处于有IP Address字符行上面二行,
所以把m值减2;
最后截取_%m%(m已减2)后面的18个字符即为板载网卡mac。

----------------------------------------------------------------------------------------------------------
还是用if代替findstr来提高效率:

@echo off&setlocal enabledelayedexpansion
for /f "delims=" %%i in ('ipconfig /all') do (
set str=%%i&set /a n+=1
set _!n!=%%i
if "!str:~8,10!"=="IP Address" set m=!n!
)
set /a m-=2
set str=!_%m%!
set mac=%str:~-18%
echo %mac%
pause>nul


Last edited by zw19750516 on 2008-5-22 at 11:20 PM ]

作者: yinjie1755     时间: 2008-5-22 13:42    标题: 我调不出效果呀!
我调不出效果呀! "||"这个判断在WIN2000下好像用不了!请回复!谢谢!

作者: HAT     时间: 2008-5-22 14:02
把4楼的代码中echo off删除,再运行一边,把运行结果贴出来看看。

作者: yinjie1755     时间: 2008-5-22 14:11    标题: 这是显示结果!
E:\>set /a m-=2

E:\>set str=!_36!

E:\>set mac=. . . . . . : Yes

E:\>echo . . . . . . : Yes 1>>mac.log

E:\>pause1>nul

作者: bat-zw     时间: 2008-5-22 15:11
晕,请贴出你的ipconfig /all,我这里没有WIN2000

Last edited by zw19750516 on 2008-5-22 at 03:13 PM ]

作者: yinjie1755     时间: 2008-5-22 15:16    标题: 这个是我的机子上的,测试和这差不多,就是一个网卡有IP!
Windows IP Configuration



Host Name . . . . . . . . . . . . : gc0427

Primary Dns Suffix . . . . . . . : evoc.local

Node Type . . . . . . . . . . . . : Hybrid

IP Routing Enabled. . . . . . . . : No

WINS Proxy Enabled. . . . . . . . : No

DNS Suffix Search List. . . . . . : evoc.local



Ethernet adapter VMware Network Adapter VMnet8:



Connection-specific DNS Suffix . :

Description . . . . . . . . . . . : VMware Virtual Ethernet Adapter for VMnet8

Physical Address. . . . . . . . . : 00-50-56-C0-00-08

Dhcp Enabled. . . . . . . . . . . : No

IP Address. . . . . . . . . . . . : 192.168.230.1

Subnet Mask . . . . . . . . . . . : 255.255.255.0

Default Gateway . . . . . . . . . :



Ethernet adapter VMware Network Adapter VMnet1:



Connection-specific DNS Suffix . :

Description . . . . . . . . . . . : VMware Virtual Ethernet Adapter for VMnet1

Physical Address. . . . . . . . . : 00-50-56-C0-00-01

Dhcp Enabled. . . . . . . . . . . : No

IP Address. . . . . . . . . . . . : 192.168.229.1

Subnet Mask . . . . . . . . . . . : 255.255.255.0

Default Gateway . . . . . . . . . :



Ethernet adapter 本地连接 7:



Connection-specific DNS Suffix . :

Description . . . . . . . . . . . : Marvell Yukon 88E8040 PCI-E Fast Ethernet Controller

Physical Address. . . . . . . . . : 00-E0-B0-F2-2F-0C

Dhcp Enabled. . . . . . . . . . . : Yes

Autoconfiguration Enabled . . . . : Yes

IP Address. . . . . . . . . . . . : 192.168.94.50

Subnet Mask . . . . . . . . . . . : 255.255.255.0

Default Gateway . . . . . . . . . : 192.168.94.1

DHCP Server . . . . . . . . . . . : 172.16.0.198

DNS Servers . . . . . . . . . . . : 172.16.0.198

202.96.174.66

Primary WINS Server . . . . . . . : 172.16.0.198

Lease Obtained. . . . . . . . . . : 2008年5月20日 16:29:43

Lease Expires . . . . . . . . . . : 2008年5月28日 16:29:43

作者: yinjie1755     时间: 2008-5-22 15:18    标题: 如下是本机板载网卡MAC
Connection-specific DNS Suffix . :

Description . . . . . . . . . . . : Marvell Yukon 88E8040 PCI-E Fast Ethernet Controller

Physical Address. . . . . . . . . : 00-E0-B0-F2-2F-0C

作者: yinjie1755     时间: 2008-5-22 15:27    标题: zw19750516注意一下,
我刚刚到测试机器上看了一下,那里除了板载有IP外,其它的都不会显IP.

作者: bat-zw     时间: 2008-5-22 15:28
172.16.0.198这个值是固定的不
把测试的ipconfig /all 发出来,我这里的确实和你那里的有很大的不同。

Last edited by zw19750516 on 2008-5-22 at 03:30 PM ]

作者: yinjie1755     时间: 2008-5-22 15:29    标题: 如下
大至是这样:
Windows IP Configuration



Host Name . . . . . . . . . . . . : gc0427

Primary Dns Suffix . . . . . . . : evoc.local

Node Type . . . . . . . . . . . . : Hybrid

IP Routing Enabled. . . . . . . . : No

WINS Proxy Enabled. . . . . . . . : No

DNS Suffix Search List. . . . . . : evoc.local



Ethernet adapter VMware Network Adapter VMnet8:



Connection-specific DNS Suffix . :

Description . . . . . . . . . . . : VMware Virtual Ethernet Adapter for VMnet8

Physical Address. . . . . . . . . : 00-50-56-C0-00-08

Dhcp Enabled. . . . . . . . . . . : No

IP Address. . . . . . . . . . . . :

Subnet Mask . . . . . . . . . . . :

Default Gateway . . . . . . . . . :



Ethernet adapter VMware Network Adapter VMnet1:



Connection-specific DNS Suffix . :

Description . . . . . . . . . . . : VMware Virtual Ethernet Adapter for VMnet1

Physical Address. . . . . . . . . : 00-50-56-C0-00-01

Dhcp Enabled. . . . . . . . . . . : No

IP Address. . . . . . . . . . . . :

Subnet Mask . . . . . . . . . . . :

Default Gateway . . . . . . . . . :



Ethernet adapter 本地连接 7:



Connection-specific DNS Suffix . :

Description . . . . . . . . . . . : Marvell Yukon 88E8040 PCI-E Fast Ethernet Controller

Physical Address. . . . . . . . . : 00-E0-B0-F2-2F-0C

Dhcp Enabled. . . . . . . . . . . : Yes

Autoconfiguration Enabled . . . . : Yes

IP Address. . . . . . . . . . . . : 192.168.94.50

Subnet Mask . . . . . . . . . . . : 255.255.255.0

Default Gateway . . . . . . . . . : 192.168.94.1

DHCP Server . . . . . . . . . . . : 172.16.0.198

DNS Servers . . . . . . . . . . . : 172.16.0.198

202.96.174.66

Primary WINS Server . . . . . . . : 172.16.0.198

Lease Obtained. . . . . . . . . . : 2008年5月20日 16:29:43

Lease Expires . . . . . . . . . . : 2008年5月28日 16:29:43

作者: bat-zw     时间: 2008-5-22 15:32
很好解决,把我4楼的set /a m-=2改为set /a m-=3

作者: yinjie1755     时间: 2008-5-22 15:41    标题: 测试机器那里没有DHCP和DNS
测试机器那里没有DHCP和DNS. SORRY!我是在我的办公电脑上导出的! 大至如下

Ethernet adapter 本地连接 7:



Connection-specific DNS Suffix . :

Description . . . . . . . . . . . : Marvell Yukon 88E8040 PCI-E Fast Ethernet Controller

Physical Address. . . . . . . . . : 00-E0-B0-F2-2F-0C

IP Address. . . . . . . . . . . . : 192.168.94.50

Subnet Mask . . . . . . . . . . . : 255.255.255.0

作者: yinjie1755     时间: 2008-5-22 15:43    标题: OK了谢谢,谢谢谢谢!!
但我不太明白,我再看一下!!

作者: bat-zw     时间: 2008-5-22 15:44
如192.168.94.1这个值是固定的:

@echo off&setlocal enabledelayedexpansion
for /f "delims=" %%i in ('ipconfig /all') do (
set /a n+=1
set _!n!=%%i
)
for /l %%i in (1,1,%n%) do echo !_%%i!|findstr /c:"192.168.94.1">nul&&set m=%%i
set /a m-=5
set str=!_%m%!
set mac=%str:~-18%
echo %mac%
pause>nul

作者: yinjie1755     时间: 2008-5-22 15:58    标题: 2000下也测试OK太谢谢你了zw19750516
不知98会不会有问题!!!

作者: bat-zw     时间: 2008-5-22 16:03
可是我一直很担心,因为我到现在还不知道,你测试环境下的ipconfig /all是怎么样的。

作者: yinjie1755     时间: 2008-5-22 16:05
你等一下,我到生产部去一下,去COPY一个来!!!

作者: yinjie1755     时间: 2008-5-22 16:15    标题: 这个是生产部上一片测试主板的IPCONFIG
Windows 2000 IP Configuration



Host Name . . . . . . . . . . . . : ok25
Primary DNS Suffix . . . . . . . :
Node Type . . . . . . . . . . . . : Broadcast

IP Routing Enabled. . . . . . . . : No

WINS Proxy Enabled. . . . . . . . : No


Ethernet adapter 本地连接 4:



Media State . . . . . . . . . . . : Cable Disconnected

Description . . . . . . . . . . . : Realtek RTL8139(A) PCI Fast Ethernet Adapter #3
Physical Address. . . . . . . . . : 00-1D-0F-17-3A-56


Ethernet adapter 本地连接 3:



Media State . . . . . . . . . . . : Cable Disconnected

Description . . . . . . . . . . . : Realtek RTL8139(A) PCI Fast Ethernet Adapter #2
Physical Address. . . . . . . . . : 00-19-E0-2C-74-B2


Ethernet adapter 本地连接 2:



Media State . . . . . . . . . . . : Cable Disconnected

Description . . . . . . . . . . . : Realtek RTL8139(A) PCI Fast Ethernet Adapter
Physical Address. . . . . . . . . : 00-1D-0F-17-5A-17


Ethernet adapter 本地连接:



Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Intel(R) PRO/100 VE Network Connection
Physical Address. . . . . . . . . : 00-90-27-11-26-3B

DHCP Enabled. . . . . . . . . . . : No

IP Address. . . . . . . . . . . . : 192.168.0.188

Subnet Mask . . . . . . . . . . . : 255.255.255.0

Default Gateway . . . . . . . . . :

DNS Servers . . . . . . . . . . . :

作者: bat-zw     时间: 2008-5-22 16:25
看这个应该就是我4楼的代码啊(不要改)。

作者: yinjie1755     时间: 2008-5-22 16:27    标题: 是的,,呵呵,我试了,OK!
是的,,呵呵,我试了,OK!我再分析一下,感觉可能会有问题!!

作者: yinjie1755     时间: 2008-5-22 16:30
判断的次数是在哪里控制?

作者: yinjie1755     时间: 2008-5-22 16:40
for /l %%i in (1,1,%n%) 这样子是不是不好控制判断的次数了呀?zw19750516
?

作者: bat-zw     时间: 2008-5-22 16:43
Originally posted by yinjie1755 at 2008-5-22 16:27:
是的,,呵呵,我试了,OK!我再分析一下,感觉可能会有问题!!


@echo off&setlocal enabledelayedexpansion
for /f "delims=" %%i in ('ipconfig /all') do (
set /a n+=1
set _!n!=%%i
echo %%i|findstr /c:"IP Address">nul&&set m=!n!
)
set /a m-=2
set str=!_%m%!
set mac=%str:~-18%
echo %mac%
pause>nul

----------------------------------------------------------------------------------------------------------
&&我还是解释下这段代码吧:
首先是将ipconfig /all所有的行都赋于数值变量,即用for读取一行就将n值加
1(初始n值为0),然后把这一行所有的字符赋值给_!n!;
然后再在所有的数值变量也就是ipconfig /all所有行中逐行查找IP Address字
符串,如找到则将这个行号赋值给m;
在ipconfig /all中可以看到有mac地址的行处于有IP Address字符行上面二行,
所以把m值减2;
最后截取_%m%(m已减2)后面的18个字符即为板载网卡mac。


Last edited by zw19750516 on 2008-5-22 at 05:06 PM ]

作者: yinjie1755     时间: 2008-5-22 16:52
那次数怎么控制,是不是有点耗时间!

作者: bat-zw     时间: 2008-5-22 16:57
Originally posted by yinjie1755 at 2008-5-22 16:52:
那次数怎么控制,是不是有点耗时间!

我不明白要控制什么次数啊?耗时是有点。

作者: yinjie1755     时间: 2008-5-22 17:09
就这样吧.真的很谢谢你了,我没有想到用这种办法,强人!谢谢了!我两天的问题解觉了!呵呵!我加了点东西,这样就可以防往服务器上记录所有主板的信息了,谢谢!测试PASS的板就不传了!(生产那边是要讲究效率的,呵呵!)
@echo off&setlocal enabledelayedexpansion

for /f " delims=" %%i in ('ipconfig /all') do (
set /a n+=1
set _!n!=%%i
)
for /l %%i in (1,1,%n%) do echo !_%%i!|findstr /c:"IP Address">nul&&set m=%%i
set /a m-=3
set str=!_%m%!
set mac=%str:~-18%
find /i /c "!mac!" \\192.168.94.27\t\testdate\winpass.log
if not errorlevel 1 goto mac
echo !mac!>>\\192.168.94.27\t\testdate\winpass.log
echo %mac%>>mac.log
pause
:mac
echo the mac is used!
echo the mac is used!
echo the mac is used!
echo the mac is used!
pause

作者: bat-zw     时间: 2008-5-22 17:12
注意我4楼和26楼修改和提效后的代码(刚刚修改的)。

作者: yinjie1755     时间: 2008-5-22 17:19
时间差不多,都是4S钟!!!

作者: pusofalse     时间: 2008-5-22 17:20
setlocal enabledelayedexpansion
for /f "delims=:" %%a in ('ipconfig /all ^|findstr /n /c:"IP Address"') do set m=%%a
set/a m-=2
for /f "delims=" %%a in ('ipconfig /all') do (
set/a n+=1
set a=%%a
if !n! equ %m% echo !a!
)
pause

我感觉这样能比较容易理解一些~

Last edited by pusofalse on 2008-5-22 at 05:21 PM ]