0% found this document useful (0 votes)
6 views

java scanning range of network ip addresses AI Overview

Code for scanning IP addresses in Java

Uploaded by

jdnwotc
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

java scanning range of network ip addresses AI Overview

Code for scanning IP addresses in Java

Uploaded by

jdnwotc
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

java scanning range of network ip addresses

Hackerrank solution

Github

Json

Eclipse

Search Results
AI Overview
AI Overview

Java

import java.io.IOException;
import java.net.InetAddress;

public class NetworkScanner {

public static void main(String[] args) {


String network = "192.168.1."; // Replace with your network address
int start = 1; // Starting IP
int end = 254; // Ending IP

for (int i = start; i <= end; i++) {


String ipAddress = network + i;
try {
InetAddress inet = InetAddress.getByName(ipAddress);
if (inet.isReachable(1000)) { // Timeout in milliseconds
System.out.println(ipAddress + " is reachable.");
}
} catch (IOException e) {
// Ignore unreachable hosts
}
}
}
}
Explanation:
 Import necessary classes:
 IOException for handling network errors.
 for representing IP addresses.
InetAddress
 Specify network and range:
 Replace "192.168.1." with your network address.
 Adjust start and end to define the range of IP addresses to scan.
 Iterate over IP addresses:
 The code constructs IP addresses by appending the current
number to the network address.
 It then uses InetAddress.getByName() to resolve the IP address.
 Check reachability:
 The isReachable() method sends an ICMP echo request (ping) to
the IP address with a specified timeout.
 If the host responds, it is considered reachable.
Important considerations:
 Firewall and network configuration:
Network firewalls can block ICMP packets, which can lead to false
negatives.
 Speed:
Pinging each host can take time. Consider using a multithreaded
approach for faster scanning.
 Alternative libraries:
For more advanced network scanning, consider using libraries like
jNetPcap or Nmap4j.

You might also like