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!

user972276
  • 2,973
  • 9
  • 33
  • 46
  • http://stackoverflow.com/questions/5898763/how-do-i-get-the-ip-address-into-a-batch-file-variable –  May 29 '13 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. –  May 29 '13 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. – user972276 May 29 '13 at 14:02
  • I don't think so.. you'll do it once and forget about it –  May 29 '13 at 14:08

4 Answers4

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:"

David Ruhmann
  • 11,064
  • 4
  • 37
  • 47
  • thanks! I guess I should just give up on a solution that works across all versions of windows. – user972276 May 29 '13 at 17:39
  • 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. – David Ruhmann May 29 '13 at 19:11
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%
Alex Filipovici
  • 31,789
  • 6
  • 54
  • 78
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.
Community
  • 1
  • 1
Endoro
  • 37,015
  • 8
  • 50
  • 63
  • I tried this on Windows 7 and it just prints "Error! No connection to the internet." – user972276 May 29 '13 at 17:30
  • Sorry, in this case you don't have connection to `ident.me`. This is the case in some Arab states. :( – Endoro May 29 '13 at 17:32
  • 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. – user972276 May 29 '13 at 17:36
  • `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 :) – Endoro May 29 '13 at 17:39
  • Yeah, I have a VPN so that would def do it. I am trying to get the local IP I guess. – user972276 May 29 '13 at 17:43
  • Local IP address is also a "real" IP address. So I think you should replace the word "real" with _"external"_ or _"public"_. – Sk8erPeter Jan 02 '16 at 15:00
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

Mahadev Gouda
  • 769
  • 11
  • 14