窄口牛 发表于 2017-8-7 00:27:15

批处理获取有效IP,mac,计算机名

本帖最后由 窄口牛 于 2020-1-16 19:33 编辑

思路是这样的,tracert本地dns,取第一个值得到网关,然后ipconfig /all,得到网关和mac,然后将ip,网关,mac输出到计算机名的文本中。
因为一般单位管理都是要管外网的,所以这个思路应该成立。

更新为无须第三方版
NT6
@echo off
Setlocal enabledelayedexpansion
wget -O temp "http://ip.chinaz.com/getip.aspx"
cls
for /f "tokens=2 delims='" %%i in (temp) do set result=%%i
del temp 2>nul
for /f "tokens=8 delims= " %%a in ('tracert -d 114.114.114.114') do echo %%a>>temp1
for /f "delims=" %%i in ('ipconfig /all') do (
call,set t=%%i
call,echo %%t: =%%>>temp
)
for /f "tokens=1,3 delims=:" %%a in ('findstr ^/n ^/g:temp1 temp ^|find "默认网关"') do set Line=%%a&set Gateway=%%b
set /a a=%Line%-9
set /a b=%Line%-8
set /a c=%Line%-7
set /a d=%Line%-6
set /a e=%Line%-4
set /a f=%Line%-3
set /a g=%Line%-2
set /a h=%Line%-1
set /a i=%Line%+1
set /a j=%Line%+3
set /a k=%Line%+4
set /a l=%Line%+5
echo 外网IP是: %result%
for /f "tokens=1,2,3 delims=:" %%a in ('findstr /N . temp ^|findstr /b "%i%"') do echo %%b>temp2
for /f "tokens=1 delims=." %%a in ('type temp2') do (set ans=%%a&del temp2)
IF /I "%ans%" EQU "DHCP服务器" (
for /f "tokens=1,3 delims=:" %%a in ('findstr /N . temp ^|findstr /b "%a%"') do (echo 网卡名称:%%b)
for /f "tokens=1,3 delims=:" %%a in ('findstr /N . temp ^|findstr /b "%b%"') do (echo MCA地址:%%b)
for /f "tokens=1,3 delims=:" %%a in ('findstr /N . temp ^|findstr /b "%e%"') do (echo IP地址为:%%b)
for /f "tokens=1,3 delims=:" %%a in ('findstr /N . temp ^|findstr /b "%f%"') do (echo 子网掩码:%%b)
echo 默认网关:%Gateway%
for /f "tokens=1,3 delims=:" %%a in ('findstr /N . temp ^|findstr /b "%k%"') do (echo 首选DNS:%%b)
for /f "tokens=1,2 delims=:" %%a in ('findstr /N . temp ^|findstr /b "%l%"') do (echo 备用DNS:%%b)
del temp
echo 下面是出网路由路径
type temp1
del temp1
) ELSE (
for /f "tokens=1,3 delims=:" %%a in ('findstr /N . temp ^|findstr /b "%c%"') do (echo 网卡名称:%%b)
for /f "tokens=1,3 delims=:" %%a in ('findstr /N . temp ^|findstr /b "%d%"') do (echo MCA地址:%%b)
for /f "tokens=1,3 delims=:" %%a in ('findstr /N . temp ^|findstr /b "%g%"') do (echo IP地址为:%%b)
for /f "tokens=1,3 delims=:" %%a in ('findstr /N . temp ^|findstr /b "%h%"') do (echo 子网掩码:%%b)
echo 默认网关:%Gateway%
for /f "tokens=1,3 delims=:" %%a in ('findstr /N . temp ^|findstr /b "%j%"') do (echo 首选DNS:%%b)
for /f "tokens=1,2 delims=:" %%a in ('findstr /N . temp ^|findstr /b "%k%"') do (echo 备用DNS:%%b)
del temp
echo 下面是出网路由路径
type temp1
del temp1
)
pause>nul
NT5@echo off
Setlocal enabledelayedexpansion
wget -O temp "http://ip.chinaz.com/getip.aspx"
cls
for /f "tokens=2 delims='" %%i in (temp) do set result=%%i
del temp 2>nul
for /f "skip=2 tokens=8 delims= " %%a in ('tracert -d 114.114.114.114') do echo %%a>>temp1
for /f "delims=" %%i in ('ipconfig /all') do (
SET /a n+=1
call,set t=%%i
call,echo !n!:%%t: =%%>>temp
)
for /f "tokens=1,2,3 delims=:" %%a in ('findstr /G:temp1 temp ^|find "DefaultGateway"') do (
set Line=%%a&set Gateway=%%c
)
set /a a=%Line%-6
set /a b=%Line%-5
set /a c=%Line%-4
set /a d=%Line%-2
set /a e=%Line%-1
set /a f=%Line%+1
set /a g=%Line%+2
set /a h=%Line%+3
echo 外网IP是: %result%
for /f "tokens=1,2,3 delims=:" %%a in ('findstr /B "%f%" temp') do echo %%b>temp2
for /f "tokens=1 delims=." %%a in ('type temp2') do (set ans=%%a&del temp2)
if /i "%ans%" EQU "DHCPServer" (
for /f "tokens=1,3 delims=:" %%a in ('findstr /B "%a%" temp') do (echo 网卡名称:%%b)
for /f "tokens=1,3 delims=:" %%a in ('findstr /B "%b%" temp') do (echo MCA地址:%%b)
for /f "tokens=1,3 delims=:" %%a in ('findstr /B "%d%" temp') do (echo IP地址为:%%b)
for /f "tokens=1,3 delims=:" %%a in ('findstr /B "%e%" temp') do (echo 子网掩码:%%b)
echo 默认网关:%Gateway%
for /f "tokens=1,3 delims=:" %%a in ('findstr /B "%g%" temp') do (echo 首选DNS:%%b)
for /f "tokens=1,2 delims=:" %%a in ('findstr /B "%h%" temp') do (echo 备用DNS:%%b)
del temp
echo 下面是出网路由路径
type temp1
del temp1
) ELSE (
for /f "tokens=1,3 delims=:" %%a in ('findstr /B "%b%" temp') do (echo 网卡名称:%%b)
for /f "tokens=1,3 delims=:" %%a in ('findstr /B "%c%" temp') do (echo MCA地址:%%b)
for /f "tokens=1,3 delims=:" %%a in ('findstr /B "%d%" temp') do (echo IP地址为:%%b)
for /f "tokens=1,3 delims=:" %%a in ('findstr /B "%e%" temp') do (echo 子网掩码:%%b)
echo 默认网关:%Gateway%
for /f "tokens=1,3 delims=:" %%a in ('findstr /B "%f%" temp') do (echo 首选DNS:%%b)
for /f "delims=" %%a in ('findstr /B "%g%" temp') do (echo 备用DNS:%%a)
del temp
echo 下面是出网路由路径
type temp1
del temp1
)
pause>nul


