-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathon_pinger.java
More file actions
89 lines (84 loc) · 2.78 KB
/
Copy pathon_pinger.java
File metadata and controls
89 lines (84 loc) · 2.78 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.*;
import java.util.Calendar;
import java.text.SimpleDateFormat;
public class on_pinger
{
public static String now(String dateFormat)
{
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
return sdf.format(cal.getTime());
}
public static void cmdexec(int count, String ipaddr, PrintStream p)
{
try
{
Process p1 = Runtime.getRuntime().exec ("date");
Process p2 = Runtime.getRuntime().exec ("ping -c 1 " + ipaddr);
try
{
Thread.sleep(500L);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
BufferedReader in1 = new BufferedReader (new InputStreamReader(p1.getInputStream()));
BufferedReader in2 = new BufferedReader (new InputStreamReader(p2.getInputStream()));
String s1 = in1.readLine();
String s2 = in2.readLine();
s2 = in2.readLine();
String s3 = count + " " + s1 + " PING TIMED OUT: " + ipaddr;
String s4 = count + " " + s1 + " " + s2;
if (s2.length() < 3)
{
System.out.println(s3);
p.println(s3);
}
else
System.out.println(s4);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main (String args[])
{
FileOutputStream fout;
PrintStream p;
int looper = -1;
int counter = 1;
if (args.length < 1)
{
System.err.println("\nApplication Error:");
System.err.println("Usage: java on_pinger ipadd1 ipadd2 .... \n\n");
System.exit(-1);
}
try
{
String opn = "ON-PINGER-" + now("yyyyMMdd") + "-" +now("HHmmss-aaa") + ".OUT";
fout = new FileOutputStream (opn);
p = new PrintStream (fout);
System.out.println("Output Filename: " + opn);
p.println("--------------------------------------------------------------------------");
p.println("This log file was created on " + now("MMMMM dd, yyyy GGG hh:mm aaa"));
p.println("--------------------------------------------------------------------------");
while (looper < 0)
{
for(int i = 0; i < args.length; i++)
{
cmdexec(counter, args[i], p);
}
counter++;
}
p.close();
fout.close();
}
catch (IOException e)
{
System.err.println ("Unable to write file" );
System.exit(-1);
}
}
}