Skip to content

Update peer.bash to be able to change the peer's IP #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 124 additions & 21 deletions peer.bash
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function existingPeerInfo() {
# Get existing peer information
exPeerName="$(grep PEERNAME networks/${theNet}/${themem} | cut -d: -f2 )"
exPeerDesc="$(grep PEERDESC networks/${theNet}/${themem} | cut -d: -f2 )"
themem_info="ID: ${themem} Name: ${exPeerName} Description: ${exPeerDesc}"
themem_info="ID: ${themem} Name: ${exPeerName} Description: ${exPeerDesc} Ip: ${ifIP} "

}

Expand Down Expand Up @@ -83,21 +83,22 @@ function selectMem() {
function getAllPeers() {

# Add header to file
echo "Peer IP Name" > ${tmpPeerFile}
echo "Peer IP IPpub Name Descripcion" > ${tmpPeerFile}

# Get all the members
for themem in $(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/member"| egrep -o '[a-f0-9]{10}'); do

# Check if the peer is authorized
ifAuth=$(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/member/${themem}" | jq '.authorized')
ifIPpub=$(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddr}/peer/${themem}" | jq '.paths[0].address')
ifIP=$(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/member/${themem}" | jq -r '.ipAssignments[]')

# ...is so then display it.
#...is so then display it.
if [[ "${ifAuth}" == "true" ]]; then

existingName=$(grep PEERNAME "networks/${theNet}/${themem}" | cut -d: -f2)

echo "${themem} ${ifIP} ${existingName}" >> ${tmpPeerFile}
echo "${themem} ${ifIP} ${ifIPpub} ${existingName}" >> ${tmpPeerFile}

fi

Expand All @@ -119,7 +120,8 @@ function getAllPeers() {
function peerManage() {

# Create members if they do not exist.
for themem in $(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/member"| egrep -o '[a-f0-9]{10}'); do
for themem in $(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/member"| egrep -o '[a-f0-9]{10}')
do

if [[ ! -f "networks/${theNet}/${themem}" ]]; then

Expand All @@ -132,7 +134,11 @@ function peerManage() {
clear

delTemp

desde=$(echo ${net} | awk '{ print $3 }')
hasta=$(echo ${net} | awk '{ print $4 }')
desden=(${desde//./ })
hastan=(${hasta//./ })
echo $desde - $hasta
echo "##################################################"
echo "Network ID and Name: ${net}"
echo "##################################################"
Expand All @@ -143,6 +149,7 @@ function peerManage() {
echo "5. 'Delete' a peer"
echo "6. Add/Change a peer's name or description"
echo "7. 'UnDelete' a peer"
echo "8. Add/Change a peer's ip"
echo "[Z] Back to Network Configuration Main Menu"
echo "[E] Exit Program"
read -p " Please select a number value: " todo
Expand All @@ -153,14 +160,18 @@ function peerManage() {
clear

# Add header to file
echo "Peer IP Name" > ${tmpPeerFile}

for themem in $(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/member"| egrep -o '[a-f0-9]{10}'); do

echo "Peer IP IPpub Name Descripcion" > ${tmpPeerFile}
# debug man echo curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/member"
for themem in $(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/member"| egrep -o '[a-f0-9]{10}')
do
#debug man echo $themem
# Check if the member is authorized.
ifIP=$(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/member/${themem}" |jq -r '.ipAssignments[]')
ifAuth=$(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/member/${themem}" |jq '.authorized')

ifIP=$(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/member/${themem}" | jq -r '.ipAssignments[]')
ifAuth=$(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/member/${themem}" | jq '.authorized')
ifIPpub=$(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddr}/peer/${themem}" | jq '.paths[0].address')

# debug man echo "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/peer/${themem}"

# If the user is authorized, don't show them
if [[ ("${ifAuth}" =~ "false" && "${ifIP}" =~ "127.0.0.100") ]]; then

Expand All @@ -172,7 +183,7 @@ function peerManage() {
existingPeerInfo

# Write results to the temp file.
echo "${themem} ${ifIP} ${exPeerName}" >> ${tmpPeerFile}
echo "${themem} ${ifIP} ${ifIPpub} ${exPeerName} ${exPeerDesc}" >> ${tmpPeerFile}

fi

Expand Down Expand Up @@ -256,14 +267,15 @@ function peerManage() {
delTemp

# Add header to file
echo "Peer IP Name" > ${tmpPeerFile}
echo "Peer IP IPpub Name Descripcion" > ${tmpPeerFile}

# Get all the members
for themem in $(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/member"| egrep -o '[a-f0-9]{10}'); do

# Check if the peer is authorized
ifAuth=$(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/member/${themem}" | jq '.authorized')
ifIP=$(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/member/${themem}" | jq -r '.ipAssignments[]')
ifIPpub=$(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddr}/peer/${themem}" | jq '.paths[0].address')

# ...is so then display it.
if [[ "${ifAuth}" == "true" ]]; then
Expand All @@ -272,7 +284,7 @@ function peerManage() {
existingPeerInfo


echo "${themem} ${ifIP} ${exPeerName}" >> ${tmpPeerFile}
echo "${themem} ${ifIP} ${ifIPpub} ${exPeerName} ${exPeerDesc}" >> ${tmpPeerFile}

fi

Expand Down Expand Up @@ -305,22 +317,23 @@ function peerManage() {
delTemp

# Add header to file
echo "Peer IP Name" > ${tmpPeerFile}
echo "Peer IP IPpub Name Descripcion" > ${tmpPeerFile}

# Get all the members
for themem in $(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/member"| egrep -o '[a-f0-9]{10}'); do

# Check if the peer is authorized
ifAuth=$(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/member/${themem}" | jq '.authorized')
ifIP=$(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/member/${themem}" | jq -r '.ipAssignments[]')
ifIPpub=$(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddr}/peer/${themem}" | jq '.paths[0].address')

# ...is so then display it.
if [[ "${ifAuth}" == "true" ]]; then

# Get existing Peer Info
existingPeerInfo

echo "${themem} ${ifIP} ${exPeerName}" >> ${tmpPeerFile}
echo "${themem} ${ifIP} ${ifIPpub} ${exPeerName} ${exPeerDesc}" >> ${tmpPeerFile}

fi

Expand Down Expand Up @@ -393,7 +406,7 @@ function peerManage() {
# Get existing Peer Info
existingPeerInfo

echo "${themem} ${exPeerName}" >> ${tmpPeerFile}
echo "${themem} ${exPeerName} ${exPeerDesc}" >> ${tmpPeerFile}

fi

Expand Down Expand Up @@ -467,6 +480,9 @@ function peerManage() {

# Get the selection value
thePeer=$(sed -n "${opt}p" <<< "${PEERS}")
themem=$(echo "${thePeer}" | awk ' { print $1 } ')
ifIP=$(echo "${thePeer}" | awk ' { print $2 } ')


existingPeerInfo

Expand Down Expand Up @@ -558,7 +574,7 @@ function peerManage() {
# Get existing Peer Info
existingPeerInfo

echo "${themem} ${exPeerName}" >> ${tmpPeerFile}
echo "${themem} ${exPeerName} ${exPeerDesc}" >> ${tmpPeerFile}

fi

Expand Down Expand Up @@ -617,6 +633,93 @@ function peerManage() {


;;

8)

clear
# Get all peers
getAllPeers

# Bring up Edit menu
selectMem "Edit"

# Authorize the member
if [[ $(seq 1 $SELECTION) =~ $opt ]]; then

# Get the selection value
thePeer=$(sed -n "${opt}p" <<< "${PEERS}")
themem=$(echo "${thePeer}" | awk ' { print $1 } ')
ifIP=$(echo "${thePeer}" | awk ' { print $2 } ')

existingPeerInfo

# Existing Information
if [[ ${exPeerName} != "" ]]; then

echo "Existing Name: ${exPeerName}"

else

exPeerName="empty"

fi

# Get the member ID
themem=$(echo "${thePeer}" | awk ' { print $1 } ')

echo "${themem_info}"
# Prompt for the IP
read -p "Enter the peer ip (leave blank for no changes): " peerIp

if ! [[ "${peerIp}" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
peerManage
fi

# If no changes then go back to Peer Manage page.
if [[ "${peerIp}" == "" ]]; then

peerManage
fi
peerIpn=(${peerIp//./ })
# No esta en la red
if [ ${peerIpn[0]} -lt ${desden[0]} ] | [ ${peerIpn[1]} -lt ${desden[1]} ] | [ ${peerIpn[2]} -lt ${desden[2]} ] | [ ${peerIpn[3]} -lt ${desden[3]} ]
then
peerManage
fi
if [ ${peerIpn[0]} -gt ${hastan[0]} ] | [ ${peerIpn[1]} -gt ${hastan[1]} ] | [ ${peerIpn[2]} -gt ${hastan[2]} ] | [ ${peerIpn[3]} -gt ${hastan[3]} ]
then
peerManage
fi

echo "New Ip: ${peerIp}"
read -p "To add the new information above, hit Enter or E to not change." toEdit

# Check if user wants to exit
if [[ "${toEdit}" =~ ^(e|E)$ ]]; then

peerManage

fi
json=$(jq -n --arg peerip "${peerIp}" '{ ipAssignments:[$peerip] }')

# debug man echo curl -X POST -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" -d "$json" "${ztAddress}/${theNet}/member/${themem}"
reIP=$(curl -X POST -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" -d "$json" "${ztAddress}/${theNet}/member/${themem}" | jq -r '(.ipAssignments[])')
#debug man echo $reIP
#debug man read
if [[ $? -eq 0 ]]; then

read -p "Peer Information added. Press Enter to continue." readEnter

peerManage

fi

fi

;;



z|Z) # Back to Main configuration

bash ztnetworks.bash
Expand Down