From b40c17421d23e6369ca4826f3955d5aa79cd7057 Mon Sep 17 00:00:00 2001 From: khundera Date: Tue, 3 Jun 2025 09:20:53 +0200 Subject: [PATCH 1/2] Update peer.bash to be able to change the peer's IP Displays and allows changing the peer's IP --- peer.bash | 145 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 124 insertions(+), 21 deletions(-) diff --git a/peer.bash b/peer.bash index 8af9e92..62a5dd8 100644 --- a/peer.bash +++ b/peer.bash @@ -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} " } @@ -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 @@ -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 @@ -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 "##################################################" @@ -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 @@ -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 @@ -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 @@ -256,7 +267,7 @@ 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 @@ -264,6 +275,7 @@ function peerManage() { # 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 @@ -272,7 +284,7 @@ function peerManage() { existingPeerInfo - echo "${themem} ${ifIP} ${exPeerName}" >> ${tmpPeerFile} + echo "${themem} ${ifIP} ${ifIPpub} ${exPeerName} ${exPeerDesc}" >> ${tmpPeerFile} fi @@ -305,7 +317,7 @@ 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 @@ -313,6 +325,7 @@ function peerManage() { # 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 @@ -320,7 +333,7 @@ function peerManage() { # Get existing Peer Info existingPeerInfo - echo "${themem} ${ifIP} ${exPeerName}" >> ${tmpPeerFile} + echo "${themem} ${ifIP} ${ifIPpub} ${exPeerName} ${exPeerDesc}" >> ${tmpPeerFile} fi @@ -393,7 +406,7 @@ function peerManage() { # Get existing Peer Info existingPeerInfo - echo "${themem} ${exPeerName}" >> ${tmpPeerFile} + echo "${themem} ${exPeerName} ${exPeerDesc}" >> ${tmpPeerFile} fi @@ -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 @@ -558,7 +574,7 @@ function peerManage() { # Get existing Peer Info existingPeerInfo - echo "${themem} ${exPeerName}" >> ${tmpPeerFile} + echo "${themem} ${exPeerName} ${exPeerDesc}" >> ${tmpPeerFile} fi @@ -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 From 397319450e3c7d6fe9645a87838f82493cf0ed91 Mon Sep 17 00:00:00 2001 From: khundera Date: Wed, 25 Jun 2025 11:20:13 +0200 Subject: [PATCH 2/2] Displays public IP and authorization status in drop-downs Displays public IP and authorization status in drop-downs --- functions.bash | 3 ++- peer.bash | 31 +++++++++++++++++-------------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/functions.bash b/functions.bash index 8750fa7..c5c466c 100644 --- a/functions.bash +++ b/functions.bash @@ -1,7 +1,8 @@ # Various functions for the program # ZeroTier network address -ztAddress='http://127.0.0.1:9993/controller/network' +ztAddr='http://127.0.0.1:9993' +ztAddress="$ztAddr/controller/network" # Token file ztToken='/var/lib/zerotier-one/authtoken.secret' diff --git a/peer.bash b/peer.bash index 62a5dd8..c6f4de0 100644 --- a/peer.bash +++ b/peer.bash @@ -35,10 +35,12 @@ fi function existingPeerInfo() { # Get existing peer information + if [ -e "networks/${theNet}/${themem}" ] + then 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} Ip: ${ifIP} " - + fi } function delTemp() { @@ -90,9 +92,10 @@ function getAllPeers() { # 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') + ifIPpub=$(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddr}/peer/${themem}" | jq -r '.paths[0].address') + #debug man echo $ifIPpub 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. if [[ "${ifAuth}" == "true" ]]; then @@ -160,7 +163,7 @@ function peerManage() { clear # Add header to file - echo "Peer IP IPpub Name Descripcion" > ${tmpPeerFile} + echo "Peer IP IPpub Name Autorizado 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 @@ -168,10 +171,10 @@ function peerManage() { # 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') - ifIPpub=$(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddr}/peer/${themem}" | jq '.paths[0].address') + ifIPpub=$(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddr}/peer/${themem}" | jq -r '.paths[0].address') -# debug man echo "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/peer/${themem}" - +# debug man echo "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddr}/peer/${themem}" + # If the user is authorized, don't show them if [[ ("${ifAuth}" =~ "false" && "${ifIP}" =~ "127.0.0.100") ]]; then @@ -183,7 +186,7 @@ function peerManage() { existingPeerInfo # Write results to the temp file. - echo "${themem} ${ifIP} ${ifIPpub} ${exPeerName} ${exPeerDesc}" >> ${tmpPeerFile} + echo "${themem} ${ifIP} ${ifIPpub} ${exPeerName} ${ifAuth} ${exPeerDesc}" >> ${tmpPeerFile} fi @@ -267,7 +270,7 @@ function peerManage() { delTemp # Add header to file - echo "Peer IP IPpub Name Descripcion" > ${tmpPeerFile} + echo "Peer IP IPpub Name Autorizado 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 @@ -275,7 +278,7 @@ function peerManage() { # 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') + ifIPpub=$(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddr}/peer/${themem}" | jq -r '.paths[0].address') # ...is so then display it. if [[ "${ifAuth}" == "true" ]]; then @@ -284,7 +287,7 @@ function peerManage() { existingPeerInfo - echo "${themem} ${ifIP} ${ifIPpub} ${exPeerName} ${exPeerDesc}" >> ${tmpPeerFile} + echo "${themem} ${ifIP} ${ifIPpub} ${exPeerName} ${ifAuth} ${exPeerDesc}" >> ${tmpPeerFile} fi @@ -317,7 +320,7 @@ function peerManage() { delTemp # Add header to file - echo "Peer IP IPpub Name Descripcion" > ${tmpPeerFile} + echo "Peer IP IPpub Nameb Autorizado 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 @@ -325,7 +328,7 @@ function peerManage() { # 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') + ifIPpub=$(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddr}/peer/${themem}" | jq -r '.paths[0].address') # ...is so then display it. if [[ "${ifAuth}" == "true" ]]; then @@ -333,7 +336,7 @@ function peerManage() { # Get existing Peer Info existingPeerInfo - echo "${themem} ${ifIP} ${ifIPpub} ${exPeerName} ${exPeerDesc}" >> ${tmpPeerFile} + echo "${themem} ${ifIP} ${ifIPpub} ${exPeerName} ${ifAuth} ${exPeerDesc}" >> ${tmpPeerFile} fi