Skip to content

Instantly share code, notes, and snippets.

@utdrmac
Last active March 5, 2024 14:57
Show Gist options
  • Save utdrmac/2125a8514960e163837e86bc934f19b6 to your computer and use it in GitHub Desktop.
Save utdrmac/2125a8514960e163837e86bc934f19b6 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# 20210119 - Use the new --endpoint flag
# - Added verbose logging
#
# 20200608 - Updated to TzKt API
#
# 20191029 - Added /v3/network back in.
# Thanks to Baking-Bad and their Mystique API
# - Added some more check logic
# - Added '>' prefix so you can tell who is outputting
#
# 20191025 - Filtering improvements @FreedomPrevails
#
# 20191019 - Babylon Update
# TZScan was the only public place to find a listing of all connected
# tezos nodes. With tzscan offline, this information is no longer
# accessible. The script below will still attempt to maintain connections
# to the foundation nodes.
# The tezos-admin-client binary will output 'Error' messages in
# most cases, even when already connected to a peer.
#
# If you found this script helpful, send us a tip!
# Baking Tacos! tz1RV1MBbZMR68tacosb7Mwj6LkbPSUS1er1
#
# Where is tezos installed?
TZPATH=/home/myuser/tezos
PARAMS="--endpoint http://127.0.0.1:8732"
# Sanity Tests
command -v $TZPATH/tezos-admin-client >/dev/null 2>&1 || { echo >&2 "Cannot find 'tezos-admin-client' in $TZPATH"; exit 1; }
command -v jq >/dev/null 2>&1 || { echo >&2 "'jq' is required. Please install it."; exit 1; }
command -v curl >/dev/null 2>&1 || { echo >&2 "'curl' is required. Please install it."; exit 1; }
command -v dig >/dev/null 2>&1 || { echo >&2 "'dig' is required. Please install it."; exit 1; }
command -v ss >/dev/null 2>&1 || { echo >&2 "'ss' is required. Please install it."; exit 1; }
# Cache connections list
NET=$(ss -nt state established)
newpeers=0
# get foundation nodes
echo "> Getting list of foundation nodes..."
for i in dubnodes franodes sinnodes nrtnodes pdxnodes; do
for j in `dig $i.tzbeta.net +short`; do
if [ -z "$(echo $NET | grep $j)" ]; then
echo "> Connecting foundation $j..."
$TZPATH/tezos-admin-client $PARAMS connect address [$j]:9732
if [ $? -eq 0 ]; then
((newpeers++))
# echo "> New connection to $j established"
fi
fi
done
done
# Public Nodes
# Loop over pages from mystique API.
APIDOMAIN="services.tzkt.io"
echo "> Fetching list of public nodes from https://$APIDOMAIN/v1/network"
for page in {0..5}; do
# get array of peers
peers=($(curl -s "https://$APIDOMAIN/v1/network?state=running&p=$page&n=50" | jq -r '.[] | .point_id' | xargs))
if [ ${#peers[@]} -eq 0 ]; then
# exit loop, no results for page
echo "> ... complete"
break
fi
# loop through peers array
echo "> Processing list of public nodes..."
for i in ${peers[@]}; do
# handle ipv4 or ipv6
numparts=$(echo $i | awk -F: '{print NF}')
basenum=$((numparts-1))
port=$(echo $i | cut -d: -f$numparts)
base=$(echo $i | cut -d: -f1-$basenum)
formatted="[$base]:$port"
if [ -z "$(echo $NET | grep $base)" ]; then
echo "> Connecting to $formatted..."
$TZPATH/tezos-admin-client $PARAMS connect address $formatted
if [ $? -eq 0 ]; then
((newpeers++))
#echo "> New connection to $j established"
fi
fi
done
done
# how many peers do we have now? how many did we add?
numpeers=$($TZPATH/tezos-admin-client $PARAMS p2p stat | grep "MAINNET" | wc -l)
echo "> Added $newpeers peers. Currently $numpeers connected. Done."
@ZaneWithSpoon
Copy link

Any tips for running this in their Docker distribution?

@utdrmac
Copy link
Author

utdrmac commented Jun 21, 2019

@ZaneWithSpoon I'm not good enough with docker to give you a solid answer. At first guess, you could mount this script something like -v addpeers.sh:/opt/path/addpeers.sh maybe? Or create a new docker volume, copy this script into the volume and mount it within the container?

@FreedomPrevails
Copy link

jq and curl are not required anymore.
You need to make sure you have installed dnsutils for dig.

@utdrmac
Copy link
Author

utdrmac commented Oct 29, 2019

Updated code to check for required utilities. Merged @FreedomPrevails improvements. Deleted comment with their code to avoid confusion. Re-added /v3/network polling.

@sebuhbitcoin
Copy link

Ive been seeing my nodes getting pretty low in peer count. going to give this a shot tomorrow. thanks.

@sebuhbitcoin
Copy link

i love this script. very useful for todays network congestion!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment