Skip to content

Instantly share code, notes, and snippets.

@faddat
Last active May 19, 2023 11:31
Show Gist options
  • Save faddat/569b6062a62768d23905c22c948736fc to your computer and use it in GitHub Desktop.
Save faddat/569b6062a62768d23905c22c948736fc to your computer and use it in GitHub Desktop.
scan for open tendermint rpcs
#!/bin/bash
# Required tools
command -v jq >/dev/null 2>&1 || { echo >&2 "jq is required but it's not installed. Aborting."; exit 1; }
command -v curl >/dev/null 2>&1 || { echo >&2 "curl is required but it's not installed. Aborting."; exit 1; }
# Fetch the chain.json file
chain_json_url="https://raw.githubusercontent.com/cosmos/chain-registry/master/neutron/chain.json"
chain_json=$(curl -s $chain_json_url)
# Extract the RPC URLs
rpcs=$(echo "$chain_json" | jq -r '.rpcs[]')
# Get the list of peers from the net_info endpoint
get_peers() {
local url=$1
local response=$(curl -s "$url/net_info")
# Check if the response is a valid JSON
if ! echo "$response" | jq empty >/dev/null 2>&1; then
echo "Invalid response from $url"
return 1
fi
# Try to extract peers
local peers=$(echo "$response" | jq -r '.result.peers[].node_info.listen_addr')
if [ $? -ne 0 ]; then
echo "Failed to extract peers from $url"
return 1
fi
echo "$peers"
}
# Check if the RPC port is open and returns a valid response for a given address
check_rpc() {
local address=$1
local url="http://$address/net_info"
local response=$(curl -s "$url")
# Check if the response is a valid JSON
if ! echo "$response" | jq empty >/dev/null 2>&1; then
return 1
fi
return 0
}
# Main script
main() {
for rpc in $rpcs
do
local peers=$(get_peers $rpc)
if [ $? -ne 0 ]; then
continue
fi
for peer in $peers
do
# Split the address into IP and port
local ip=$(echo "$peer" | cut -d ':' -f 1)
local port=$(echo "$peer" | cut -d ':' -f 2)
# If the IP doesn't start with 0 or 127 and the port is specified, use the address from rpc_address
if [[ ! $ip =~ ^(0|127) ]] && [ -n "$port" ]; then
if check_rpc $peer; then
echo "Valid RPC found at $peer"
else
echo "Invalid RPC at $peer"
fi
else
# Otherwise use the remote_ip and port 26657
local default_port=26657
local default_address="$ip:$default_port"
if check_rpc $default_address; then
echo "Valid RPC found at $default_address"
else
echo "Invalid RPC at $default_address"
fi
fi
done
done
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment