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.
Related
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
I'd like to create simple script to show wlan password on Windows10.
This will be well solution for users that's not familiar with cmd commands.
On Windows 7 it could be done using GUI, but not on newer OSes.
I stuck on line
for /f "tokens=*" %%j in ('netsh wlan show profile %ssid% key=clear ^| findstr "Key Content"') do set wlan_password=%%j
The variable wlan_password is always null. Even if i change set instruction to e.g. echo it shows that syntax is incorrect. I can't troubleshoot that.
Why the line above doesn't work, but the line:
for /f "tokens=*" %%i in ('netsh wlan show interfaces ^| findstr "Profile"') do set wlan_output=%%i
works well?
#echo off
set wlan_output=
set connected_ssid=
set ssid=
set wlan_password=
for /f "tokens=*" %%i in ('netsh wlan show interfaces ^| findstr "Profile"') do set wlan_output=%%i
for /f "tokens=2 delims=:" %%a in ("%wlan_output%") do set connected_ssid=%%a
call :TRIM %connected_ssid% connected_ssid
set ssid=%1
if "%ssid%"=="" set /p "ssid=Podaj nazwe sieci [%connected_ssid%]: " || set "ssid=%connected_ssid%"
if not "%ssid%"=="" (
for /f "tokens=*" %%j in ('netsh wlan show profile %ssid% key=clear ^| findstr "Key Content"') do set wlan_password=%%j
echo "Haslo do sieci %ssid%: %wlan_password%"
exit /b
)
else (
echo "Nie podano nazwy sieci. Nie mozna odczytac hasla"
exit /b
)
pause
exit /b
:TRIM
SET %2=%1
GOTO :EOF
Ugh - sorry for not spotting the real problem sooner: you have also to escape the = within the for command:
for /f "tokens=*" %%j in ('netsh wlan show profile %ssid% key^=clear ^| findstr /c:"Key Content"') do set wlan_password=%%j
set wlan_password
Note: use findstr /c:"Key Content" or find "Key Content", because findstr "Key Content" returns each line that contains Key OR Content (or both). (Not that it would make any difference in this special case, but without /c: it will bite you sooner or later)
To get the key only:
for /f "tokens=1,* delims=:" %%j in ('netsh wlan show profile %ssid% key^=clear ^| find "Key Content"') do set "wlan_password=%%k"
set "wlan_password=%wlan_password:~1%"
echo ---%wlan_password%---
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.
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%
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