How to get IP only?

May 24, 2010 at 05:39:22
Specs: Windows XP
How to get IP?

If you want see ur IP, you can type ipconfig in cmd.
Yeah that's easy.
But for who want only know the IP address or defaultgateway and not the whole standard output, check this out.

CMD Line - IP in variable & output.
for /f "tokens=15" %j in ('ipconfig') do set IP=%j & msg * %IP%
This will set your "IP" in variable IP, and will send only the IP as a message.

Batch Script - IP variable & output.
for /f "tokens=15" %%j in ('ipconfig') do set IP=%%j & msg * %IP%
You will need two '%%' signs in a batch script, otherwise it wont work.

TOKENS
Token 11 = Defaultgateway
Token 13 = Subnetmask
Token 15 = IP address

Example of a Batch Script

for /f "tokens=15" %%j in ('ipconfig') do set IP=%%j
if %IP:~0,3%==10. goto LABEL

etc..

I hope this information is helpful.

Kind regards,

Muhammed Us\MUs
"Batch Hobbyist"


See More: How to get IP only?

Report •

#1
May 24, 2010 at 08:34:23
well... not quite
====================================
@echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=2 delims=:" %%a in ('ipconfig ^| find "IP Address"') do (
set P=%%a
set P=!P: =!
)

echo.!P!

========================================
But, as Dr. McCoy would say, it's worse than that. It assumes there is only one adapter.


=====================================
Helping others achieve escape felicity

M2


Report •

#2
May 24, 2010 at 13:15:51
Hi M2,

Your script didnt work, I dont know why, I just put 'pause' @ the end.
But i just see, 'press any key to continue.'. .' and not ur VAR echo.

&

If you have more adapters and you save 'TOKEN 15' as a IP, it will
set the last IP, into the 'VAR'.
It looks like each adapter has his own "TOKEN 15".

Example:

- I got a hamachi adapter..that got a IP.
- My system got a IP.

I run 'ipconfig'

Output:

Hamachi adapter..etc
IP Address : . . . . . XXX XXX XXX XXX
(MORE STUFF HERE)

Ethernet adapter..etc
IP Address : . . . . . XXX XXX XXX XXX
(MORE STUFF HERE)- (MORE ADAPTERS, MORE IP'S)

Now if i run the command below in cmd, with more adapters:
for /f "tokens=15" %j in ('ipconfig') do set IP=%j

The cmd output:

C:\Documents and Settings\MUs>for /f "tokens=15" %j in ('ipconfig') do set IP=%j
C:\Documents and Settings\MUs>set IP=(Hamachi IP) XXX XXX XXX XXX
C:\Documents and Settings\MUs>set IP=(System IP) XXX XXX XXX XXX

It will set the last output line as a VAR.

But instead of setting into a VAR, I could echo it? , So iv still got all IP addresses from the adapters, without the other standard output.

Kind Regards,

Muhammed Us\MUs



Report •

#3
May 24, 2010 at 14:47:18
By the way, this is OS version-specific. On Windows Vista and Windows 7, it's not called "IP Address" - it's "IPv4 Address" (I know it says Windows XP on this post's details but thought it would be nice to have a batch file that works irrespective of what version OS you are running. However, on Vista, you can't even run IPCONFIG without being in an elevated command prompt.)

Report •

Related Solutions

#4
May 24, 2010 at 15:54:32
Hi klint,

LOL hoping for consistency among win versions.


=====================================
Helping others achieve escape felicity

M2


Report •

#5
May 25, 2010 at 00:50:11
Hi klint,

I know it's OS version specific, that's why i dont use " ^| find " , but "TOKEN 15".

Muhammed Us\MUs


Report •

Ask Question