How to get a windows ip address, via code - windows

I need the ppp IP address stored into a variable.
The only way I have found is 'ipconfig > text.txt' into a tmp file and then script a search for the ppp interface. This is very dirty.
Is there an better alternative in windows?

setlocal enabledelayedexpansion
for /f "delims=" %%a in ('ipconfig ^| find /i "IPv4 Address"') do (set VarIP=!%%a%!)
Echo Your PPP IP : %VarIP%

#echo off
for /f "tokens=2,3 delims={,}" %%a in ('"WMIC NICConfig where IPEnabled="True" get IPAddress /value | find "I" "') do echo IPv4 %%~a IPV6 %%~b
pause

You can also extract using ping:
#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%

Related

Issue with bat script checking cellular connection status and then displaying IP if connected or displaying not connected if not on cellular

I have been tinkering with this for a while and I am unable to make this fully work in a script. If I run commands separately they work as expected...any help would be greatly appreciated. Thank you in advance!
cls
#echo off
set celValue=
for /f "tokens=3 delims=: " %%a in ('netsh mbn show connection interface^="Cellular"^| findstr
"Interface"') do set celValue=%%a
if "%celValue%"=="Connected" (for /f "tokens=3 delims=: " %%i in ('netsh interface ip show config
name^="Cellular"^| findstr "IP Address"') do echo Cellular IP: %%i) else (echo Cellular not
connected)
pause
I am not able to test this as I have no broadband connections on this device. So you'd need to do the testing for us. So besides the esacpaing issues shown in a comment, your parenthesized code blocks had the incorrect formatting.
cls
#echo off
set celValue=
for /f "tokens=3 delims=: " %%a in ('netsh mbn show connection interface^="Cellular" ^| findstr /I "Interface"') do set celValue=%%a
if "%celValue%"=="Connected" (
for /f "tokens=3 delims=: " %%i in ('netsh interface ip show config name^="Cellular" ^| findstr /I "IP Address"') do (
echo Cellular IP: %%i
)
) else (
echo Cellular not connected
)
pause

Adding names to an IP address without failing the batch file

I want to use a batch file to ping a set of servers. The batch file works and reports ok or failed. All I want is to have it display a name of the server in the check.
This is what I use in the batch file:
#echo off
for /f "delims=" %%a in (C:\List of IPs.txt) do ping -n 1 %%a >nul && (echo %%a ok) || (echo %%a failed to respond)
pause
In the text file it points to is just a list of IPs, how do I make it so I can see a name next to the IP?
Thank you in advance!
just insert another for /f loop to get the name:
#echo off
SetLocal
for /f "usebackq" %%a in ("List of IPs.txt") do (
for /f %%b in ('"ping -n 1 %%a >nul && (echo ok) || (echo failed to respond) "') do (
for /f "tokens=1*" %%m in ('nslookup %%a ^|findstr /b "Name:"') do (
echo %%a %%n %%b
)
)
)
One option is to format your file to have the IP and the name you are interested in.
192.168.1.200 server1
192.168.1.201 server2
192.168.1.202 printer1
192.168.1.203 that one stupid printer in the back
Then you can read both from the file.
One thing I'll note -- your method of checking pings is not consistent. You can get an errorlevel 0 even if the ping actually fails (from a valid response of "unreachable"). Instead (in English) a successful ping always includes "ttl=". Thus you would do this:
untested
#echo off
for /f "usebackq tokens=1* delims= " %%a in ("C:\List of IPs.txt") do ping -n 1 %%a ^|find /i "ttl=" >nul && (echo %%b at %%a is ok) || (echo %%b at %%a failed to respond)
pause
nslookup and/or ping -a can be leveraged if you have dns working with everything you're trying to access.
Based on comment, I've added the following solution that works on my English Windows 10 computer with the text file above:
#echo off
for /f "usebackq tokens=1* delims= " %%a in ("C:\temp\List of IPs.txt") do ping -n 1 -w 1500 %%a |find /i "ttl=" >nul &&echo %%b at %%a is ok||echo %%b at %%a failed to respond
pause
Note there is a space between the equal sign and the double quote at delims= "
Output:
server1 at 192.168.1.200 is ok
server2 at 192.168.1.201 is ok
printer1 at 192.168.1.202 is ok
that one stupid printer in the back at 192.168.1.203 failed to respond
Press any key to continue . . .

windows cmd to get default gateway address [duplicate]

I want to return the Default Gateway like i have for the IPv4 but it returns Blank.
for /f "tokens=1-2 delims=:" %%a in ('ipconfig^|find "IPv4"') do set ip=%%b
set ip=%ip:~1%
echo.
echo IP Address is: %ip%
echo.
This is what i have so far but it returns blank, i have tried to alter it but it still returns a blank answer.
for /f "tokens=1-2 delims=:" %%a in ('ipconfig^|find "Default"') do set ip=%%b
set ip=%ip:~2%
echo.
echo The Gateway is: %ip%
echo.
Any ideas how i can do this. and return the default gateway for a given Computer.
Here's a WMIC script:
#echo off
for /f "tokens=2,3 delims={,}" %%a in ('"WMIC NICConfig where IPEnabled="True" get DefaultIPGateway /value | find "I" "') do echo IPv4 %%~a IPV6 %%~b
pause
set "ip="
for /f "tokens=1-2 delims=:" %%a in ('ipconfig^|find "Default"') do if not defined ip set ip=%%b
Take a look at the listing from ipconfig - it's likely that there is more than one default line. You would therefore get the data from the last line containing default
The above construct returns the first line containing default.
This worked for me.
Got first listed gateway.
#echo off
set "ip="
for /f "tokens=2,3 delims={,}" %%a in ('"WMIC NICConfig where IPEnabled="True" get DefaultIPGateway /value | find "I" "') do if not defined ip set ip=%%~a
echo IP Mobile Address is: %ip%
pause
for /f "tokens=1-5" %%a in ('route -4 print 0.*'
) do #if "%%e"=="" if "%%a"=="%%b" set "ip=%%c"
This will extract the default gateway from the ipv4 route table.

Windows Batch - How to get the external IP into a batch-file variable

I am making a program that checks if a user's IP is a certain IP address.
Currently, I created a successful internal IP version:
#echo off
set userIp=192.168.90.100
for /f "tokens=4 delims= " %%i in ('route print ^| find " 0.0.0.0"') do set localIp=%%i
for /f "delims=[] tokens=2" %%a in ('ping %computername% -4 -n 1 ^| findstr "["') do set thisip=%%a
goto :Check
:Check
if %localIp%==%userIp% goto :Good
if %thisip%==%userIp% goto :Good
goto :Bad
And I am trying to make the same thing that works with external IPs.
I researched online, and here is what I got so far.
#echo off
for /f "tokens=2 delims=:" %%a IN ('nslookup myip.opendns.com. resolver1.opendns.com ^| findstr /IC:"Address"') do if /i %%a=="10.11.12.13" goto :Good
goto :Bad
I need a bit of help on how to fix this.
With pure batch/already present tools:
EDIT: changed the batch to properly handle also IPv6 addresses
#Echo off
for /f "tokens=1* delims=: " %%A in (
'nslookup myip.opendns.com. resolver1.opendns.com 2^>NUL^|find "Address:"'
) Do set ExtIP=%%B
Echo External IP is : %ExtIP%
Reference
Another one with powershell:
#Echo off
For /f %%A in (
'powershell -command "(Invoke-Webrequest "http://api.ipify.org").content"'
) Do Set ExtIP=%%A
Echo External IP is : %ExtIP%
And another slightly different powershell variant:
#Echo off
For /f %%A in (
'powershell -nop -c "(Invoke-RestMethod http://ipinfo.io/json).IP"'
) Do Set ExtIP=%%A
Echo External IP is : %ExtIP%
To get your public IP without additional parsing do this:
curl "http://api.ipify.org"
EDIT:
This version is more reliable across windows language versions:
for /f "tokens=3 delims== " %%A in ('
nslookup -debug myip.opendns.com. resolver1.opendns.com 2^>NUL^|findstr /C:"internet address"
') do set "ext_ip=%%A"
First it seems there is a . too much after the first .com.
Second when using your command with simply google.com and echo %a I get the following:
" xx.xx.xx.x" without the quotes and with two leading spaces!
So your if will never be true!
Change it to something like this: if "%%a"==" xx.xx.xx.x" Goto:good and you should be fine.

Store IP address in variable - windows version independent

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!
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:"
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%
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.
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

Resources