How to check if IP exists with bat

Asked

Viewed 2,076 times

9

I can drip all the network addresses with something similar to this:

for /l %%x in (1, 1, 100) do ping 10.1.1.%%x

However, not all addresses are valid, and in that case, he of the timeout. Is there any way I can check if the IP exists, only then drip it?

Explaining in more detail, follows an example of what I wanted to do more or less (in an example language)

for(int i = 1; i < 256; i++)
{
    var ip = formataip(i);
    if(ip_existe(ip))
       ping_ip(ip);
}
  • I think the easiest way maybe would be to filter out the timeouts and only display the ones that respond to a first ping.

  • If it’s online. If it’s not Timeout ping.

  • @Embarrassed because it is, as I was researching, I was realizing it too, so I was just leaving the question a longer time with no accepted answer to see if there was some obscure way kkk

4 answers

10


dark way kkk

I don’t know how obscure this is, but everything indicates that by RFC, packages TCP ACK unsolicited should be answered with TCP RST (connection refused), which can be "abused" (and is) by programs that scan the network, known as SYN Scans.

They start a handshake but do not end - only analyze the response of host.

There are several of these, one of them is from Microsoft itself, and is called psping.

Using the following . bat:

@echo off
for /L %%a in (1,1,255) do (
   psping -n 2 192.168.10.%%a:3389
)

Note the use of the remote desktop, 3389 - I got reply from hosts windows and mac that had the service enabled and disabled.

TCP connect to 192.168.20.26:3389:
3 iterations (warmup 1) ping test:
Connecting to 192.168.20.26:3389 (warmup): from 0.0.0.0:63028:
The remote computer refused the network connection.

In this case, if the computer is refusing the connection, we can assume that it exists... xD

And in the case of this old friend of mine, who blocks ICMP:

Pinging 192.168.10.200 with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Request timed out.

He didn’t expect this:

PsPing v2.10 - PsPing - ping, latency, bandwidth measurement utility
Copyright (C) 2012-2016 Mark Russinovich
Sysinternals - www.sysinternals.com
TCP connect to 192.168.10.200:3389:
11 iterations (warmup 1) ping test:
Connecting to 192.168.10.200:3389 (warmup): from 192.168.10.192:63049: 0.64ms
Connecting to 192.168.10.200:3389: from 192.168.10.192:63050: 0.47ms
Connecting to 192.168.10.200:3389: from 192.168.10.192:63051: 0.54ms
Connecting to 192.168.10.200:3389: from 192.168.10.192:63052: 0.53ms
Connecting to 192.168.10.200:3389: from 192.168.10.192:63053: 0.54ms
Connecting to 192.168.10.200:3389: from 192.168.10.192:63054: 0.53ms

This technique will not always work, obviously - and it is possible to test other ports (I, particularly not had luck with them), but they say that SYN Scans has... ;)

Sources:
ping Alternative for tcp?
Netcat Power Tools

  • 1

    Interesting! So the host may be online, but still not accept my connection! I think I’ll need this too, thank you very much!

  • @The difference is that psping implements a ping for TCP and the ping program implements for ICMP. And the host/server can be configured to do anything, such as ignore ICMP or TCP communication, ignore certain ports or refuse certain ports.

8

A suggestion is to perform the command ping and search for a string in the answer.

For example:

When the ping is successfully executed it is returned something like Resposta de 10.1.1.1: bytes=32 tempo<1ms TTL=255 and when there is error something like Esgotado o tempo limite do pedido. and etc...

In this case, if the response string contains something like TTL= means that the ping was successfully executed. Thus we can use the code below to analyze the return in the command:

@echo off
set "host=10.1.1"
setlocal
for /L %%I in (1,1,10) do (
    ping -n 1 -w 1000 %host%.%%I | find /i "TTL=" >NUL && (
        echo %host%.%%I: ONLINE
    ) || (
        echo %host%.%%I OFFLINE
    )
)
pause

In the above code the number of requests was limited to only one with ping -n 1 and the waiting time limit for each response 1000ms with ping -w 1000. To learn more type ping /?.

  • It just doesn’t work when the server is blocked for Ping. But I think that’s too much of a hassle on my part

  • @Jeffersonquesado Yes, if it is blocked for ping will not really work and this you will not be able to solve with a batch code but with the administrator of the network/ server :)

3

It is not the solution to your problem, but it can help you.

There is a program called Nmap, it serves to scan the network and several other functions related to ips scanning...

Follow the website link: https://nmap.org/download.html#windows

Installer: https://nmap.org/dist/nmap-7.40-setup.exe

With the command below you get all the hosts that responded:

nmap -sP 192.168.1.1/24 | find /i "scan report"

You can output this command to a file and then loop it through the records.

Example of output of command:

Nmap scan report for 192.168.1.38
Nmap scan report for 192.168.1.41
Nmap scan report for 192.168.1.45
Nmap scan report for 192.168.1.62
Nmap scan report for 192.168.1.67
Nmap scan report for 192.168.1.68
Nmap scan report for 192.168.1.87
Nmap scan report for 192.168.1.90
Nmap scan report for 192.168.1.92
Nmap scan report for 192.168.1.95
Nmap scan report for 192.168.1.96
Nmap scan report for 192.168.1.97
Nmap scan report for 192.168.1.99
Nmap scan report for 192.168.1.102
Nmap scan report for 192.168.1.104
Nmap scan report for 192.168.1.110
Nmap scan report for 192.168.1.111

2

Unfortunately, is not possible do this in a common network.

There’s nothing on the network that will tell you which Ips exist. But even if it existed, to know if the address is online you would need to make the least possible communication with the machine and wait for the answer to know if it is online.

This minor communication is the ping.

Now if what you need is to run multiple "pings" faster, one thing you can do is create an application that runs multiple simultaneous pings and returns to you only those that are successful. Take an example: https://stackoverflow.com/q/13492134/460775

When we are dealing with infrastructure, there are tools for this. A widely used is the Nmap.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.