窄口牛 发表于 2017-8-7 08:05:05

本帖最后由 窄口牛 于 2017-8-7 08:09 编辑

现在是这么拼凑的,nt5和nt6都正常工作。
@echo off
echo GET MAC ADDRESS
getmac /v > %Computername%.txt
for /f "skip=4 tokens=1,8" %%a in ('tracert -4 本地dns地址) do (
    if "%%a" equ "1" echo 网关地址:%%b >> %Computername%.txt
)
pause

得到结果
连接名          网络适配器      物理地址            传输名称                                                
=============== =============== =================== ==========================================================
本地连接      Realtek PCIe GB 00-30-18-A4-A3-77   \Device\Tcpip_{10F76D38-5CF3-46CF-8EEF-41DC9ECEA4DF}      
VMware NetworkVMware Virtual00-50-56-C0-00-01   \Device\Tcpip_{648078BB-82BC-459C-BFEC-9A0F784EBAF5}      
VMware NetworkVMware Virtual00-50-56-C0-00-08   \Device\Tcpip_{8BC73126-1161-41C8-8E3A-9E8D0645D011}      
本地连接 3      Dell Wireless 1 C0-18-85-12-A8-A5   媒体已断开连接                                          
网关地址:192.168.2.254
需要手工筛选

nttwqz 发表于 2017-8-7 13:20:57

其实,如果不需要自动还原,ipconfig /all就可以满足需求。

窄口牛 发表于 2017-8-7 15:09:24

是的,主要是想减去人工筛选。

kingshui 发表于 2017-8-16 16:39:07

本帖最后由 kingshui 于 2017-8-16 16:45 编辑

取IP和mac。。。。

for /f "tokens=1 delims= " %i in ('wmic nic get name^,index^|findstr /i "intel realtek"') do (wmic nicconfig where index=%i get ipaddress,macaddress /value)

常用网卡牌子写上去试下。

窄口牛 发表于 2017-8-28 20:16:50

@echo off
setlocal enabledelayedexpansion
for /f "tokens=4 delims=: " %%i in ('nbtstat -n ^| findstr /i "ip"') do (
    set "IP_addr=%%i"
    set IP_addr=!IP_addr:~1,-1!
    echo 本机IP为:!IP_addr!>>%Computername%.txt
)
for /F "skip=1 delims=" %%a in ('nbtstat -n') do (
    set "IP_name=%%a"
    set IP_name=!IP_name:~,-1!&goto 1
)
:1
for /F "tokens=3 delims=:," %%m in ('getmac /v /fo csv ^|findstr !IP_name!') do (
    set "IP_mac=%%m"
    SET IP_mac=!IP_mac:~1,-1!
echo 本机MAC为:!IP_mac!>>%Computername%.txt
)
拼凑了一个这样的

窄口牛 发表于 2017-8-29 09:17:20

多网卡已连接就不好使了

nttwqz 发表于 2017-8-29 19:48:23

窄口牛 发表于 2017-8-28 20:16
拼凑了一个这样的

你这是单位外网用的?

刚试了,家用不好用,出来三个IP,而且命令窗口还findstr错误

我用下面的命令可以正常显示。

wmic nicconfig where "dhcpenabled='false' and ipenabled='true'" get caption,ipaddress,ipsubnet,DefaultIPGateway,DNSServerSearchOrder,macaddress /format:hform >C:\IP.html

