7

I am looking for a cheap and efficient way (rather than hacking through the Bitcoin source code) of listing my peers.

Something I have come up with:

#!/bin/bash

netstat -p tcp -nba | grep  '.8333.*ESTABLISHED' | perl -npe 's/.*\s+((\d+.){3}\d+)\.\d+\s+ESTABLISHED.*/$1/'

The script above generates a list of IPs.

Ning
  • 2,741
  • 3
  • 18
  • 16

2 Answers2

5

Use the getpeerinfo RPC command (available since v0.7.0).

Pieter Wuille
  • 98,249
  • 9
  • 183
  • 287
3

First, setup add the following to your bitcoin.conf to enable RPC (of course, you can use other values):

rpcuser=user 
rpcpassword=Pa55w0rd
rpcport=8333
server=1

After that, restart bitcoin and use following command to get IPs of all peers connected (you can probably make the parser part shorter by using perl instead of grep/sed):

USER=user
PASSWORD=Pa55w0rd
PORT=8333
curl --user $USER:$PASSWORD --data-binary \
'{"jsonrpc": "1.0", "id":"curltest", "method": "getpeerinfo", "params": [] }' \
-H 'content-type: text/plain;' http://127.0.0.1:$PORT/ 2>/dev/null \
| grep -o '"addr":"[^"]\+"' | cut -d":" -f2-3 | sed 's/^"//;s/"$//'
aland
  • 1,388
  • 8
  • 18