簡體   English   中英

制作一個批處理文件,首先 ping IP 地址,並在設置 IPv4“本地連接”的 IP 地址之前驗證它沒有被使用

[英]Make a batch file that will first ping an IP address and verify it's not used before setting the IP address of the IPv4 “Local Area Connection”

我目前在一家釀酒廠工作,該釀酒廠主要使用基於以太網的控制網絡,該網絡專用於所有自動化設備。 為了節省時間,我為釀酒廠的每個系統創建了一個批處理文件,以便當我在工廠的不同部分時能夠快速更改我的計算機的 IP 地址。 我創建了這個批處理文件目錄並將它們放在我們的服務器上,以便我們的維護人員可以訪問它們。 雖然這很好用,但我想到如果多人嘗試以這種方式在線 go,那么網絡上將會出現沖突的 IP 地址。

我試圖將我的批量文件(s)首先修改為ZA12A3079E14CED46E6BA52B8A90B21AZ地址(10.120.194.254),然后基於結果更改IPV4局部連接的ZA12A3079E1414CBARENINGRION ZA12AE14CBAR OYTRASE ORNASER ENLICAT1111146EBAD52B2BA2BA2BA2BA2BA2BA2BA81BA52BA8A.AN991BBAR ENARENCE OR .194.253)... (10.120.194.252)... 等等。 我的理想目標是對 5 個 IP 地址執行相同的過程,然后如果所有地址都不成功,則顯示一個對話框,顯示一條消息以檢查網絡上的用戶數量。 通常,IP 地址 xxx.xxx.xxx.250-254 用於在網絡上進行編程,因此這就是背后的原因。 網絡上有這么多用戶的可能性很低,但對於“以防萬一”的情況來說,這將是一個很好的功能。

下面是我的原始批處理文件代碼,用於更改 IPv4“本地連接”的 IP 地址。

@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

我嘗試使用錯誤級別 function 但在 ping 網絡時無法獲得一致的結果。 此外,每次我 ping 一個地址時,它都會輪詢 4 次,然后轉到下一個。 有沒有辦法縮短這個輪詢時間? 下面是我一直試圖開始工作的批處理文件的代碼:

@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

運行批處理文件后,我附上了 CMD 提示 window 的快照: IP 地址快照

https://ss64.com/nt/ping.html

成功的 PING 並不總是返回0%errorlevel%因此,為了可靠地檢測到成功的 ping,pipe 將 output 放入FIND並查找文本“TTL”。

在您的實例中,您需要將FIND的 pipe 和 output 轉換為帶有FOR循環的變量。 如果FIND在 output 文本中沒有找到“TTL”,則不會定義該變量。 您可以檢查變量是否已定義以定義您的結果(移動到下一個 IP 或將當前 IP 設置為本地地址)。 在您的腳本中,這可能如下所示:

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$

在第一行, | ping的 output 輸送到find命令中(| 用插入符號轉義)。 第二行是您的IF語句的重新實現。 &將命令鏈接在一起,以便它們可以在同一個IF子句中處理。 如果IF子句不匹配,第三行將GOTO您的下一個 label。

此外,每次我 ping 一個地址時,它都會輪詢 4 次,然后轉到下一個。 有沒有辦法縮短這個輪詢時間?

嘗試ping -n 1 target_IP 在繼續之前它只會 ping 一次。 對於大多數內置命令,您可以通過嘗試[command] /?獲得令人滿意的功能打印輸出。 在 CMD 中。

SS64DosTips有關批處理腳本的 Wikibooks 頁面也是您遇到困難時的不錯參考。

希望這可以幫助。 這也是我的第一個貢獻,所以請隨時詢問我是否有不清楚的地方(或者如果您有其他問題)。

閱讀您的問題看起來可以歸結為問題:

如何使用 windows 批處理文件在我的子網中找到使用過的或免費的 IP 地址?

答案基本上由三行代碼加上一些解釋組成:

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 的 IP 都會寫入ips.txt 那是您的“不使用”列表。 您可以輕松自定義此代碼段,使其每 n 分鍾自動運行一次,無需用戶輸入或 output 免費 ips 並忽略使用過的。

如果要在腳本找到空閑地址后自動設置 ip 地址,請使用netsh interface ip set address "LAN" static %ip% %subnet% %gateway% 有關使用netsh的更多信息,您可能需要閱讀使用批處理文件分配 ip 地址如何使用 windows 上的腳本更改 ip 地址


雖然這種方法可能完全符合您的需求,但應考慮為每個站點使用基於 DHCP 的 ip 管理或固定 IP。 但這不是您問題的 StackOverflow 部分。 有關網絡和基礎設施管理的幫助可以在其他社區找到。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM