仅在bat文件中获取没有“IPv4地址... ...:”的IPv4地址

时间:2017-05-14 16:06:42

标签: batch-file command ip-address

我将创建一个小批量文件,将我的IP地址直接复制到我的剪贴板。我试过了:

@echo off
ipconfig | find "IPv4" | clip
pause

但是给了我:IPv4 Address. . . . . . . . . . . : 192.168.xx.xx。有没有办法只获得192.168.xx.xx

4 个答案:

答案 0 :(得分:2)

for /f "tokens=2 delims=[]" %%a in ('ping -n 1 -4 ""') do echo %%a | clip
  • 对本地计算机(ping)执行""命令,使用ipv4(-n 1)只发送一个数据包(-4

    < / LI>
  • 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)

答案 1 :(得分:2)

这个批处理文件可以解决这个问题,如果你想要的话也可以给你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

答案 2 :(得分:-1)

在这里我创建了一个批处理文件。您可以根据需要将其裁剪。

@echo off
ipconfig | findstr /R /C:"IPv4 Address"
pause

您可以在此处查看批处理文件的输出...

enter image description here

答案 3 :(得分:-1)

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 );