Skip to content

Instantly share code, notes, and snippets.

@saada2006
Created November 22, 2022 21:42
Show Gist options
  • Save saada2006/b6f0f22184766bc6b815ba8b8d48dec6 to your computer and use it in GitHub Desktop.
Save saada2006/b6f0f22184766bc6b815ba8b8d48dec6 to your computer and use it in GitHub Desktop.
Networking Thing
import java.awt.Desktop;
import java.io.*;
import java.sql.Timestamp;
import java.util.ArrayList;
public class Main {
public static boolean pinging = true;
public static final long DELAY = 100;
public static int countOccurences(String src, String token) {
int count = 0, idx = 0;
while(true) {
idx = src.indexOf(token, idx);
if(idx == -1)
break;
idx++;
count++;
}
return count;
}
public static String getField(String src, String token, String delims, boolean includeEnd) {
int loc = src.indexOf(token);
if(loc == -1) {
return "0";
}
String field = "";
for(int i = loc + token.length(); i < src.length(); i++) {
char c = src.charAt(i);
boolean bad = (delims.indexOf(c) != -1);
if(!bad || (bad && includeEnd))
field += c;
if(bad)
break;
}
return field;
}
public static String execProcess(ProcessBuilder procBuilder) throws IOException, InterruptedException {
Process proc = procBuilder.start();
BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String output = "";
String line = null;
while((line = br.readLine()) != null) {
output += line + '\n';
}
return output;
}
public static void main(String[] args) throws IOException, InterruptedException {
File logFileName = new File("log-" + System.currentTimeMillis() + ".txt");
PrintStream log = new PrintStream(logFileName);
ProcessBuilder procBuilder = new ProcessBuilder();
procBuilder.command("cmd.exe", "/c", "ping 1.1.1.1");
Thread cli = new Thread() {
public void run() {
System.out.println("Press ENTER to stop");
try { System.in.read(); } catch (Exception iDontCare) {};
System.out.println("Stopping...");
pinging = false;
}
};
cli.start();
int samples = 0;
int longRunAvg = 0;
int longRunSent = 0;
int longRunRecv = 0;
ArrayList<Timestamp> failures = new ArrayList<Timestamp>();
long nextSleep = System.currentTimeMillis() + DELAY;
while(pinging) {
// basically all we need to do is get our summary from pinging
String result = execProcess(procBuilder);
Timestamp now = new Timestamp(System.currentTimeMillis());
log.print("[" + now + "]\t");
int avg = 0, sent = 0, recv = 0;
try {
avg = Integer.valueOf(getField(result, "Average = ", "ms\n", false));
sent = Integer.valueOf(getField(result, "Sent = ", ",", false));
} catch (Exception e) {
e.printStackTrace();
System.out.println("Returned result:\n" + result);
}
recv = countOccurences(result, "TTL");
if(recv != sent || avg == 0)
failures.add(now);
if(avg != 0) {
longRunAvg += avg;
samples++;
}
longRunSent += sent;
longRunRecv += recv;
log.println("Avg " + avg + "ms\tSuccess Rate " + recv + "/" + sent);
long duration = nextSleep - System.currentTimeMillis();
if(duration > 0)
Thread.sleep(duration);
nextSleep = System.currentTimeMillis() + DELAY;
}
if(samples == 0)
samples = 1;
log.println("Long run ping: " + longRunAvg / samples + "ms");
log.println("Long run success rate: " + longRunRecv + "/" + longRunSent + " (" + 100.0 * longRunRecv/longRunSent + "%)");
if(failures.size() == 0) {
log.println("No failures, hooray!");
} else {
log.println("Failure timestamps:");
for(Timestamp fail : failures) {
log.println("\t"+fail);
}
}
System.out.println("Opening log file...");
Desktop.getDesktop().open(logFileName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment