标题: [网络连接] [已解决]断网状态下批处理如何获取ip地址 [打印本页]
作者: 同行的你 时间: 2021-3-16 22:15 标题: [已解决]断网状态下批处理如何获取ip地址
正常情况下可以通过ipconfig /all或route print获取IP地址,MAC地址。但现在有个需求需要在不插网线不联网的情况下获取机器的IP地址(机器的IP地址是固定的)。
这个是可以实现的吗?谢谢大佬们!
作者: cutebe 时间: 2021-3-17 08:28
IP:netsh
MAC:getmac
作者: 同行的你 时间: 2021-3-17 10:39
回复 2# cutebe
非常感谢您的指导。测试可用!谢谢!
作者: Batcher 时间: 2022-3-25 14:56
1. netsh 命令- netsh interface ip dump | findstr "address="
复制代码
2. 注册表- @echo off
- for /f "tokens=3" %%i in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards" /s ^| findstr "ServiceName"') do (
- for /f "tokens=3" %%j in ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\%%i" /v IPAddress 2^>nul ^| findstr "IPAddress"') do (
- echo,%%j
- )
- )
- pause
复制代码
作者: Echo11 时间: 2022-3-28 16:06
本帖最后由 Echo11 于 2022-3-28 16:08 编辑
回复 4# Batcher
大佬你好,细究发现,注册表方法简单这样写在某些情况下是不准确的,比如你先动态获取了IP,然后改又设置了静态IP,虽然网络适配器状态中只显示当前使用的,但是注册表里不会马上把原来动态获取的IP等信息清空。动态IP和静态IP等信息,在这个注册表项下,是用不同的键名来存储的。这样会获得两个IP地址。
下面是我修改的显示完整信息的代码(未精简)- @ECHO OFF
- for /f "tokens=3" %%i in ('reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards" /s ^| findstr "ServiceName"') do (
- ECHO.------------------------------
- for /f "tokens=3" %%j in ('reg query "HKLM\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\%%i\Connection" /s /v "Name" ^| findstr /I "Name"') do ECHO.网络链接名称:[%%j]
- for /f "tokens=3" %%k in ('reg query "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\%%i" /v "EnableDHCP" ^| findstr /I "EnableDHCP"') do (
- REM 使用DHCP,DNS可以是DHCP获取,可以是静态指定
- IF %%k==0x1 (
- ECHO.DHCP:启用
- for /f "tokens=3" %%l in ('reg query "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\%%i" ^| findstr /I /C:" DhcpIPAddress "') do ECHO.IPAddress:[%%l]
- for /f "tokens=3" %%l in ('reg query "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\%%i" ^| findstr /I /C:" DhcpSubnetMask "') do ECHO.SubnetMask:[%%l]
- for /f "tokens=3" %%l in ('reg query "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\%%i" ^| findstr /I /C:" DhcpDefaultGateway "') do ECHO.DefaultGateway:[%%l]
- for /f "tokens=2,*" %%m in ('reg query "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\%%i" ^| findstr /I /C:" NameServer "') do IF not "%%n"=="" (
- ECHO.Static DNS:[%%n]
- ) else (
- for /f "tokens=2,*" %%o in ('reg query "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\%%i" ^| findstr /I /C:" DhcpNameServer "') do ECHO.DHCP DNS:[%%p]
- )
- )
- REM 不使用DHCP,DNS必须也是静态指定
- IF %%k==0x0 (
- ECHO.DHCP:未启用
- for /f "tokens=3" %%l in ('reg query "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\%%i" ^| findstr /I /C:" IPAddress "') do ECHO.IPAddress:[%%l]
- for /f "tokens=3" %%l in ('reg query "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\%%i" ^| findstr /I /C:" SubnetMask "') do ECHO.SubnetMask:[%%l]
- for /f "tokens=3" %%l in ('reg query "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\%%i" ^| findstr /I /C:" DefaultGateway "') do ECHO.DefaultGateway:[%%l]
- for /f "tokens=2,*" %%m in ('reg query "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\%%i" ^| findstr /I /C:" NameServer "') do ECHO.Static DNS:[%%n]
- )
- )
- )
- pause
复制代码
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |