Possible solution with a better answer: https://serverfault.com/questions/615561/avahi-how-do-i-setup-multiple-servers-that-originally-have-the-same-hostname
Avahi with multiple instances on the same network will simply increment an integer at the end of the name. This exactly what I am needing and the below is sort-of trying to accomplish.
Since this is a built-in feature of this implementation and possibly the protocol itself, this satisfies exactly what I need.
Now to enumerate and loop through that list of servers, and I am good-to-go!
My idea is to check the network to see if my avahi service dns exists. If so, increment it by 1 until it doesn't exist. Then set this to run before avahi-daemon starts (or after...and if a change is needed, apply the change and restart avahi-daemon).
nano -w /etc/avahi/services/canrelay-mqtt.service
Insert the text:
<!-- Put this in /etc/avahi/services/mosquitto.service -->
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
<service-group>
<name replace-wildcards="yes">Mosquitto MQTT server on %h</name>
<service>
<type>_canrelay._tcp</type>
<port>1883</port>
<txt-record>info=Publish, Publish! Read all about it! mqtt.org</txt-record>
</service>
</service-group>
Setup avahi-brows to look at the network and find similar mDNS domains:
sudo apt install avahi-browse
avahi-browse -apt | cut -d';' -f5 | grep _canrelay._tcp | sort | uniq
cat canrelay-mqtt.service | sed "s/<type>/;/" | sed "s@</type>@;@" | tr -d '\n' | tr -d '\r' | cut -d';' -f2
sudo /etc/avahi/setup-services.sh
So far I have this script:
#!/bin/bash
#avahi-browse -apt | cut -d';' -f5 | grep _canrelay._tcp | sort | uniq
#cat canrelay-mqtt.service | sed "s/<type>/;/" | sed "s@</type>@;@" | tr -d '\n' | tr -d '\r' | cut -d';' -f2
mkdir /etc/avahi/service-scripts/
## Only copy what doesn't exist - Well, once I learn how to send -pm flags when ran this way...
##find . -iname '*mqtt*' -type f -exec cp -pn {} /etc/avahi/services /etc/avahi/service-scripts
## Backup files if they do not already exist... Should this only migrate mqtt files? Probably...Lets do what is above.
yes n | cp -p -n /etc/avahi/services/*mqtt* /etc/avahi/service-scripts/
## Copies them back to their original. service-scripts now drives services!
yes y | cp -f -p /etc/avahi/service-scripts/* /etc/avahi/services/
for f in "/etc/avahi/services/*mqtt*"
do
# Should this only apply to mqtt? Append "| grep mqtt"???
service=`cat $f | sed "s/<type>/;/" | sed "s@</type>@;@" | tr -d '\n' | tr -d '\r' | cut -d';' -f2`
# If service can't be found, continue processing...
if [ -z "$service" ]
then
continue
fi
# Check if the non-local service is already running
foundservice=`avahi-browse -apt --ignore-local | cut -d';' -f5 | grep $service | sort | uniq | head -n 1`
index=0
currentservice="$service"
while [ "$currentservice" = "$foundservice" ]
do
index=$((index+1))
currentservice="${service}${index}"
foundservice=`avahi-browse -aplt | cut -d';' -f5 | grep $currentservice | sort | uniq | head -n 1`
done
if [ "$currentservice" != "service" ]
then
# Appended number at the end, since the service already exists
sed -i 's/$currentservice/$service/g' $f
fi
done
I haven't got to test this out yet. I only have one of the three Raspberry pi's up.
I also plan on having something similar with ESP32 and my Kendryte risc-v and their mDNS servers they can run (using the Arduino firmware - because it is quick for prototyping).
The protocol I am using is can bus. Which is why I chose can-relay. Though it is falling out of favor. Bluetooth mesh is a much better protocol.
Thank you @Dougie! I will keep editing this until I get it working how I need. With the first device having the main dns name. And all others being numbered and being secondary.
To get a list of IP's to try:
avahi-browse _canrelay._tcp --resolve -tp | grep ^= | cut -d';' -f8-9 | sed 's/;/:/g' | sort
Since this won't act like a normal dns. It is more like an advertised address. Nice that it includes the port.
Now to use this command to setup MQTT! In progress...
Let's kick off only the server. This does not yet account for the server dropping off. Load balancing mqtt might be a solution? But anyway. Here it is:
mkdir /etc/mqtt/run.d
nano -w /etc/mqtt/run.d/canrelay.sh
Contents:
#!/bin/bash
while read -r line
do
nc -vz $line || continue
ip=`echo "$line" | cut -d' ' -f1`
port=`echo "$line" | cut -d' ' -f2`
while read -r currentip
do
if [ "$ip" = "$currentip" ]
then
echo "port $port -> ip $ip"
echo "port $port" > /etc/mosquitto/conf.d/canrelay.conf
mosquitto -c /etc/mosquitto/conf.d/canrelay.conf && echo "Server mqtt has been established!" && exit 0
fi
done <<< `ifconfig | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b"`
done <<< `avahi-browse _canrelay._tcp --resolve -tp | grep ^= | cut -d';' -f8-9 | sed 's/;/ /g' | sort`
while read -r line
do
nc -vz $line || continue
ip=`echo "$line" | cut -d' ' -f1`
port=`echo "$line" | cut -d' ' -f2`
while read -r currentip
do
if [ "$ip" != "$currentip" ]
then
echo "port $port -> ip $ip"
mosquitto_pub -h $ip -p $port -t can_relay -m "Publish stared" && echo "Client mqtt has been established!" && exit 0
fi
done <<< `ifconfig | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b"`
done <<< `avahi-browse _canrelay._tcp --resolve -tp | grep ^= | cut -d';' -f8-9 | sed 's/;/ /g' | sort`
echo "Warning: Could not successfully establish a server or a client."
chmod +x canrelay.sh
hostname
– Milliways Aug 5 at 5:06hostname
pointed me towards mDNS / avahi / zeroconf. Sometimes the simple answers are the best ones. I apologize for being vague. Thank you! – TamusJRoyce Aug 6 at 0:25