Skip to content

Instantly share code, notes, and snippets.

@oznotes
Created November 6, 2023 12:29
Show Gist options
  • Save oznotes/4f46226916ccb56622c7f69ffb0b2053 to your computer and use it in GitHub Desktop.
Save oznotes/4f46226916ccb56622c7f69ffb0b2053 to your computer and use it in GitHub Desktop.
Batch Ping Command

Introduction

While working with an Orange Pi on a network to which I had limited access, traditional methods of network scanning such as arp -a and net view /all were not yielding the results I needed. These commands typically allow for the discovery of devices on a network by listing IP addresses and connected resources, but in my case, they were insufficient due to restricted access to the router.

In an environment without router access, discovering devices can be challenging. My goal was to locate the Orange Pi, which was proving difficult to identify amongst the numerous devices on the network.

Successful Approach

To overcome this challenge, I devised a method of systematically pinging the entire subnet to identify all active IP addresses. This process involves sending a ping request to each possible IP address within the subnet and recording the responses. This method can determine which IP addresses are active, indicating potential devices on the network.

Here is how I set up the command to accomplish this:

Command Breakdown

The command is broken down as follows:

for /L %i in (0,1,255) do ping -n 1 -w 250 192.168.1.%i>>ipaddress.txt

For Loop

  • for /L: This initiates a for loop with the /L parameter, which is used for looping over a range of numbers.
  • %i: This is the variable that changes value with each iteration of the loop.
  • (0,1,255): Defines the range for the loop. The first number 0 is the start value, the second number 1 is the step value, and 255 is the end value.

Ping Command

  • ping: A command-line utility used to test the reachability of a host on an IP network.
  • -n 1: The number of echo requests to send to each host ( 1 means only one request will be sent).
  • -w 250: The timeout interval in milliseconds to wait for each reply.

Target IP Address

  • 192.168.1.%i: The base IP address with the last octet being replaced by the variable %i. This results in the ping command targeting IP addresses from 192.168.1.0 to 192.168.1.255.
  • Before executing this command, it's crucial to confirm the network's IP address format. Most local networks use the 192.168.x.x format, but some may use the 10.x.x.x range. Select the appropriate format for the subnet you are scanning.

Output Redirection

  • >>ipaddress.txt: Appends the result of each ping command to the file ipaddress.txt. If the file does not exist, it will be created. If the file already exists, new content will be added to the end of the file without overwriting existing data.

Usage

To run this command, open the Command Prompt in Windows and enter the command directly. This command must be run with appropriate permissions to execute ping requests on the network.

If this command is to be used inside a batch file (*.bat), remember to double the percent signs ( %%i instead of %i) to escape them.

Command Reference

I found this method particularly helpful, and it was inspired by a discussion on SuperUser. For additional context and potential alternative methods, you can refer to the post: Windows command to display all IP addresses.

Final Notes

This approach proved effective for my needs, allowing me to find the Orange Pi without requiring access to the router's administration interface. It serves as a reminder that sometimes unconventional methods can provide solutions when traditional tools fail to deliver the necessary results.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment