I want that Skype only starts when I am connected on a specific LAN IP address.
#echo off
set ip_address_string="IPv4 Address"
for /f "usebackq tokens=2 delims=:" %%f in (`ipconfig ^| findstr /c:%ip_address_string%`) do echo bounded IP-address: %%f
But how can I realize that Skype only will be called if any ip address contains 64.2.4.*?
Moreover, the batch file will run every 5 minutes and if it is required the Skype-process should be killed.
#echo off
set "ip_address_string=64.2.4."
ipconfig | findstr /c:"%ip_address_string%" >nul|| (
echo IP "%ip_address_string%" not found.
taskkill /im skype.exe /f
exit /b 0
)
echo connected as/to "%ip_address_string%"
?
Related
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 . . .
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'm trying to write a batch file, that gives me an IP back, but I just know the MAC address
arp -a
wont work for me, because I never pinged that IP before. I want to search for it in the network with literally just knowing the MAC address.
Information: The IP is static.
Assuming, your subnet is 192.168.1.xxx
#echo off
REM clear arp cache (optional):
arp -d
REM ask everybody on the subnet for a response:
for /l %%a in (1,1,255) do start /b ping -n 2 -w 500 192.168.1.%%a >nul
REM wait for the processes to finish:
timeout 2 >nul
REM show the responses (with IP and MAC):
arp -a
When you found the IP, you can try to resolve the computer name with:
ping -a -n 1 192.168.1.xxx
Edit found a way to speed it up.
Edit : just realized you where on Windows. Sadly, there is no native way to ping on braodcast addresses with Windows, but you can use the trick from this answer : FOR /L %i in (1,1,255) do #ping -n 1 192.168.1.%i | find "Reply". It seems super slow though.
Previous answer is below.
As far as I know, you have to fill in your ARP cache to do what you want.
This answer is what you need.
As the post says, you can :
ping all hosts of your LAN by pinging your broadcast address (ping -b -c1 192.168.1.255, replace the IP by your actual broadcast address)
fping your whole network (fping -a -g 192.168.1.0/24, replace with your actual netmask)
nmap the network (nmap -sP 192.168.1.0/24)
nbtscan (windows hosts only, nbtscan 192.168.1.0/24)
And then look up in your ARP cache.
for /f "skip=3 delims=\" %%A in ('net view ^| findstr /v /C:"The command completed successfully"') do Echo %%A >> "%userprofile%\desktop\computerlist.txt"
Lists turned on computers.
wmic /node:#computerlist.txt nicconfig where macaddress='whatever:whatever:etc' get ipaddress /format:htable
To get IP for a specified MACAddress
wmic /node:#computerlist.txt nicconfig get ipaddress /format:htable
to get the IPAddress for all MACAddresses on a computer.
I remember that sachadee posted this code ScanIP-MAC.bat
#echo off
Title Scan for IP and MAC Adress on LAN
mode con cols=60 lines=20
Color A
set ip=192.168.1.1
set debut=1
set fin=10
if exist ping.txt (del ping.txt)
if exist ping2.txt (del ping2.txt)
if exist ping3.txt (del ping3.txt)
if exist ping4.txt (del ping4.txt)
for /L %%i in (%debut%,1,%fin%) do (echo Recherche de la machine : %ip:~0,9%.%%i
for /f "tokens=5 delims= " %%f in ('ping -4 -n 1 %ip:~0,9%.%%i ^|find /i "32"') do (echo %%f >> ping.txt
)
for /f "tokens=1 delims= " %%k in ('Type ping.txt ^|findstr /i "19"') do echo %%k > ping2.txt
for /f %%l in (ping2.txt) do (arp -a %%l >> ping3.txt
)
)
Cmd /U /C Type ping3.txt > ping4.txt
Del ping.txt
Del ping2.txt
Del ping3.txt
Start ping4.txt
I am not a batch user so i dont know the commands but i know the way to do this is sending broadcast ping request to ur network and check for the mac address from the reply's using wireshark.
I'm trying to write a batch file that tests network connectivity by pinging the default gateway of the given network.
However, as I want this to be an automated process which then logs the results to a text file, I'd like to be able to ping the gateway on various networks, without having to change/enter the ip address.
Is there a generic term or command to ping the default gateway for the network you're currently connected to?
(I already have the commands for output options etc.)
So far, I have this....
#echo off
goto :NETWORK1
:NETWORK1
ipconfig
echo .
Set /P gateway=PLEASE ENTER GATEWAY IP ADDRESS (shown above):
if /I "%gateway%" EQU "exit" goto :EXIT
if /I not "%gateway%" EQU "exit" goto :NETWORK2
:NETWORK2
echo CLOSING THIS WINDOW WILL ABORT THE CONNECTIVITY TEST.
echo . >> "C:\Network Test Results %date:/=.%.txt"
echo Time: >> "C:\Network Test Results %date:/=.%.txt"
time /t >> "C:\Network Test Results %date:/=.%.txt"
ping %gateway% -n 20 >> "C:\Network Test Results %date:/=.%.txt"
goto :NETWORK2
:EXIT
exit
But I don't want to have to specify the IP address, so that I can take away the need for user input.
smalll fix for Wernfried Domscheit
for /f "tokens=2 delims=:" %%g in ('netsh interface ip show address ^| findstr /c:"Default Gateway"') do ping %%g
pause
just adding /c: before "Default Gateway"
On a command line you can try this one:
for /f "tokens=2 delims=:" %g in ('netsh interface ip show address ^| findstr "Default Gateway"') do ping %g
Note, inside a batch-file you must double the %, i.e.
for /f "tokens=2 delims=:" %%g in ('netsh interface ip show address ^| findstr "Default Gateway"') do ping %%g
According to jimbobomcgee on serverfault who phrased the correct answer for the question How to Extract command-line output into a Variable?
for /f "usebackq tokens=1,2,3 delims=:" %A in (`ipconfig ^| Find "Default Gateway" ^| Findstr/N "." ^| Findstr/B "1:"`) do #if not defined MYVAR set MYVAR=%~C
#echo off
setlocal enableextensions disabledelayedexpansion
set "gateway="
for /f "tokens=1-5" %%a in ('route -4 print 0.*') do #if "%%e"=="" if "%%a"=="%%b" set "gateway=%%c"
if not defined gateway goto :eof
echo CLOSING THIS WINDOW WILL ABORT THE CONNECTIVITY TEST.
:loop
echo %date% %time%
>> "Network Test Results %date:/=.%.txt" (
echo(
echo(Time: %time%
ping -n 20 -4 %gateway%
)
goto :loop
Gateway determination from route information copied from here
edited for a more tolerant parse of the route command use
for /f "tokens=3" %%a in ('route -4 print 0.* ^| find "0."') do set "gateway=%%a"
After help from #MC ND, I have created the following code, which does exactly what I needed....
pings the default gateway with the results being output to a text file
shows a message telling the user not to close the command window
#echo off
setlocal enableextensions disabledelayedexpansion
for /f "tokens=3" %%a in ('route -4 print 0.*') do set "gateway=%%a"
:NETWORK
echo CLOSING THIS WINDOW WILL ABORT THE CONNECTIVITY TEST.
echo Time >> "C:\Network Test Results %date:/=.%.txt"
time /t >> "C:\Network Test Results %date:/=.%.txt"
echo . >> "C:\Network Test Results %date:/=.%.txt"
ping %gateway% -n 20 >> "C:\Network Test Results %date:/=.%.txt"
goto :NETWORK
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