-
-
Save hubacekjirka/ee95f4d8ad9ae1c9f53cd36b8eb9fea3 to your computer and use it in GitHub Desktop.
Java program to ping an IP address
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* execution example: pinger.jar "8.8.8.8" "C:\\tmp\\out.txt" | |
* | |
* Motivation: Guys in UnityMedia.de are not masters of stable internet connection. Before complaining to their help desk | |
* I needed some hard evidence of the connection's disruptions. | |
* | |
* Description: Invokes system's command line and executes endless ping command. Reads command line's output, parses it | |
* and outputs it into a pipe-delimited text file for analysis in tools such as Tableau. | |
*/ | |
package pinger; | |
import java.io.BufferedReader; | |
import java.io.BufferedWriter; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.OutputStreamWriter; | |
import java.text.SimpleDateFormat; | |
import java.util.ArrayList; | |
import java.util.Date; | |
import java.util.List; | |
public class Pinger { | |
public static final boolean DEBUG = true; | |
public static final int BUFFERSIZE = 60; | |
public static String filePath; | |
public static void main(String[] args) { | |
System.out.println("Pinger for Windows by Jiri Hubacek, forked from https://gist.github.com/madan712/4509039"); | |
if (args.length != 2) { | |
System.out.println("Argument error. Expected arguments: <ipaddress> <filepath>"); | |
System.exit(-1); | |
} | |
String ip = args[0]; | |
filePath = args[1]; | |
System.out.println("Entering endless ping loop"); | |
while (1 == 1) { | |
runSystemCommand("ping " + ip + " -t"); | |
} | |
} | |
public static void runSystemCommand(String command) { | |
List<String> buffer = new ArrayList<String>(BUFFERSIZE); | |
try { | |
Process p = Runtime.getRuntime().exec(command); | |
BufferedReader inputStream = new BufferedReader(new InputStreamReader(p.getInputStream())); | |
String s = ""; | |
int timeoutCounter = 0; | |
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); | |
// reading the command's output stream | |
while ((s = inputStream.readLine()) != null) { | |
if (s.contains("Reply")) { | |
timeoutCounter=0; | |
if (DEBUG) System.out.println(sdf.format(new Date()) + "|" + s.substring(s.indexOf("time=") + 4 + 1, s.indexOf("ms")) + "|0" ); | |
buffer.add(sdf.format(new Date()) + "|" + s.substring(s.indexOf("time=") + 4 + 1, s.indexOf("ms")) + "|0" ); | |
} else if (s.contains("Request timed")) { | |
timeoutCounter++; | |
if (timeoutCounter >= 5) { | |
// set flag to 1 as there's longer than 5 second timeout | |
if (DEBUG) System.out.println( sdf.format(new Date()) + "|1000|1"); | |
buffer.add(sdf.format(new Date()) + "|1000|1"); | |
} else { | |
if (DEBUG) System.out.println( sdf.format(new Date()) + "|1000|0"); | |
buffer.add(sdf.format(new Date()) + "|1000|0"); | |
} | |
} else if (s.contains("General failure.")) { | |
// general failure is triggered in situations such as when the network cable is unplugged or the NIC restarts | |
// therefore lag flag is not being set | |
if (DEBUG) System.out.println( sdf.format(new Date()) + "|1100|0"); | |
buffer.add(sdf.format(new Date()) + "|1100|0"); | |
} | |
if (buffer.size() == BUFFERSIZE) { | |
try { | |
System.out.println("Buffer limit of " + BUFFERSIZE + " hit, flushing buffer to file"); | |
writeFile(buffer); | |
buffer.clear(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public static void writeFile(List<String> in) throws IOException { | |
File fout = new File(filePath); | |
FileOutputStream fos; | |
BufferedWriter bw; | |
if (fout.exists()) { | |
fos = new FileOutputStream(fout, true); | |
bw = new BufferedWriter(new OutputStreamWriter(fos)); | |
} | |
else { | |
fos = new FileOutputStream(fout, false); | |
bw = new BufferedWriter(new OutputStreamWriter(fos)); | |
bw.write("ts|latency|lag_flag"); | |
bw.newLine(); | |
} | |
// flushing buffer | |
for (String item : in) { | |
bw.write(item); | |
bw.newLine(); | |
} | |
bw.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment