Created
October 24, 2019 07:22
-
-
Save corck/8c929d685df637a7e66a763bb2fadcf1 to your computer and use it in GitHub Desktop.
find connected NEO nodes from coz monitor RPC list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import ipaddress | |
def get_endpoints(): | |
endpoints = [] | |
response = requests.get("https://raw.githubusercontent.com/CityOfZion/neo-mon/master/docs/assets/mainnet.json") | |
response.raise_for_status() | |
if response.status_code == 200: | |
results = response.json() | |
for site in results['sites']: | |
if site["type"] == "RPC": | |
if "address" in site: | |
endpoints.append(f"{site['protocol']}://{site['address']}:{site['port']}") | |
continue | |
if "url" in site: | |
endpoints.append(f"{site['protocol']}://{site['url']}:{site['port']}") | |
return endpoints | |
def main(): | |
payload = { | |
"jsonrpc": "2.0", | |
"method": "getpeers", | |
"params": [], | |
"id": 1 | |
} | |
addresses = [] | |
for addr in get_endpoints(): | |
try: | |
response = requests.post(addr, json=payload, timeout=3) | |
except Exception: | |
continue | |
response.raise_for_status() | |
if response.status_code == 200: | |
if 'result' in response.json(): | |
result = response.json()['result'] | |
if 'connected' in result: | |
for entry in result['connected']: | |
addresses.append(f"{entry['address']}:{entry['port']}") | |
print(list(set(addresses))) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment