gpt4 book ai didi

windows - 作为 CMD 变量的特定以太网的 IP 地址

转载 作者:可可西里 更新时间:2023-11-01 11:41:55 33 4
gpt4 key购买 nike

我需要每 10 分钟检查一次我的 IP 地址并将其写入 csv 文件(如果它已更改)。但是,我有几个网卡在使用中。如何在 CMD 中使用其 MAC 地址获取特定卡的 IP 地址?

请参阅下文以修改对 another question 的回复通过 @mousio .虽然它对我不起作用!

@echo off
setlocal enabledelayedexpansion
set "MAC1=Physical Address"
set "MAC2=11-11-11-11-11-11"
set MACfound=false
for /f "usebackq tokens=1-2 delims=:" %%f in (`ipconfig /all`) do (
set "item1=%%f"
set "item2=%%g"
if /i "!item1!"=="!MAC1!" if "!item2!"=="!MAC2!" (
set MACfound=true
) else if not "!item1!"=="!item:IPv4 Address=!" if "!MACfound!"=="true" (
echo Your IP Address is: %%g
set MACfound=false
)
)

请参阅下面的 ipconfig/all 部分响应

Ethernet adapter Ethernet 3:

Connection-specific DNS Suffix . : xyz.xyz.com
Description . . . . . . . . . . . : Intel(R) 82579LM Gigabit Network Connection
Physical Address. . . . . . . . . : 11-11-11-11-11-11
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
Link-local IPv6 Address . . . . . : 1111::1111:1111:1111:111111(Preferred)
IPv4 Address. . . . . . . . . . . : 111.11.11.11(Preferred)
Subnet Mask . . . . . . . . . . . : 111.111.1.1
Lease Obtained. . . . . . . . . . : Thursday, July 25, 2019 9:51:30 AM
Lease Expires . . . . . . . . . . : Monday, August 26, 2019 12:33:23 PM
Default Gateway . . . . . . . . . : 111.11.1.1
DHCP Server . . . . . . . . . . . : 111.11.11.11
DHCPv6 IAID . . . . . . . . . . . : 111111111
DHCPv6 Client DUID. . . . . . . . : 11-11-11-11-11-11-11-11-11-11-11-11-11-11
DNS Servers . . . . . . . . . . . : 111.11.11.11
111.11.11.11
NetBIOS over Tcpip. . . . . . . . : Enabled

最佳答案

IPconfig 很难解析,因为所需的信息分布在多行中。使用正确的工具。我推荐wmic:

for /f "tokens=2 delims={}" %%a in ('wmic nicconfig where MACAddress^="11:11:11:11:11:11" get IPAddress /value') do echo %%~a

注意:wmic 中 MAC 地址的格式不同(冒号而不是破折号)。不要忘记转义 =

编辑:分隔 IPv4 和 IPv6 地址,只需将字符串与另一个 for 循环分开:

@echo off
setlocal enabledelayedexpansion
for /f "tokens=2 delims={}" %%a in ('wmic nicconfig where MACAddress^="11:11:11:11:11:11" get IPAddress /value') do set "adresses=%%a"
echo All Addresses: %adresses%
for %%a in (%adresses%) do (
echo %%~a|find "." >nul && set "ip4=!ip4!,%%~a
echo %%~a|find ":" >nul && set "ip6=!ip6!,%%~a
)
echo IPv4-Address(es): %ip4:~1%
echo IPv6-Address(es): %ip6:~1%

编辑(由 Mosy 编写):完成所有必需任务的代码,即在 csv 文件中写入 ip 地址,如果它发生变化,则每 10 分钟更新一次:

基本上,同一路径下会有两个批处理文件,第一个名为 ip_main.bat 并包含:

@echo off
echo -- IP ADDRESS UPDATER, PLEASE DO NOT CLOSE! --
set parent=%~dp0%
CD "%parent%"
setlocal enabledelayedexpansion
for /f "tokens=2 delims={}" %%a in ('wmic nicconfig where MACAddress^="11:11:11:11:11:11" get IPAddress /value') do set "adresses=%%a"

for %%a in (%adresses%) do (
echo %%~a|find "." >nul && set "ip4=!ip4!,%%~a
echo %%~a|find ":" >nul && set "ip6=!ip6!,%%~a
)
set ip4_old=%ip4:~1%
set ip6_old=%ip6:~1%
set "ip4=%ip4*=%"
set "ip6=%ip6*=%"

call ip_writer > ip_file.csv

:loop
for /f "tokens=2 delims={}" %%a in ('wmic nicconfig where MACAddress^="11:11:11:11:11:11" get IPAddress /value') do set "adresses=%%a"

for %%a in (%adresses%) do (
echo %%~a|find "." >nul && set "ip4=!ip4!,%%~a
echo %%~a|find ":" >nul && set "ip6=!ip6!,%%~a
)
set ip4_new=%ip4:~1%
set ip6_new=%ip6:~1%
set "ip4=%ip4*=%"
set "ip6=%ip6*=%"

if not "%ip4_new%"=="%ip4_old%" (
call ip_writer > ip_file.csv
set ip4_old=%ip4_new%
)

set "ip4_new=%ip4*=%"

timeout 600 /nobreak > nul
goto loop

第二个批处理文件称为 ip_writer.bat 并包含

@echo off
set parent=%~dp0%
CD "%parent%"
setlocal enabledelayedexpansion
for /f "tokens=2 delims={}" %%a in ('wmic nicconfig where MACAddress^="11:11:11:11:11:11" get IPAddress /value') do set "adresses=%%a"
rem echo All Addresses: %adresses%
for %%a in (%adresses%) do (
echo %%~a|find "." >nul && set "ip4=!ip4!,%%~a
echo %%~a|find ":" >nul && set "ip6=!ip6!,%%~a
)
set ip4=%ip4:~1%
set ip6=%ip6:~1%

echo ip4_address
echo %ip4%

关于windows - 作为 CMD 变量的特定以太网的 IP 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57230764/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com