Thursday, May 10, 2018

How to execute bash shell command and get result in Java

Java is a very powerful programming language. But it's more powerful when combine Java with Bash.

First. import necessary library.
import java.io.BufferedReader;
import java.io.InputStreamReader;

Second.
String cmd = "nslookup -type=any "+str;
Process proc = Runtime.getRuntime().exec(cmd);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
StringBuilder result = new StringBuilder();
String s = null;
while ((s = stdInput.readLine()) != null)
{
    result = result.append(s).append('\n');
}
Result:
example 1, execute "ping google.com":

PING google.com (173.194.202.139) 56(84) bytes of data.
64 bytes from pf-in-f139.1e100.net (173.194.202.139): icmp_seq=1 ttl=53 time=0.663 ms
64 bytes from pf-in-f139.1e100.net (173.194.202.139): icmp_seq=2 ttl=53 time=0.259 ms
64 bytes from pf-in-f139.1e100.net (173.194.202.139): icmp_seq=3 ttl=53 time=0.253 ms
64 bytes from pf-in-f139.1e100.net (173.194.202.139): icmp_seq=4 ttl=53 time=0.344 ms

--- google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3040ms
rtt min/avg/max/mdev = 0.253/0.379/0.663/0.169 ms

online portal: https://myipaddress.pro/ping

example 2, execute "nslookup -type=any google.com"

Server: 127.0.0.53
Address: 127.0.0.53#53

Non-authoritative answer:
Name: google.com
Address: 173.194.202.113
Name: google.com
Address: 173.194.202.101
Name: google.com
Address: 173.194.202.100
Name: google.com
Address: 173.194.202.139
Name: google.com
Address: 173.194.202.138
Name: google.com
Address: 173.194.202.102
Name: google.com
Address: 2607:f8b0:400e:c00::65
google.com nameserver = ns3.google.com.
google.com nameserver = ns2.google.com.
google.com nameserver = ns1.google.com.
google.com mail exchanger = 20 alt1.aspmx.l.google.com.
google.com text = "docusign=05958488-4752-4ef2-95eb-aa7ba8a3bd0e"
google.com text = "v=spf1 include:_spf.google.com ~all"
google.com mail exchanger = 40 alt3.aspmx.l.google.com.
google.com rdata_257 = 0 issue "pki.goog"
google.com nameserver = ns4.google.com.
google.com mail exchanger = 10 aspmx.l.google.com.
google.com
origin = ns1.google.com
mail addr = dns-admin.google.com
serial = 196121370
refresh = 900
retry = 900
expire = 1800
minimum = 60
google.com mail exchanger = 50 alt4.aspmx.l.google.com.

Authoritative answers can be found from:
online portal: https://myipaddress.pro/nslookup


example 3, execute "traceroute facebook.com"

traceroute to facebook.com (157.240.22.35), 30 hops max, 60 byte packets
 1  108.170.242.249 (108.170.242.249)  18.725 ms 108.170.242.233 (108.170.242.233)  18.685 ms  28.299 ms
 2  * * *
 3  po131.asw01.sjc1.tfbnw.net (157.240.32.28)  19.409 ms  26.368 ms po131.asw03.sjc1.tfbnw.net (157.240.32.32)  19.391 ms
 4  po215.psw02.sjc3.tfbnw.net (157.240.47.223)  19.634 ms po244.psw04.sjc3.tfbnw.net (157.240.48.33)  19.869 ms po214.psw04.sjc3.tfbnw.net (157.240.48.27)  19.940 ms
 5  173.252.67.3 (173.252.67.3)  19.516 ms 173.252.67.61 (173.252.67.61)  19.579 ms 173.252.67.83 (173.252.67.83)  19.676 ms
 6  edge-star-mini-shv-01-sjc3.facebook.com (157.240.22.35)  19.353 ms  23.683 ms  19.263 ms

online portal: https://myipaddress.pro/traceroute

How to use CoinMarketCap JSON API

CoinMarketCap is a professional blockchain market information website. You can get real time market information from there. Such as Maret cap, trade volume, trending and so on.
If you use API provided by coinmarketcap, you can build your own version of coinmarketcap. How can you use this API, let's find out.

Step 1, you need to visit this API list https://coinmarketcap.com/api/.
Step 2, As above, if you visit https://api.coinmarketcap.com/v2/ticker/?limit=10 in your browser, you can get the raw json string.


Step 3, you can use a online json format tools to minify the data.
such as Best Programming Toolkithttps://bestprogrammingtoolkit.com/json-formatter
put the json string you just get in step 2 in the first textbox, then click "Compact/Minify".
the result will show in the second textbox.

FYI, Best Programming Toolkit is a very useful website, it can do many task such as:
JSON formatter: https://bestprogrammingtoolkit.com/json-formatter
XML formatter: https://bestprogrammingtoolkit.com/xml-formatter


Step 4, use Java or json or any programming language to parse the string you got in the step 3.
In python:
use build-in package JSON.
  1. import json
  2. str = '{"name": "Bitcoin","price": 9346.5}'
  3. resultString = json.dumps(json.loads(str), indent=4, ensure_ascii=False)
  4. print(resultString)
In Java:
use Maven GSON developed by google.
import GSON in pom.xml
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
code:
  1. import com.google.gson.JsonElement;
  2. import com.google.gson.Gson;
  3. import com.google.gson.GsonBuilder;
  4. import com.google.gson.JsonParser;
  5. //function
  6. public String format(String jsonStr) {
  7. JsonParser parser = new JsonParser();
  8. JsonElement je = parser.parse(jsonStr);
  9. Gson gson = new GsonBuilder().setPrettyPrinting().create();
  10. return gson.toJson(je);
  11. }


And that's how it's should be done.