我目前在一家酒厂工作,该酒厂主要使用基于以太网的控制网络,所有自动化设备都是专用的。为了帮助节省时间,我为酒厂的每个系统创建了一个批处理文件,以便当我在工厂的不同地方时,能够快速改变我的计算机的IP地址。is escaped with a caret). The second line is a re-implementation of your
statement.
will link the commands together so that they can be processed in the same
@echo off
netsh interface IPv4 set address name="Local Area Connection" static 10.120.194.254 255.255.255.0
@echo off
netsh interface IPv4 set dns name="Local Area Connection" source=dhcp
clause. If the
@echo off
ping "10.120.194.254"
IF %errorlevel%==0 GOTO IP_Attempt2
@echo off
netsh interface IPv4 set address name="Local Area Connection" static 10.120.194.254 255.255.255.0
@echo off
netsh interface IPv4 set dns name="Local Area Connection" source=dhcp
exit /b
:IP_Attempt2
ping "10.120.194.253"
IF %errorlevel%==0 GOTO IP_Attempt3
@echo off
netsh interface IPv4 set address name="Local Area Connection" static 10.120.194.253 255.255.255.0
@echo off
netsh interface IPv4 set dns name="Local Area Connection" source=dhcp
exit /b
:IP_Attempt3
ping "10.120.194.252"
IF %errorlevel%==0 GOTO IP_Attempt4
@echo off
netsh interface IPv4 set address name="Local Area Connection" static 10.120.194.252 255.255.255.0
@echo off
netsh interface IPv4 set dns name="Local Area Connection" source=dhcp
exit /b
:IP_Attempt4
ping "10.120.194.251"
IF %errorlevel%==0 GOTO IP_Attempt5
@echo off
netsh interface IPv4 set address name="Local Area Connection" static 10.120.194.251 255.255.255.0
@echo off
netsh interface IPv4 set dns name="Local Area Connection" source=dhcp
exit /b
:IP_Attempt5
ping "10.120.194.250"
IF %errorlevel%==0 GOTO Max_IP_Attempts
@echo off
netsh interface IPv4 set address name="Local Area Connection" static 10.120.194.250 255.255.255.0
@echo off
netsh interface IPv4 set dns name="Local Area Connection" source=dhcp
exit /b
:Max_IP_Attempts
echo Max attempts have been made with this batch file. Verify users on the network.
pause
exit /b
. It will only ping once before moving on.For most built-in commands, you can get a satisfactory printout of functionality by trying in CMD. SS64
,DosTips
%errorlevel%
, and0
the Wikibooks page on batch scriptingFIND
are also good references to have around if you ever get stuck.
Hope this helps. This is my first contribution as well, so please feel free to ask if I left something unclear (or if you have additional questions).FIND
FOR
FIND
Reading your question looks like it boils down to the question:
FOR /F "delims=" %%f IN ('ping "IP" ^| find "TTL"') DO (SET tempVar=%%f)
IF NOT DEFINED tempVAR netsh interface IPv4 set address name="Local Area Connection" static IP 255.255.255.0 & netsh interface IPv4 set dns name="Local Area Connection" source=dhcp & EXIT /B
GOTO IP_Attempt$
|
How can I find used or free IP Addresses in my subnet using windows batch files?ping
And the answer consists basically of three lines of code plus some explanations:find
Every IP which responds to a single ping is written to IF
. That's your "don't use"-list.You could easily customize this snippet to work automatically every n minutes and without user input or to output free ips and ignore used ones.&
If you want to set the ip address automatically after your script found a free one, use IF
. For further information on using IF
you might want to read GOTO
assign the ip address by the using the batch file
orHow to change ip address using script on windows
.ping -n 1 target_IP
While this approach may fit your needs perfectly, it should be considered to use DHCP-based ip-management or fixed IPs for every station. But that's not the StackOverflow-Part of your question. Help regarding networking and infrastructure management can be found on other communities. 虽然这很好用,但我想到,如果多人试图以这种方式上网,网络上会有冲突的IP地址。[command] /?
我尝试修改我的批处理文件,首先ping一个IP地址(10.120.194.254),然后根据结果,改变IPv4 "局域网连接 "到该IP地址,或者继续到下一个可用的IP地址(10.120.194.253)。(10.120.194.252)...等等。我的理想目标是对5个IP地址执行同样的程序,然后如果所有的IP地址都不成功,就会出现一个对话框,显示一条消息来检查网络上的用户数量。通常情况下,IP地址xxx.xxx.xxx.250-254是用于网络上编程的,所以这就是背后的原因。一个网络上出现这么多用户的概率很低,但如果有这个功能,对于这种 "以防万一 "的情况,也是一个不错的功能。
下面是我的原始批处理文件的代码,用于更改IPv4 "局域网连接 "的IP地址。我试着使用errorlevel函数,但在ping网络时无法得到一致的结果。此外,每次我ping一个地址时,它都会轮询4次,然后转到下一个。有什么办法可以缩短这个轮询时间吗?下面是我的批处理文件的代码,我一直在尝试让它工作。我附上了运行批处理文件后CMD提示窗口的快照。
我目前在一家酒厂工作 主要使用基于以太网的控制网络 所有的自动化设备都是专用的。我是该酒厂唯一的控制工程师,是 ...
来自:
一个成功的PING并不总是返回一个
的
if exist ips.txt del ips.txt > nul
echo Please enter the first 24 bits of your subnet (e.g. 192.168.0):
set /p ip=
for /L %%N IN (1, 1, 254) DO (
echo pinging %ip%.%%N
ping %ip%.%%N -n 1 -w 1 | find "TTL" && echo %ip%.%%N >> ips.txt
)
cls
type ips.txt
因此,为了可靠地检测到一个成功的ping,将输出的管道输入到 ips.txt
并寻找文本 "TTL"。
在你的例子中,你将会想把输出的 netsh interface ip set address "LAN" static %ip% %subnet% %gateway%
变成一个带有 netsh
循环。如果 没有在输出文本中找到 "TTL",变量将不会被定义。你可以检查变量是否被定义,以定义你的结果(要么移动到下一个IP,要么将当前IP设置为本地地址).在你的脚本中,这可以看起来像这样。在第一行: 管道的输出。 进入
命令(