-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpingIP.java
More file actions
73 lines (65 loc) · 1.79 KB
/
Copy pathpingIP.java
File metadata and controls
73 lines (65 loc) · 1.79 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
// Brian Huang
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
public class pingIP
{
public static void runPing(String command) //Ping user specified IP address
{
try
{
Process p = Runtime.getRuntime().exec(command); //accepts a command and returns according to command received.
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = "";
while ((str = input.readLine()) != null)
{
System.out.println(str);
}
} catch (Exception e){
e.printStackTrace();
}
}
public static void main(String[] args)
{
String yes;
Scanner scan = new Scanner(System.in);
do
{
String ip = null, contInput = "";
int numPing = 4;
boolean pingTillStop = false;
do
{
System.out.println("Please enter an IP address.");
ip = scan.nextLine();
} while (ip.trim().isEmpty());
System.out.println("Do you want to ping host continuously? Enter yes or no.");
contInput = scan.nextLine();
if (contInput.equalsIgnoreCase("yes"))
pingTillStop = true;
if (!pingTillStop)
{
System.out.println("How many times do you want to ping?");
try
{
numPing = scan.nextInt();
scan.nextLine();
}
catch (Exception e)
{
System.out.println(e + "\nIP will be pinged the default number: 4 times.");
}
runPing("ping " + ip + " -n " + numPing);
}
if (pingTillStop)
{
System.out.println("Pinging host continuously. Terminate to stop.");
runPing("ping " + ip + " -t");
}
System.out.println("Ping another address?");
yes = scan.nextLine();
} while (yes.equalsIgnoreCase("yes"));
System.out.println("Have a great day!");
scan.close();
}
}