send_interfaces_IP.sh

This script runs on a remote Raspberry Pi to transmit at regular interval, via MQTT, its various interfaces (IP) : eth, wlan, ppp, wan

#!/bin/bash
# This script allows the device to send its own interfaces IP, after a reboot, then at regular interval
# The Mosquitto-client 'mosquitto_pub' is used to send the data to a mosquitto broker
# Information can then be collected by Home Assistant for example
TOPIC_WLAN=piplex/wlan
TOPIC_ETH=piplex/eth
TOPIC_WAN=piplex/wan
TOPIC_PPP=piplex/ppp
DIRECTORY='/mnt' # the directory in which any temporary files will be stored
HOSTNAME=''
# uses a broker which has greater chances to be always on.
BROKER='bubuoat.mine.nu'
DOMAIN='zadsiti.mine.nu'
# files where to store the last wlan IP address FILENAME_WLAN=$DIRECTORY/last_wlan_ip.txt
IP_FILE_WLAN=$DIRECTORY/wlan_ip.txt
IP_FINAL_WLAN=$DIRECTORY/wlan_ip_final.txt
CLEAN_IP_WLAN=$DIRECTORY/wlan_clean_ip.txt
FIRST_LINE_WLAN=$DIRECTORY/ip_wlan.txt
# files where to store the last WAN Ip address of the eth interface
FILENAME_ETH=$DIRECTORY/last_eth_ip.txt
IP_FILE_ETH=$DIRECTORY/eth_ip.txt
IP_FINAL_ETH=$DIRECTORY/eth_ip_final.txt
CLEAN_IP_ETH=$DIRECTORY/eth_clean_ip.txt
FIRST_LINE_ETH=$DIRECTORY/ip_eth.txt
# files where to store the last WAN Ip address of the 3gdongle
FILENAME_WAN=$DIRECTORY/last_wan_ip.txt
IP_FILE_WAN=$DIRECTORY/ip_wan.txt
FILENAME_PPP=$DIRECTORY/last_ppp_ip.txt
IP_FILE_PPP=$DIRECTORY/ip_ppp.txt
IP_FINAL_PPP=$DIRECTORY/ppp_ip_final.txt
CLEAN_IP_PPP=$DIRECTORY/clean_ip_ppp.txt
# wait 180 seconds to make sure Piplex3 has rebooted
#sleep 180
#reads the Hostname
HOSTNAME=`hostname`

while true
do
# delete all previous files
#WLAN
if [[ -s $FILENAME_WLAN ]]; then
	rm $FILENAME_WLAN
fi
if [[ -s $IP_FILE_WLAN ]]; then
	rm $IP_FILE_WLAN
fi
if [[ -s $IP_FINAL_WLAN ]]; then
	rm $IP_FINAL_WLAN
fi
if [[ -s $CLEAN_IP_WLAN ]]; then
	rm $CLEAN_IP_WLAN
fi
if [[ -s $FIRST_LINE_WLAN ]]; then
	rm $FIRST_LINE_WLAN
fi
#ETH
if [[ -s $FILENAME_ETH ]]; then
	rm $FILENAME_ETH
fi
if [[ -s $IP_FILE_ETH ]]; then
	rm $IP_FILE_ETH
fi
if [[ -s $IP_FINAL_ETH ]]; then
	rm $IP_FINAL_ETH
fi
if [[ -s $CLEAN_IP_ETH ]]; then
	rm $CLEAN_IP_ETH
fi
if [[ -s $FIRST_LINE_ETH ]]; then
	rm $FIRST_LINE_ETH
fi
#WAN
if [[ -s $FILENAME_WAN ]]; then
	rm $FILENAME_WAN
fi
if [[ -s $IP_FILE_WAN ]]; then
	rm $IP_FILE_WAN
fi
#PPP
if [[ -s $FILENAME_PPP ]]; then
	rm $FILENAME_PPP
fi
if [[ -s $IP_FILE_PPP ]]; then
	rm $IP_FILE_PPP
fi
if [[ -s $IP_FINAL_PPP ]]; then
	rm $IP_FINAL_PPP
fi
if [[ -s $CLEAN_IP_PPP ]]; then
	rm $CLEAN_IP_PPP
fi
if [[ -s $FIRST_LINE_PPP ]]; then
	rm $FIRST_LINE_PPP
fi
# Remove any previous dongle file
if [[ -s $DIRECTORY/dlink.txt ]]; then
	rm $DIRECTORY/dlink.txt
fi
if [[ -s $DIRECTORY/huawei.txt ]]; then
	rm $DIRECTORY/huawei.txt
fi
if [[ -s $DIRECTORY/qualcomm.txt ]]; then
	rm $DIRECTORY/qualcomm.txt
fi

#my_date=`/bin/date "+%d.%m.%y - %H.%M.%S"`
# get the wlan IP
ifconfig wlan0 >> $IP_FILE_WLAN
# search for the first 'inet' word and store it into FILENAME_WLAN
grep 'inet' $IP_FILE_WLAN | sed -e 's/^[[:space:]]*//' > $FILENAME_WLAN

#reads the 1st line of the file and store it into WAN_IP
WAN_IP=$(head -n 1 $FILENAME_WLAN)

