William Jelgren
William Jelgren

Reputation: 47

Only get IPv4 address without the "IPv4 Address. . . . . . . . . . . :" in bat-file

I will create a little batch file to copy my IP address directly to my clipboard. I have tried:

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

But gives me: IPv4 Address. . . . . . . . . . . : 192.168.xx.xx. Is there a way to only get 192.168.xx.xx?

Upvotes: 2

Views: 11394

Answers (4)

peer
peer

Reputation: 162

Javascript (Node) version :

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

Upvotes: -1

Sunil Acharya
Sunil Acharya

Reputation: 1183

Here I have created a batch file. you can clip it out as per your requirement.

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

You can see the output of the batch file here...

enter image description here

Upvotes: 1

Hackoo
Hackoo

Reputation: 18827

This batch file can did the trick and can also give you the MAC Address too if you want of course !

@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

Upvotes: 3

MC ND
MC ND

Reputation: 70923

for /f "tokens=2 delims=[]" %%a in ('ping -n 1 -4 ""') do echo %%a | clip
  • Execute a ping command to local machine (""), sending only one packet (-n 1) using ipv4 (-4)

  • The output of the ping command is processed inside a for /f command

  • The first line in the ping output includes the ip address enclosed in square brackets

  • The for /f tokenizes the line using the square brackets as delimiters, and retrieve the second token

                         v       v          (delimiters)
    Pinging computername [x.x.x.x] with 32 bytes of data
    1                     2       3             (tokens)

Upvotes: 4

Related Questions