forked from cybercongress/networks
-
Notifications
You must be signed in to change notification settings - Fork 16
/
getpeers.sh
57 lines (50 loc) · 1.42 KB
/
getpeers.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/bin/bash
## https://gist.github.com/Cordtus/87b147862627d3039f394695b78e4361
## dump connected peers to comma separated persistent_peers list
RED='\033[0;91m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m'
x=0
printf "\n${RED}rpc${NC} address?: "
read -r RPC
DATA=$(curl -s $RPC/net_info)
if [ -z "$DATA" ]
then
printf "%b\n${RED}rpc%b${NC} error\n\n"
exit
fi
NETWORK=$(echo $DATA | jq '.result | .peers | .[0] | .node_info | .network' | tr -d '"')
COUNT=$(echo $DATA | jq '.result | .n_peers' | tr -d '"')
while [ $x -lt $COUNT ]
do
if [ $(echo $DATA | jq ".result | .peers | .[$x] | .is_outbound") == 'true' ]
then
ID=$(echo $DATA | jq ".result | .peers | .[$x] | .node_info | .id" 2> /dev/null)
ADDRESS=$(echo $DATA | jq ".result | .peers | .[$x] | .remote_ip" 2> /dev/null)
PORT=$(echo $DATA | jq ".result | .peers | .[$x] | .node_info | .listen_addr" 2> /dev/null | tr -d '"')
PEER=$(echo $ID | tr -d '"')@$(echo $ADDRESS | tr -d '"'):${PORT##*:}
PEERS[${#PEERS[@]}]="$PEER"
((x++))
else
((x++))
fi
done
printf "\n${RED}$x${NC} outbound peers found"
printf "\n${RED}number${NC} of peers to list? (1,3,10,all): "
read -r NUMBER
if [ -z $NUMBER ]
then
TOTAL="$x"
elif [ $NUMBER == "all" ] || [ $NUMBER -ge $x ]
then
TOTAL="$x"
else
TOTAL="$NUMBER"
fi
while [[ $i -lt $TOTAL ]]
do
P_LIST="$P_LIST,$(echo ${PEERS[$i]} | tr -d '\n')"
((i++))
done
printf "\npersistent_peers = \"${P_LIST:1}\"\n"