2014-03-05 88 views
4

通过下面的代码,我得到一个错误“Invalid interface LAN:specified”。此代码在Win7中工作,但是,不能在Windows XP中工作。我怀疑它与“!adapterName!”有关在XP内部发生冲突。从批处理脚本中分配IP

我利用这个脚本来获取网卡名称,以防它在将来会发生什么变化,并希望保留这个方案。任何想法如何在XP环境中继续使用这样的脚本?

:: Set primary and alternate DNS for IPv4 
@ECHO OFF 
for /f "tokens=* delims=0" %%a in ("%SITEID%") DO set DROPZERO=%%a 
SETLOCAL EnableDelayedExpansion 

SET adapterName= 

FOR /F "tokens=* delims=:" %%a IN ('IPCONFIG ^| FIND /I "ETHERNET ADAPTER"') DO SET adapterName=%%a 

REM Removes "Ethernet adapter" from the front of the adapter name 
SET adapterName=!adapterName:~17! 
REM Removes the colon from the end of the adapter name 
SET adapterName=!adapterName:~0,-1! 

echo Setting Static IP Information... 
set IP_Addr=10.102.%DROPZERO%.105 
set D_Gate=10.102.%DROPZERO%.1 
set Sub_Mask=255.255.255.0 
netsh interface ip set address "!adapterName!" static %IP_Addr% %Sub_Mask% %D_Gate% 1 > output_net.txt 
netsh interface ip set dns name="!adapterName!" static 10.98.1.26 primary >> output_net.txt 
netsh interface ip add dns name="!adapterName!" 10.98.1.48 index=2 >> output_net.txt 
+0

也许'ipconfig'输出的格式不同于Windows 7到XP。看看'IPCONFIG^|的输出在Windows 7中查找/ I“ETHERNET ADAPTER”,然后在XP中查找。如果格式不同,那么for循环可能不会正确设置'adapterName'。 – unclemeat

+0

我按照你的建议分解了它,并且正确设置了adaptername。 –

+0

用'echo netsh'替换你的'netsh'调用,看看这些命令是否是你认为的那些。 – Mark

回答

1

这里是T-Diddy发布的批处理代码的改进版,适用于英语和德语的Windows。

@echo off 
rem Set primary and alternate DNS for IPv4. 
setlocal 
set "SITEID=020" 

rem The next line removes leading zeros to define the third octet of the 
rem IPv4 address always correct. There are implementations on IP address 
rem parsing routines on Windows (for example command ping) and Linux which 
rem interpret integer numbers with a leading 0 as octal number and not as 
rem decimal number according to C standard. The number 020 would be set 
rem unexpected as decimal number 16 instead of 20. 
for /F "tokens=* delims=0" %%A in ("%SITEID%") do set DROPZERO=%%A 

rem Get first ethernet adapter name output by command IPCONFIG filtered 
rem by command FINDSTR with a case-insensitive regular expression search. 
rem "Ethernet adapter " is output left to the name of the adapter on English 
rem Windows. But on German Windows "Ethernetadapter" is the right word. This 
rem is the reason for the regular expression with an optional space between 
rem "ethernet" and "adpater". 
set "AdapterName=" 
for /F "tokens=* delims=:" %%A in ('%SystemRoot%\System32\ipconfig.exe ^| %SystemRoot%\System32\findstr.exe /I /R "ethernet.*adapter"') do (
    set "AdapterName=%%A" 
    goto SetAddress 
) 
rem No ethernet adapter found, exit batch job. 
endlocal 
goto :EOF 

:SetAddress 
rem Remove everything from beginning of string to first occurrence of 
rem the string "adapter " to remove "Ethernet adapter " respectively 
rem on German Windows "Ethernetadapter". 
set "AdapterName=%AdapterName:*adapter =%" 

rem Remove the colon from the end of the adapter name. 
set "AdapterName=%AdapterName:~0,-1%" 

echo Ethernet adapter found: %AdapterName% 
echo Setting static IP information ... 
set IpAddress=10.102.%DROPZERO%.105 
set DefaultGate=10.102.%DROPZERO%.1 
set SubNetMask=255.255.255.0 
%SystemRoot%\System32\netsh.exe interface ip set address "%adapterName%" static %IpAddress% %SubNetMask% %DefaultGate% ^1>output_net.txt 
%SystemRoot%\System32\netsh.exe interface ip set dns name="%adapterName%" static 10.98.1.26 primary>>output_net.txt 
%SystemRoot%\System32\netsh.exe interface ip add dns name="%adapterName%" 10.98.1.48 index=^2>>output_net.txt 
endlocal 

我在这里看不到在Windows XP中的一个问题上使用

set "AdapterName=%AdapterName:~0,-1%" 

但在for循环命令findstr是用来代替find。在设置适配器名称时使用双引号,以避免错误地将尾随空格附加到适配器的名称上。这可能是重要的区别。

供大家阅读这个答案的一般注意事项:

决不1个或2前导零进入4个字节的IPv4地址。这很容易导致被解释为八进制数,这绝对不是人们所期望的。

你不相信。尝试一下在Linux或Windows例如在运行命令

ping 192.168.050.020 

你有没有预料写在输出平与192.168.40.16执行?