从批处理脚本分配IP [英] Assigning IP from Batch Script

查看:159
本文介绍了从批处理脚本分配IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下代码我收到错误无效的接口LAN:指定。此代码适用于Win7,但不适用于Windows XP。我怀疑它与!adapterName!有关。在XP中发生冲突。

With the below code I am getting an error "Invalid interface LAN: specified". This code works in Win7, however, does NOT working in windows XP. I suspect it has something to do with the "!adapterName!" conflicting within XP.

我利用这个脚本来获取NIC名称,以防将来更改某些方式并希望保留此方案。我有什么想法可以在XP环境中继续使用这样的脚本吗?

I utilize this script to obtain the NIC Name in case it changes in the future some how and would like to keep this scheme. Any ideas how I can continue with such script in an XP environment?

    :: 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


推荐答案

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

Here is an improved version of the batch code posted by T-Diddy which works on English and German 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

我在这里看不到使用

I don't see here a problem on Windows XP on using

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

但是在 for 循环中,使用了命令 findstr 而不是找到。并且双引号用于设置适配器名称,以避免错误地将尾随空格附加到适配器的名称。这可能是重要的区别。

But in the for loop the command findstr is used instead of find. And double quotes are used on setting the adapter name to avoid that by mistake a trailing space is appended to the name of the adapter. That could be the important difference.

阅读此答案的每个人的一般说明:

切勿输入带有1或2个前导零的IPv4地址的4个八位字节。这很容易导致被解释为八进制数,这绝对不是任何人所期望的。

Never enter the 4 octets of an IPv4 address with 1 or 2 leading zeros. That could very easily result in being interpreted as octal number which is definitely not expected by anybody.

你不相信。在Linux或Windows上试用,例如运行命令

You don't believe that. Try it out on Linux or Windows for example on running the command

ping 192.168.050.020

您是否预期在输出中写入192.168.40.16执行ping?

Do you have expected that ping is executed with 192.168.40.16 as written in output?

这篇关于从批处理脚本分配IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