Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

69
LINES

< > BotCompany Repo | #1002698 // Native Ping with getting IP and time (tested on Linux and Windows)

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Libraryless. Click here for Pure Java version (4045L/28K).

!7

static S ip = "tinybrain.de" /*"127.0.0.1"*/;
static int count = 1;
static boolean result;

p {
  // Java has trouble executing pings, so we use the native command.
  
  S cmd = "ping " + (isWindows() ? "-n" : "-c") + " " + count + " " + ip;
  S output = backtick(cmd);
  print(output);
  
  new Matches m;
  if (isWindows()) {
    if (jfind3("reply from *.*.*.*: bytes=* time=* ms ttl=*", output.toLowerCase(), m)) {
      S ip = m.unq(0) + "." + m.unq(1) + "." + m.unq(2) + "." + m.unq(3);
      S ttl = m.unq(6), time = m.unq(5);
      print("Reachable. IP " + ip + " TTL " + ttl + " TIME " + time);
    } else
      print("Not reachable.");
  } else {
    if (jfind3("(*.*.*.*): *=* ttl=* time=*", output.toLowerCase(), m)) {
      S ip = m.unq(0) + "." + m.unq(1) + "." + m.unq(2) + "." + m.unq(3);
      S ttl = m.unq(6), time = m.unq(7);
      print("Reachable. IP " + ip + " TTL " + ttl + " TIME " + time);
    } else
      print("Not reachable.");
  }
}

static boolean jfind3(S pat, S s, Matches matches) {
  if (s == null) return false;
  ret jfind3(pat, javaTok(s), matches);
}

static boolean jfind3(S pat, L<S> toks, Matches matches) {
  L<S> tokpat = javaTok(pat);
  S[] m = jfind2(tokpat, toks);
  //print(structure(tokpat) + " on " + structure(toks) + " => " + structure(m));
  if (m == null)
    ret false;
  else {
    if (matches != null) matches.m = m;
    ret true;
  }
}

static S[] jfind2(L<S> pat, L<S> tok) {
  for (int idx = 0; idx < tok.size(); idx += 2) {
    S[] result = jfind2(pat, tok, idx);
    if (result != null) return result;
  }
  return null;
}

static S[] jfind2(L<S> pat, L<S> tok, int idx) {
  if (idx+pat.size() > tok.size())
    return null;
  new L<S> result;
  for (int i = 1; i < pat.size(); i += 2) {
    S p = pat.get(i), t = tok.get(idx+i);
    if (eq(p, "*"))
      result.add(t);
    else if (!eq(p, t))
      return null;
  }
  return toStringArray(result);
}

Author comment

Began life as a copy of #1001197

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1002698
Snippet name: Native Ping with getting IP and time (tested on Linux and Windows)
Eternal ID of this version: #1002698/4
Text MD5: 7f21162f8f38e21d30b5b0c98d7a3c2c
Transpilation MD5: 23ee9410a1b5c9e6bfd24239270c27d3
Author: stefan
Category: javax networking
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-10-18 13:02:22
Source code size: 1985 bytes / 69 lines
Pitched / IR pitched: No / No
Views / Downloads: 655 / 1069
Version history: 3 change(s)
Referenced in: [show references]