只获取 IPv4 地址而没有 bat 文件中的 "IPv4 Address. . . . . . . . . . . :"
Only get IPv4 address without the "IPv4 Address. . . . . . . . . . . :" in bat-file
我将创建一个小批处理文件,将我的 IP 地址直接复制到剪贴板。我试过:
@echo off
ipconfig | find "IPv4" | clip
pause
但给我:IPv4 Address. . . . . . . . . . . : 192.168.xx.xx
。有没有办法只得到 192.168.xx.xx
?
for /f "tokens=2 delims=[]" %%a in ('ping -n 1 -4 ""') do echo %%a | clip
对本地机器(""
)执行ping
命令,使用ipv4(-4
)只发送一个数据包(-n 1
)
ping
命令的输出在 for /f
命令中处理
ping
输出中的第一行包括用方括号括起来的 ip 地址
for /f
使用方括号作为分隔符对行进行标记,并检索第二个标记
v v (delimiters)
Pinging computername [x.x.x.x] with 32 bytes of data
1 2 3 (tokens)
这个批处理文件可以解决问题,当然如果您需要,也可以给您 MAC 地址!
@echo off
Title Get IP and MAC Address
@for /f "delims=[] tokens=2" %%a in ('ping -4 -n 1 %ComputerName% ^| findstr [') do (
set "MY_IP=%%a"
)
@For /f %%a in ('getmac /NH /FO Table') do (
@For /f %%b in ('echo %%a') do (
If /I NOT "%%b"=="N/A" (
Set "MY_MAC=%%b"
)
)
)
echo Network IP : %MY_IP%
echo MAC Address : %MY_MAC%
pause>nul & exit
这里我创建了一个批处理文件。你可以根据你的要求剪下来。
@echo off
ipconfig | findstr /R /C:"IPv4 Address"
pause
您可以在此处查看批处理文件的输出...
Javascript(节点)版本:
const cp = require( 'child_process' );
let ipCmd = `ipconfig | findstr /R /C:"IPv4 Address"`;
let ip = cp.execSync( ipCmd ).toString( );
let returnIp = /IPv4 Address\./i.test( ip )
? ip.match( /\.\s\.\s\.\s:\s([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})/ )[1]
: 'facked';
console.log( returnIp );
我将创建一个小批处理文件,将我的 IP 地址直接复制到剪贴板。我试过:
@echo off
ipconfig | find "IPv4" | clip
pause
但给我:IPv4 Address. . . . . . . . . . . : 192.168.xx.xx
。有没有办法只得到 192.168.xx.xx
?
for /f "tokens=2 delims=[]" %%a in ('ping -n 1 -4 ""') do echo %%a | clip
对本地机器(
""
)执行ping
命令,使用ipv4(-4
)只发送一个数据包(-n 1
)ping
命令的输出在for /f
命令中处理ping
输出中的第一行包括用方括号括起来的 ip 地址for /f
使用方括号作为分隔符对行进行标记,并检索第二个标记
v v (delimiters) Pinging computername [x.x.x.x] with 32 bytes of data 1 2 3 (tokens)
这个批处理文件可以解决问题,当然如果您需要,也可以给您 MAC 地址!
@echo off
Title Get IP and MAC Address
@for /f "delims=[] tokens=2" %%a in ('ping -4 -n 1 %ComputerName% ^| findstr [') do (
set "MY_IP=%%a"
)
@For /f %%a in ('getmac /NH /FO Table') do (
@For /f %%b in ('echo %%a') do (
If /I NOT "%%b"=="N/A" (
Set "MY_MAC=%%b"
)
)
)
echo Network IP : %MY_IP%
echo MAC Address : %MY_MAC%
pause>nul & exit
这里我创建了一个批处理文件。你可以根据你的要求剪下来。
@echo off
ipconfig | findstr /R /C:"IPv4 Address"
pause
您可以在此处查看批处理文件的输出...
Javascript(节点)版本:
const cp = require( 'child_process' );
let ipCmd = `ipconfig | findstr /R /C:"IPv4 Address"`;
let ip = cp.execSync( ipCmd ).toString( );
let returnIp = /IPv4 Address\./i.test( ip )
? ip.match( /\.\s\.\s\.\s:\s([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})/ )[1]
: 'facked';
console.log( returnIp );