RE: Checking Status of WWAN Device - If connected to USB Port & Connected to Network
Posted 06-27-2023 at 04:26 AM by tix
#!/bin/bash
#Ibrahim Kiptoo Kibowen - tkibowen@gmail.com - June 27th, 2023
#bash script to check for the ip address of a network interface and most importantly to confirm if the interface is actually plugged in
#eth1 in my case is a Huawei E3531 - Huawei WWAN dongle on Linux - (idVendor=12d1, idProduct=14dc) that appears as an ethernet device when plugged to USB port
#when connected to the internet via `dhcpcd eth1` the ISP gives a Class C private IP address to the dongle e.g. 192.168.x.x
#in case your Linux box cries about expansion of variables below simply run this script as bash -c '<script_name>' instead of ./<script_name>
#Enjoy!
#Tunawakilisha +254
#wewe! usikule mboflo na chai yangu! (hey you! don't eat my bread and tea)
#local variables - name of NIC - change to suite you system
public_wwan="eth1"
#grab the IP address of the iface (eth1)
WWAN_PUBLICIP=$(ip -4 a show $public_wwan|grep -i inet|awk '{print $1 ":" $2}')
#check that the device actually exists - sometimes the dongle was unplugged or USB bus generally lost it :)
WWAN_PUBLICIP_NAME=$(ifconfig $public_wwan|grep -i $public_wwan|awk '{print $1}'|sed 's/\://g')
#debug
echo " *********** WWAN IP ADDRESS: $WWAN_PUBLICIP AND NAME OF INTERFACE: $WWAN_PUBLICIP_NAME *********** "
##LOGIC-O-RAMA
if [[ $WWAN_PUBLICIP == *"192.168."* ]] && [ "${WWAN_PUBLICIP_NAME}" = "${public_wwan}" ]; then
echo "1 > SUCCESS - WWAN GSM interface is connected with IP $WWAN_PUBLICIP"
###....<put your extra processing/commands/notification here>....
#valid case 2 - not connected but device exists - added logic for $WWAN_PUBLICIP_NAME to get correct result
elif [ -z $WWAN_PUBLICIP ] && [ "${WWAN_PUBLICIP_NAME}" = "${public_wwan}" ]; then
echo "2 > FAILURE - $public_wwan detected but not connected ...trying to connect ... please wait ...."
###....<put your connection skript here> ....
#valid case 3 - not connected but device does NOT exists - added logic for $WWAN_PUBLICIP_NAME to get correct result
elif [ -z $WWAN_PUBLICIP ] && [ "${WWAN_PUBLICIP_NAME}" != "${public_wwan}" ]; then
echo "3 > FAILURE - $public_wwan offline or unplugged ...."
###....<put your extra processing/commands/notification here>....
#all other cases bogus
else
echo "ZZZzzz.... Bogus ZZZzZZzz......"
fi
#Ebu enda kamua ngo'mbe na ulishe kuku! (Go milk the cow and feed the chicken!)
#Tunawakilish +254'
###------------------------------------BEGIN :::: DEBUG :::: sample output------------------------------------
###sample output###
#
#
##case 1 - device plugged into USB port and connected
## *********** WWAN IP ADDRESS: inet:192.168.8.100/24 AND NAME OF INTERFACE: eth1 ***********
## 1 > SUCCESS - WWAN GSM interface is connected with IP inet:192.168.8.100/24
#
##case 2 - device plugged into USB port but not connected
## *********** WWAN IP ADDRESS: AND NAME OF INTERFACE: eth1 ***********
## 2 > FAILURE - eth1 detected but not connected ...trying to connect ... please wait ....
#
#
##case 3 - device unplugged/removed from usb port
## Device "eth1" does not exist.
## eth1: error fetching interface information: Device not found
## *********** WWAN IP ADDRESS: AND NAME OF INTERFACE: ***********
## 3 > FAILURE - eth1 offline or unplugged ....
###------------------------------------END :::: DEBUG :::: sample output------------------------------------
#Ibrahim Kiptoo Kibowen - tkibowen@gmail.com - June 27th, 2023
#bash script to check for the ip address of a network interface and most importantly to confirm if the interface is actually plugged in
#eth1 in my case is a Huawei E3531 - Huawei WWAN dongle on Linux - (idVendor=12d1, idProduct=14dc) that appears as an ethernet device when plugged to USB port
#when connected to the internet via `dhcpcd eth1` the ISP gives a Class C private IP address to the dongle e.g. 192.168.x.x
#in case your Linux box cries about expansion of variables below simply run this script as bash -c '<script_name>' instead of ./<script_name>
#Enjoy!
#Tunawakilisha +254
#wewe! usikule mboflo na chai yangu! (hey you! don't eat my bread and tea)
#local variables - name of NIC - change to suite you system
public_wwan="eth1"
#grab the IP address of the iface (eth1)
WWAN_PUBLICIP=$(ip -4 a show $public_wwan|grep -i inet|awk '{print $1 ":" $2}')
#check that the device actually exists - sometimes the dongle was unplugged or USB bus generally lost it :)
WWAN_PUBLICIP_NAME=$(ifconfig $public_wwan|grep -i $public_wwan|awk '{print $1}'|sed 's/\://g')
#debug
echo " *********** WWAN IP ADDRESS: $WWAN_PUBLICIP AND NAME OF INTERFACE: $WWAN_PUBLICIP_NAME *********** "
##LOGIC-O-RAMA
if [[ $WWAN_PUBLICIP == *"192.168."* ]] && [ "${WWAN_PUBLICIP_NAME}" = "${public_wwan}" ]; then
echo "1 > SUCCESS - WWAN GSM interface is connected with IP $WWAN_PUBLICIP"
###....<put your extra processing/commands/notification here>....
#valid case 2 - not connected but device exists - added logic for $WWAN_PUBLICIP_NAME to get correct result
elif [ -z $WWAN_PUBLICIP ] && [ "${WWAN_PUBLICIP_NAME}" = "${public_wwan}" ]; then
echo "2 > FAILURE - $public_wwan detected but not connected ...trying to connect ... please wait ...."
###....<put your connection skript here> ....
#valid case 3 - not connected but device does NOT exists - added logic for $WWAN_PUBLICIP_NAME to get correct result
elif [ -z $WWAN_PUBLICIP ] && [ "${WWAN_PUBLICIP_NAME}" != "${public_wwan}" ]; then
echo "3 > FAILURE - $public_wwan offline or unplugged ...."
###....<put your extra processing/commands/notification here>....
#all other cases bogus
else
echo "ZZZzzz.... Bogus ZZZzZZzz......"
fi
#Ebu enda kamua ngo'mbe na ulishe kuku! (Go milk the cow and feed the chicken!)
#Tunawakilish +254'
###------------------------------------BEGIN :::: DEBUG :::: sample output------------------------------------
###sample output###
#
#
##case 1 - device plugged into USB port and connected
## *********** WWAN IP ADDRESS: inet:192.168.8.100/24 AND NAME OF INTERFACE: eth1 ***********
## 1 > SUCCESS - WWAN GSM interface is connected with IP inet:192.168.8.100/24
#
##case 2 - device plugged into USB port but not connected
## *********** WWAN IP ADDRESS: AND NAME OF INTERFACE: eth1 ***********
## 2 > FAILURE - eth1 detected but not connected ...trying to connect ... please wait ....
#
#
##case 3 - device unplugged/removed from usb port
## Device "eth1" does not exist.
## eth1: error fetching interface information: Device not found
## *********** WWAN IP ADDRESS: AND NAME OF INTERFACE: ***********
## 3 > FAILURE - eth1 offline or unplugged ....
###------------------------------------END :::: DEBUG :::: sample output------------------------------------
Total Comments 0