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).

1  
!7
2  
3  
static S ip = "tinybrain.de" /*"127.0.0.1"*/;
4  
static int count = 1;
5  
static boolean result;
6  
7  
p {
8  
  // Java has trouble executing pings, so we use the native command.
9  
  
10  
  S cmd = "ping " + (isWindows() ? "-n" : "-c") + " " + count + " " + ip;
11  
  S output = backtick(cmd);
12  
  print(output);
13  
  
14  
  new Matches m;
15  
  if (isWindows()) {
16  
    if (jfind3("reply from *.*.*.*: bytes=* time=* ms ttl=*", output.toLowerCase(), m)) {
17  
      S ip = m.unq(0) + "." + m.unq(1) + "." + m.unq(2) + "." + m.unq(3);
18  
      S ttl = m.unq(6), time = m.unq(5);
19  
      print("Reachable. IP " + ip + " TTL " + ttl + " TIME " + time);
20  
    } else
21  
      print("Not reachable.");
22  
  } else {
23  
    if (jfind3("(*.*.*.*): *=* ttl=* time=*", output.toLowerCase(), m)) {
24  
      S ip = m.unq(0) + "." + m.unq(1) + "." + m.unq(2) + "." + m.unq(3);
25  
      S ttl = m.unq(6), time = m.unq(7);
26  
      print("Reachable. IP " + ip + " TTL " + ttl + " TIME " + time);
27  
    } else
28  
      print("Not reachable.");
29  
  }
30  
}
31  
32  
static boolean jfind3(S pat, S s, Matches matches) {
33  
  if (s == null) return false;
34  
  ret jfind3(pat, javaTok(s), matches);
35  
}
36  
37  
static boolean jfind3(S pat, L<S> toks, Matches matches) {
38  
  L<S> tokpat = javaTok(pat);
39  
  S[] m = jfind2(tokpat, toks);
40  
  //print(structure(tokpat) + " on " + structure(toks) + " => " + structure(m));
41  
  if (m == null)
42  
    ret false;
43  
  else {
44  
    if (matches != null) matches.m = m;
45  
    ret true;
46  
  }
47  
}
48  
49  
static S[] jfind2(L<S> pat, L<S> tok) {
50  
  for (int idx = 0; idx < tok.size(); idx += 2) {
51  
    S[] result = jfind2(pat, tok, idx);
52  
    if (result != null) return result;
53  
  }
54  
  return null;
55  
}
56  
57  
static S[] jfind2(L<S> pat, L<S> tok, int idx) {
58  
  if (idx+pat.size() > tok.size())
59  
    return null;
60  
  new L<S> result;
61  
  for (int i = 1; i < pat.size(); i += 2) {
62  
    S p = pat.get(i), t = tok.get(idx+i);
63  
    if (eq(p, "*"))
64  
      result.add(t);
65  
    else if (!eq(p, t))
66  
      return null;
67  
  }
68  
  return toStringArray(result);
69  
}

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: 791 / 1321
Version history: 3 change(s)
Referenced in: [show references]