3

I have looked around and cant seem to find an in house windows version independent solution to getting the ip address of a computer in a batch file. What I would like to do is, no matter what windows machine I am on (whether its running win 7 or XP or maybe even 98) I would like to be able to figure out the ip address and store it into a variable in an easy fashion.

I can use ipconfig and parse out the IPv4 address but windows 7 outputs something slightly different than earlier versions so I would first have to figure out what version of windows they have and then look for the appropriate string. Any help would be great!

4
  • stackoverflow.com/questions/5898763/… Commented May 29, 2013 at 13:59
  • You will probably have to detect the OS and do something different for each... Windows doesn't have grep unless you want to install a version of it. Commented May 29, 2013 at 14:00
  • I was afraid of that since I have not found any solutions like I want. I saw the question you posted and thought of just doing that and getting the windows version but just thought I would see if there is an alternative that is easier. Commented May 29, 2013 at 14:02
  • I don't think so.. you'll do it once and forget about it Commented May 29, 2013 at 14:08

4 Answers 4

7

XP Pro / Vista / 7 / 8:

For Windows XP and newer I would recommend using WMIC.

@echo off for /f "skip=1 delims={}, " %%A in ('wmic nicconfig get ipaddress') do for /f "tokens=1" %%B in ("%%~A") do set "IP=%%~B" echo %IP% 

98 / 2000 / XP Home:

@echo off for /f "tokens=2* delims=:" %%A in ('ipconfig /all ^| find "IP Address"') do for /f "tokens=1" %%B in ("%%~A") do set "IP=%%~B" for /f "tokens=2* delims=:" %%A in ('ipconfig /all ^| find "IPv4 Address"') do for /f "tokens=1" %%B in ("%%~A") do set "IP=%%~B" echo %IP% 

Other Commands

netsh interface ip show addresses

nbtstat -n | find "IpAddress:"

Sign up to request clarification or add additional context in comments.

2 Comments

thanks! I guess I should just give up on a solution that works across all versions of windows.
You should be able to use the ipconfig method on all versions of Windows. Notice how I search for two different strings ("IP" for 98, "IPv4" for 7). I have not checked all versions of windows, but you could just add as many searches as there are different strings. But unfortunately, there is not one command that works for all the legacy versions.
6

I guess this would do it:

@echo off FOR /F "tokens=2,3" %%A IN ('ping %computername% -n 1 -4') DO IF "from"== "%%A" set "IP=%%~B" echo %IP:~0,-1% 

2 Comments

Sorry, should of specified I wanted the IPv4 address. I just tried this and it gave me the IPv6 address on windows 7
@user972276, how about now?
1

Get your real internet IP Windows version independent with GNU wget

@echo off&setlocal for /f %%i in ('wget ident.me --output-document=- 2^>nul') do set "myRealIP=%%i" if defined myRealIP (echo Your real IP is stored in %myRealIP%) else echo Error! No connection to the internet. 

6 Comments

I tried this on Windows 7 and it just prints "Error! No connection to the internet."
Sorry, in this case you don't have connection to ident.me. This is the case in some Arab states. :(
I just went to ident.me from a web browser and it gave me an incorrect IP address. So I can access that website. I would rather my solution not depend on anything other than what comes standard with windows.
ident.me gives you the IP adress, the internet can see from you. If you use proxies or VPN the answer may vary. But for the net this is you real IP :)
Yeah, I have a VPN so that would def do it. I am trying to get the local IP I guess.
|
1

This works fine with windows 10

@echo off for /f "skip=1 delims={}, " %%A in ('wmic nicconfig get ipaddress') do for /f "tokens=1" %%B in ("%%~A") do set "IP=%%~B" for /f "tokens=1 delims=:" %%j in ('ping %computername% -4 -n 1 ^| findstr Reply') do ( set localip=%%j ) echo Public IP is: %IP% echo Local IP is: %localip:~11% 

Returns both public and private IP

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.