Skip to content

Instantly share code, notes, and snippets.

@utdrmac utdrmac/addpeers.sh Secret
Last active Oct 29, 2019

Embed
What would you like to do?
#!/bin/bash
#
# 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="-A 127.0.0.1 -P 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
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="mystique.tzkt.io"
for page in {0..5}; do
# get array of peers
peers=($(curl -s "https://$APIDOMAIN/v3/network?state=running&p=$page&n=50" | jq -r '.[] | .point_id' | xargs))
if [ ${#peers[@]} -eq 0 ]; then
# exit loop, no results for page
echo "> No more peers found. Exiting."
break
fi
# loop through peers array
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 $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."
@grassrobot

This comment has been minimized.

Copy link

grassrobot commented Mar 8, 2019

would love noob instructions on how to execute and use this.

@RomarQ

This comment has been minimized.

Copy link

RomarQ commented Mar 8, 2019

would love noob instructions on how to execute and use this.

  1. make sure tezos folder is inside $HOME dir
  2. download the script
  3. Install "jq" and "curl" packages on your system
  4. sudo chmod +x addpeers.sh
  5. Start your node
  6. ./addpeers.sh
@grassrobot

This comment has been minimized.

Copy link

grassrobot commented Mar 9, 2019

excellent. working!

@ZaneWithSpoon

This comment has been minimized.

Copy link

ZaneWithSpoon commented Jun 21, 2019

Any tips for running this in their Docker distribution?

@utdrmac

This comment has been minimized.

Copy link
Owner 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

This comment has been minimized.

Copy link

FreedomPrevails commented Oct 25, 2019

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

@utdrmac

This comment has been minimized.

Copy link
Owner 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.