0

I have been learning Batch Scripting on my own and I can't seem to get one to work.

I want a .bat that pings all IP Addresses on a local network, then prints all the active IPs to a .txt file.

@ECHO OFF
REM This script pings all IPAdresses on an Xfinity Router. 
::v1.0 - BTE - 24FEB19
::
FOR /L %i (1,1,254) DO
ping -n 1 10.0.0.%i 

This is what I have so far, but I don't know how to get the pingable IPs to print to a .txt.

Any help is appreciated!

B.

BEdwards
  • 5
  • 1
  • 5
  • 2
    `For` needs to be on one line. – Noodles Feb 26 '19 at 01:41
  • 2
    Or at the very least, you need a `(` at the end of the `for` line. – SomethingDark Feb 26 '19 at 03:44
  • You need to understand what the term batch file means. It is batches of commands that is executed one after the other. So that said. Your for line ends with do.. Do what? :). So Double your `%` and move the ping line to after do. I.e. `FOR /L %℅i (1,1,254) DO ping -n 1 10.0.0.%℅i` – Gerhard Feb 26 '19 at 05:17
  • Type `for /?` into a Command Prompt window and read the help message; one of the first sentences provides the solution. Anyway, are you sure you want to try to ping one IP after another? if there are a lot of unresponsive ones, you'll have to wait for quite some time. So perhaps take a look at these posts: [Improving Batch File for loop with start subcommand](https://stackoverflow.com/a/40964527), [Arrange the pinging of multiple website in order with batch?](https://stackoverflow.com/a/50840684)... – aschipfl Feb 26 '19 at 13:28

1 Answers1

1

Try this:

::@ECHO OFF
REM This script pings all IPAdresses on an Xfinity Router. 
::v1.0 - BTE - 24FEB19
::
FOR /L %%i in (1,1,254) DO ping -n 1 10.0.0.%%i | findstr "ms" && (echo 10.0.0.%%i)>>"pingable_ips.txt"
  • for variable in batch file should use %%. (On cmd prompt use single %)
  • The loop content needs to be inside a pair of parentheses, or in the same line of for.
  • You need in after the for variable.
  • And when you are testing, don't use @echo off yet.

Here I used the ms in the reply message as the signal, you can change it according to your situation.

You can put loop inside a pair of parentheses too:

FOR /L %%i in (1,1,254) do (
    ping -w 77 -n 1 10.0.0.%%i | findstr "ms" && (echo 10.0.0.%%i)>>"pingable_ips.txt"
)
pause

So you can add other things inside the loop (inside the parens).
Added the -w timeout switch, to speed it up, for local ips are quick, you can increase the time.
Check for /?, ping /?, and findstr /? for more.

Til
  • 5,150
  • 13
  • 26
  • 34
  • The .bat ran, but it didnt create the .txt. Do you know what could be wrong? – BEdwards Feb 27 '19 at 02:23
  • @BEdwards Could be folder permission, or no local ip pingable(maybe firewall/router/switch blocked icmp packet). You can remove `echo off`, open `cmd` first, and run the batch inside the `cmd` window, then you can check the commands log, see if you can find some trace in it. – Til Feb 27 '19 at 02:57
  • I ran it from cmd and I can see it pinging all IP addresses, but when it concludes, it doesn't create the txt. I am running it from my desktop, should I put it in a folder? – BEdwards Feb 27 '19 at 22:26
  • @BEdwards Can you ping local ips manually? Try to write a batch file place at same location, and one just to ping ips, and just to `>>` to a file, see if they're working or not. – Til Feb 28 '19 at 02:26
  • Does the txt file need to already exist? – BEdwards Feb 28 '19 at 16:38
  • @BEdwards Not needed. – Til Feb 28 '19 at 16:51