0

I posted this a few days ago, but I am still having some issues. Please help if possible. Here is my code

@echo off
rem This script pings all IPAdresses on an Xfinity Router. 
::v1.2 - BTE - 01Mar19
::
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"

The above was a batch file that I tried to create to ping all IP addresses on my network, then write all pingable IPs to a text file. After some troubleshooting from others here, I got it to at least ping all IPs on the network. I am still having trouble with getting the batch file to create a text file with the pingable IPs.

This batch file runs all the way from 10.0.0.1 to 10.0.0.254, but it stops after.

EDIT: Thank You, I got it to work!

BEdwards
  • 5
  • 1
  • 5
  • 1
    You don't have any code after the FOR command completes. Why wouldn't the code stop? – Squashman Mar 02 '19 at 00:52
  • 1
    I am unable to reproduce your issue. I ran your code on my own network and received a text file of every IP address that I was using. If you are expecting more IP addresses beyond the ones that start with `10.0.0.`, you will have to add another loop. – SomethingDark Mar 02 '19 at 00:53
  • Try two things: 1. Insert a line `cd /d "%~dp0"` at the beginning of the batch file. 2. Run as administrator. – Til Mar 02 '19 at 02:14
  • Oh, and add a `pause` line after the for line. Comment out `@ECHO OFF` like this: `::@ECHO OFF`, and copy / paste the things around those pingable ips here so we can see what's wrong. – Til Mar 02 '19 at 02:23
  • 1
    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 Mar 02 '19 at 13:23

1 Answers1

0

While I don't know what went wrong with your code, I would suggest the following one which shouldn't produce any errors/misbehaviours except if this is your fault:

@echo off
rem This script pings all IPAdresses on an Xfinity Router. 
rem v1.2 - BTE - 01Mar19

for /L %%A IN (1 1 254) do (
    (ping -n 1 10.0.0.%%A | findstr "ms") && (
        echo 10.0.0.%%A>>"pingable_ips.txt"
    ) || (
        echo Failed to find string "ms" when pinging 10.0.0.%%A!
    )
)

Or in one line:

@echo off
rem This script pings all IPAdresses on an Xfinity Router. 
rem v1.2 - BTE - 01Mar19

for /L %%A IN (1 1 254) do (ping -n 1 10.0.0.%%A | findstr "ms") && (echo 10.0.0.%%A>>"pingable_ips.txt") || (echo Failed to find string "ms" when pinging 10.0.0.%%A!)

Actually, my code checks if command ping -n 1 10.0.0.%%A | findstr "ms" returns errorlevel EQUal to 0. If yes, then it runs command echo 10.0.0.%%A>>"pingable_ips.txt". If no, it will run echo Failed to find string "ms" when pinging 10.0.0.%%A!.

double-beep
  • 5,031
  • 17
  • 33
  • 41