# copy WAN_IP into the file FIRST_LINE_WLAN
echo "$WAN_IP" | sed 's_inet__' | sed -e 's/^[[:space:]]*//' >> $FIRST_LINE_WLAN
# reads the first 16 characters of the first line and copy them into IP_FINAL_WLAN file
dd if=$FIRST_LINE_WLAN of=$IP_FINAL_WLAN bs=16 count=1 'status=none'
# reads the first line of IP_FINAL_WLAN file
IP=$(head -n 1 $IP_FINAL_WLAN)
# removes any character after the first 'Space' encoutered
echo "${IP%% *}" > $CLEAN_IP_WLAN
# reads the first line of CLEAN_IP file and stores into 'clean_ip'
clean_ip_wlan=$(head -n 1 $CLEAN_IP_WLAN)

# publish the information with mosquitto_pub
mosquitto_pub -p 1883 -h $BROKER -t $TOPIC_WLAN -m  "$HOSTNAME $clean_ip_wlan"

# Process the ETH interface
ifconfig eth0 >> $DIRECTORY/eth_ip.txt
# search for the first 'inet' word and store it into FILENAME_ETH
grep 'inet' $DIRECTORY/eth_ip.txt | sed -e 's/^[[:space:]]*//' > $FILENAME_ETH

#reads the 1st line of the file and store it into ETH_IP
ETH_IP=$(head -n 1 $FILENAME_ETH)

# copy ETH_IP into the file FIRST_LINE_ETH
echo "$ETH_IP" | sed 's_inet__' | sed -e 's/^[[:space:]]*//' >> $FIRST_LINE_ETH
# reads the first 16 characters of the first line and copy them into IP_FINAL_ETH file
dd if=$FIRST_LINE_ETH of=$IP_FINAL_ETH bs=16 count=1 'status=none'
# reads the first line of IP_FINAL_ETH file
IP=$(head -n 1 $IP_FINAL_ETH)
# removes any character after the first 'Space' encoutered
echo "${IP%% *}" > $CLEAN_IP_ETH
# reads the first line of CLEAN_IP file
clean_ip_eth=$(head -n 1 $CLEAN_IP_ETH)

# publish the information with mosquitto_pub
mosquitto_pub -p 1883 -h $BROKER -t $TOPIC_ETH -m "$HOSTNAME $clean_ip_eth"

# process the WAN interface
ping -c 1 $DOMAIN > $IP_FILE_WAN
#echo "# $my_date " >> $DIRECTORY/piplex/last_wan_ip.txt
grep '(' $IP_FILE_WAN | sed -e 's/^[[:space:]]*//' > $FILENAME_WAN

#reads the 1st line of the file
WAN_IP=$(head -n 1 $FILENAME_WAN)
# extract anything before open parenthesis
string=${WAN_IP#*(}
# extract anything after close parenthesis
clean_ip_wan=${string%%)*}

mosquitto_pub -p 1883 -h $BROKER -t $TOPIC_WAN -m "$HOSTNAME $clean_ip_wan"

# Process the PP interface when a 3gDongle is in use
# HERE there is a need to do a test to detect whether a USB Dongle is inserted into the device
dongle=0
brand="Unknown"
# insert as many dmesg command with different #G Dongle brand to increase the possibility of test
# to detect the brand, insert the USB dongle into the device and run the command 'lsusb'
# the device should appear, with the brand clearly listed in the result
dmesg | grep "D-Link" >>  $DIRECTORY/dlink.txt
dmesg | grep "HUAWEI" >>  $DIRECTORY/huawei.txt
dmesg | grep "Qualcomm" >>  $DIRECTORY/qualcomm.txt

if [[ -s $DIRECTORY/dlink.txt ]]; then
	brand="D-Link"
	dongle=1
fi
if [[ -s $DIRECTORY/huawei.txt ]]; then
	brand="Huawei"
	dongle=1
fi
if [[ -s $DIRECTORY/qualcomm.txt ]]; then
        brand="Qualcomm"
        dongle=1
fi

if  test "$dongle" = "1" ; then
	# There is a 3G USB Dongle
	ifconfig ppp0 > $IP_FILE_PPP
	#echo "# $my_date " >> $DIRECTORY/samba/crescent/last_wan_ip.txt
	grep 'inet' $IP_FILE_PPP | sed -e 's/^[[:space:]]*//' > $FILENAME_PPP

	#reads the 1st line of the file
	WAN_IP=$(head -n 1 $FILENAME_PPP)
	#echo $WAN_IP
	# copy WAN_IP into a file
	echo "$WAN_IP" | sed 's_inet__' | sed -e 's/^[[:space:]]*//' > $IP_FILE_PPP
	# reads the first 16 characters of the first line and copy them into IP_FINAL file
	dd if=$IP_FILE_PPP of=$IP_FINAL_PPP bs=16 count=1 'status=none'
	# reads the first line of IP_FINAL file
	IP=$(head -n 1 $IP_FINAL_PPP)
	# removes any character after the first 'Space' encoutered
	echo "${IP%% *}" > $CLEAN_IP_PPP
	# reads the first line of CLEAN_IP file
	clean_ip_ppp=$(head -n 1 $CLEAN_IP_PPP)

	mosquitto_pub -p 1883 -h $BROKER -t $TOPIC_PPP -m "$HOSTNAME $clean_ip_ppp"
fi
#send to Pushover
#curl -s \
  --form-string "token=a33wtohzbzvwedcuweijvinyvhmh8t" \
  --form-string "user=u6ysovfgq1nhysszxzh91qnwadch2y" \
  --form-string "title=$HOSTNAME"\
  --form-string "message=Wlan $clean_ip_wlan" \
  https://api.pushover.net/1/messages.json
# wait 60 seconds (1 minute) before next pulling
sleep 60
done
exit 0