Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the IP address into a batch-file variable?

Tags:

batch-file

ip

I have an odd question, not sure if its possible.

I'd like to write a script, and for example I'm going to use ipconfig as my command.

Now when you normally run this command theres a ton of output.

What I'd like to have is a script that would show only the IP address, for example.

echo Network Connection Test ipconfig <---This would run in the background echo Your IP Address is: (INSERT IP ADDRESS HERE) 

The output would be

Network Connection Test  Your IP Address is: 192.168.1.1 

Is this even possible?

like image 440
level42 Avatar asked May 05 '11 13:05

level42


People also ask

How do I find the IP address of a script?

If you're looking for a public IP address of the box, you could use the following: dig @ns1.google.com -t txt o-o.myaddr.l.google.com +short | tr -d \"

How do I find a variable IP address?

Right-click the network adapter and select the Properties option. Select the Internet Protocol Version 4 (TCP/IPv4) option. Click the Properties button. Select the Obtain an IP address automatically option.

How do I assign an IP address to a command line?

Type ipconfig /release at the Command Prompt window, press Enter, it will release the current IP configuration. Type ipconfig /renew at the Command Prompt window, wait for a while, the DHCP server will assign a new IP address for your computer.


2 Answers

The following code works on any locale of any platform since Windows XP and it looks for the network IP from a (more or less) random of your network cards. It will never take longer than a few milliseconds.

for /f "delims=[] tokens=2" %%a in ('ping -4 -n 1 %ComputerName% ^| findstr [') do set NetworkIP=%%a echo Network IP: %NetworkIP% 

The following one looks for your public IP instead and works on Windows 7 and newer machines.

for /f %%a in ('powershell Invoke-RestMethod api.ipify.org') do set PublicIP=%%a echo Public IP: %PublicIP%   

You can find detailed explanations of these commands on my blog.

like image 196
Fabio Iotti Avatar answered Oct 17 '22 06:10

Fabio Iotti


This will print the IP addresses in the output of ipconfig:

@echo off set ip_address_string="IPv4 Address" rem Uncomment the following line when using older versions of Windows without IPv6 support (by removing "rem") rem set ip_address_string="IP Address" echo Network Connection Test for /f "usebackq tokens=2 delims=:" %%f in (`ipconfig ^| findstr /c:%ip_address_string%`) do echo Your IP Address is: %%f 

To only print the first IP address, just add goto :eof (or another label to jump to instead of :eof) after the echo, or in a more readable form:

set ip_address_string="IPv4 Address" rem Uncomment the following line when using older versions of Windows without IPv6 support (by removing "rem") rem set ip_address_string="IP Address" for /f "usebackq tokens=2 delims=:" %%f in (`ipconfig ^| findstr /c:%ip_address_string%`) do (     echo Your IP Address is: %%f     goto :eof ) 

A more configurable way would be to actually parse the output of ipconfig /all a little bit, that way you can even specify the adapter whose IP address you want:

@echo off setlocal enabledelayedexpansion ::just a sample adapter here: set "adapter=Ethernet adapter VirtualBox Host-Only Network" set adapterfound=false echo Network Connection Test for /f "usebackq tokens=1-2 delims=:" %%f in (`ipconfig /all`) do (     set "item=%%f"     if /i "!item!"=="!adapter!" (         set adapterfound=true     ) else if not "!item!"=="!item:IP Address=!" if "!adapterfound!"=="true" (         echo Your IP Address is: %%g         set adapterfound=false     ) ) 
like image 38
mousio Avatar answered Oct 17 '22 08:10

mousio