窄口牛 发表于 2017-8-29 20:30:28

我那个瞎弄得,这个好用。

kingshui 发表于 2017-9-1 10:46:56

nttwqz 发表于 2017-8-29 19:48
你这是单位外网用的?

刚试了,家用不好用,出来三个IP,而且命令窗口还findstr错误


兄台的命令不错,不过参数好像错了?一般单位都使用dhcp来分配IP,所以,dhcpenabled还是要为true的,输出为文本文本比html应该更方便些?

我应用到我的域脚本发粗吧,多谢!

仅供参考

wmic nicconfig where "dhcpenabled='true' and ipenabled='true'" getcaption,ipaddress,macaddress /value|more|find "="

nttwqz 发表于 2017-9-1 16:49:19

kingshui 发表于 2017-9-1 10:46
兄台的命令不错,不过参数好像错了?一般单位都使用dhcp来分配IP,所以,dhcpenabled还是要为true的, ...

意思是自动获取?

我这是显示手动分配的IP,自动的还备份个什么……

窄口牛 发表于 2017-9-1 17:31:50

是的,手动才需要备份。
自动的没必要了

foxesp 发表于 2017-9-2 07:19:36

支持顶下

窄口牛 发表于 2017-9-2 13:32:18

@echo off
setlocal ENABLEEXTENSIONS
wget -O temp "http://ip.chinaz.com/getip.aspx"
cls
for /f "tokens=2 delims='" %%i in (temp) do set result=%%i
del temp 2>nul
echo 外网IP是: %result%
for /f"tokens=2* delims=]" %%i in ('wmic nicconfig where "dhcpenabled='true' and ipenabled='true'" 2^>nul getcaption /value ^|find "="') do echo %%i%%j>>temp1
for /f"tokens=2* delims=]" %%i in ('wmic nicconfig where "dhcpenabled='false' and ipenabled='true'" 2^>nul getcaption /value ^|find "="') do echo %%i%%j>>temp1
for /f "tokens=* delims= " %%a in (temp1) do echo 网卡名称:%%a|findstr /v "VMware Microsoft"&del temp1 2>nul
for /f"tokens=2 delims==" %%i in ('wmic nicconfig where "dhcpenabled='true' and ipenabled='true'" 2^>nul get macaddress /value ^|find "="') do echo MAC地址:%%i|findstr /v "00:0C:29 00:50:56 02:00:4C"
for /f"tokens=2 delims==" %%i in ('wmic nicconfig where "dhcpenabled='false' and ipenabled='true'" 2^>nul get macaddress /value ^|find "="') do echo MAC地址:%%i|findstr /v "00:0C:29 00:50:56 02:00:4C"
set "i=0.0.0.0" & set "j="
for /f "tokens=4" %%a in ('route print^|findstr 0.0.0.0.*0.0.0.0') do (
if not defined j for %%b in (%%a) do set "i=%%b" & set "j=1")
echo IP地址:%i%
for /f tokens^=2^,4delims^=^" %%i in ('wmic nicconfig where "dhcpenabled='true' and ipenabled='true'" 2^>nul get IPSubnet /value ^|find "="') do echo 子网掩码:%%i %%j
for /f tokens^=2^,4delims^=^" %%i in ('wmic nicconfig where "dhcpenabled='false' and ipenabled='true'" 2^>nul get IPSubnet /value ^|find "="') do echo 子网掩码:%%i %%j
for /f "tokens=3" %%a in ('route print^|findstr 0.0.0.0.*0.0.0.0') do (
if not defined k for %%b in (%%a) do set "g=%%b" & set "k=1")
echo 默认网关:%g%
for /f tokens^=2^,4delims^=^" %%i in ('wmic nicconfig where "dhcpenabled='true' and ipenabled='true'" 2^>nul get DNSServerSearchOrder /value ^|find "="') do echo 首选DNS:%%i&echo 备用DNS:%%j
for /f tokens^=2^,4delims^=^" %%i in ('wmic nicconfig where "dhcpenabled='false' and ipenabled='true'" 2^>nul get DNSServerSearchOrder /value ^|find "="') do echo 首选DNS:%%i&echo 备用DNS:%%j
echo 下面是出网路由路径显示
echo 稍微有点慢,请等待
for /F "skip=3 delims=" %%i in ('tracert -d 114.114.114.114') do echo %%i
pause >nul

最终定稿

nttwqz 发表于 2017-9-2 23:04:15

窄口牛 发表于 2017-9-2 13:32
最终定稿

wmic nicconfig where "ipenabled='true' and not description like '%%Microsoft%%' and not description like '%%Vmware%%'" get description

窄口牛 发表于 2017-9-2 23:11:45

明白,谢谢指教。

窄口牛 发表于 2017-9-3 12:26:04

绕了半天还是没啥用,还是不能解决有效问题,只能tracert到网关,然后在ipconfig里找距离这个网关最近的别的参数,批处理实现太困难了。

窄口牛 发表于 2020-1-16 19:33:42

更新为无需第三方版
页: [1]
查看完整版本: 批处理获取有效IP,mac,计算机名