-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPing.java
More file actions
87 lines (70 loc) · 2.73 KB
/
Ping.java
File metadata and controls
87 lines (70 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.lang.Process;
import java.lang.Runtime;
import java.io.IOException;
public class Ping {
public static void main(String[] args) {
StringBuilder ping_command = new StringBuilder();
StringBuilder output = new StringBuilder();
StringBuilder ip_output = new StringBuilder();
List<String> live_ip = new ArrayList<String>();
final String IP = new String("192.168.");
final String CMD = new String("ping ");
final String CMDARG = new String(" -n 1");
short NUM3 = 0;
short NUM4 = 0;
short NUM4_start = 0;
short NUM4_end = 255;
ping_command.append(CMD).append(IP);
System.out.println("Finding local IP's that can be talked to...\n");
Scanner scnr = new Scanner(System.in);
System.out.println("What value for the third set of bytes(192.168.???.xxx) (0-255): ");
short ans1 = scnr.nextShort();
scnr.nextLine();
System.out.println("Would you like to multiple values for the fourth set of bytes(192.168.xxx.???) (y/n): ");
String ans2 = scnr.nextLine();
if (ans1 >= 0 && ans1 <= 255) {
NUM3 = ans1;
}
if (ans2.equalsIgnoreCase("y")) {
System.out.println("Start Position: ");
short ans3 = scnr.nextShort();
System.out.println("End Position: ");
short ans4 = scnr.nextShort();
NUM4_start = ans3;
NUM4_end = ans4;
NUM4 = NUM4_start;
}
scnr.close();
ping_command.append(NUM3).append(".");
try {
Runtime r = Runtime.getRuntime();
Process p;
BufferedReader in;
while (NUM4 >= 0 && NUM4 <= NUM4_end) {
ping_command.append(NUM4).append(CMDARG);
p = r.exec(ping_command.toString());
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String inputLine;
while((inputLine = in.readLine()) != null) {
output.append(inputLine).append("\n");
}
if (output.toString().contains("time")) {
ip_output.delete(0, ip_output.length());
ip_output.append(IP + NUM4);
live_ip.add(ip_output.toString());
}
NUM4++;
ping_command.delete(17, ping_command.length());
output.delete(0, output.length());
}
} catch (IOException e) {
e.printStackTrace();
}
live_ip.forEach(System.out::println);
}
}