Skip to content

Instantly share code, notes, and snippets.

@laanwj
Created June 15, 2017 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laanwj/27241686db0db712cd94f692a491ad78 to your computer and use it in GitHub Desktop.
Save laanwj/27241686db0db712cd94f692a491ad78 to your computer and use it in GitHub Desktop.
Count statistics about connections for bitcoind RPC
#!/usr/local/bin/python3 [5/1819]
import sys
sys.path.append('./bitcoin/test/functional')
from test_framework.authproxy import AuthServiceProxy, JSONRPCException
import time
import os
datadir = os.path.join(os.getenv('HOME'), '.bitcoin')
with open(os.path.join(datadir,'.cookie'),'r') as f:
cookie = f.read()
tgt = "http://%s@127.0.0.1:8332/" % (cookie)
proxy = AuthServiceProxy(tgt)
out = sys.stdout # stream to write to
while True:
now = time.time()
peers = proxy.getpeerinfo()
in_ipv4 = 0
in_ipv6 = 0
in_tor = 0
out_ipv4 = 0
out_ipv6 = 0
out_tor = 0
for peer in peers:
addr = peer['addr']
if peer['inbound']:
if addr.startswith('['):
in_ipv6 += 1
elif addr.startswith('127.0.0.1:'):
in_tor += 1
else:
in_ipv4 += 1
else:
if addr.startswith('['):
out_ipv6 += 1
elif '.onion' in addr:
out_tor += 1
else:
out_ipv4 += 1
print('[by network]')
print('in: ipv4 %3d ipv6 %3d tor %3d' % (in_ipv4, in_ipv6, in_tor))
print('out: ipv4 %3d ipv6 %3d tor %3d' % (out_ipv4, out_ipv6, out_tor))
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment