【发布时间】:2017-10-13 10:42:40
【问题描述】:
我将创建一个小批处理文件来将我的 IP 地址直接复制到我的剪贴板。我试过了:
@echo off
ipconfig | find "IPv4" | clip
pause
但是给了我:IPv4 Address. . . . . . . . . . . : 192.168.xx.xx。有没有办法只得到192.168.xx.xx?
【问题讨论】:
标签: batch-file command ip-address
我将创建一个小批处理文件来将我的 IP 地址直接复制到我的剪贴板。我试过了:
@echo off
ipconfig | find "IPv4" | clip
pause
但是给了我:IPv4 Address. . . . . . . . . . . : 192.168.xx.xx。有没有办法只得到192.168.xx.xx?
【问题讨论】:
标签: batch-file command ip-address
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 使用方括号作为分隔符对行进行标记,并检索第二个标记
【讨论】:
这个批处理文件可以解决问题,当然也可以给你 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
【讨论】:
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 );
【讨论】: