6

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.

  • 1
    Can 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 6:29
  • 1
    Sure. 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
6

I propose the following with simple bash scripting using curl and jq.


First, install jq (to parse json)


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
  • 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 jq on macOS (but I didn't specify so in my question). However, both of your curl and jq methods are definitely faster than using ruby. 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 8 '16 at 8: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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.