What's the fastest way to display the current Monero block height with a shell script? Doesn't matter which scripting language. The idea is to call this script or block from other scripts, so it should just return the value.
Asked
Active
Viewed 170 times
6
-
1Can you be more specific as to what you're doing? Do you have the daemon at your disposal, for instance, or are you limited to online APIs? – bigreddmachine Sep 21 '16 at 06:29
-
1Sure. I have an Applescript dialog, wherein I enter a specific block number, to then show the transactions for it in Safari. The default value in the dialog is the current block height, which is looked up by this shell script. I don’t have a local daemon on this particular computer, so I think I need to go out to the block explorer. I haven’t thought about where it would be possible to interrogate a remote daemon to retrieve the same info. Is it possible? – dpzz Sep 21 '16 at 21:59
-
yep, it's possible. See my answer below :) – bigreddmachine Sep 21 '16 at 23:14
3 Answers
6
I propose the following with simple bash scripting using curl and jq.
First, install jq (to parse json)
- macOS:
brew install jq - Ubuntu:
sudo apt-get install jq - Others at https://stedolan.github.io/jq/download/
Then, write a simple bash script
Using the moneroclub public node:
#!/bin/bash
INFO=($(curl -sS -X POST http://node.moneroclub.com:8880/json_rpc -d '{"jsonrpc":"2.0","id":"0","method":"\
get_info"}' -H 'Content-Type: application/json' | jq '.result.height'))
echo $INFO
Alternatively, using moneroblocks.info:
#!/bin/bash
INFO=($(curl -sS -X GET http://moneroblocks.info/api/get_stats/ | jq '.height|tonumber'))
echo $INFO
bigreddmachine
- 3,737
- 1
- 10
- 30
-
Thanks for this. Maybe, they're not "simpler", because depending on the platform, one needs to install extra software. Out-of-the-box, I don't have
jqon macOS (but I didn't specify so in my question). However, both of yourcurlandjqmethods are definitely faster than usingruby. I will adjust my question in that sense, so that I can accept your answer. One remark though: I had to remove the backslash in your first code example for it to work. – dpzz Oct 08 '16 at 08:56
5
Currently, I am using ruby to parse the JSON returned from moneroblocks.info:
ruby << END_OF_RUBY
require 'open-uri'
require 'json'
open ("http://moneroblocks.info/api/get_stats/") { |src|
puts JSON.parse(src.read)["height"]
}
END_OF_RUBY
Which right nows returns: 1140328
dpzz
- 4,539
- 3
- 19
- 45
4
If you have a daemon running on your own machine, use:
height=$(monerod print_height)
This will save the height as the shell variable $height.
I see now that the question was for using a block explorer. Oh well.
Ginger Ale
- 5,676
- 2
- 18
- 46