On Sat, Aug 22, 2020 at 11:53 AM Tom H tomh0665@gmail.com wrote:
On Sat, Aug 22, 2020 at 1:57 AM ToddAndMargo via users users@lists.fedoraproject.org wrote:
Followup: I create a number of bash functions to handle the task(s). Thank you all for the help!
function GetIP () { echo "$(ip -f inet route | grep -i $InetDev | grep -i src | awk '{print $9}')" }
function CheckInternetStatus () { local GoogleDNS="8.8.8.8"; local RtnStr="$( ping -W .1 -c 1 $GoogleDNS | grep loss );" # echo "RtnStatus = <$?> RtnStr = <$RtnStr>" > "/dev/tty" if [[ "$RtnStr" == *", 0% packet loss"* ]]; then echo "UP" else echo "DOWN" fi }
function GetPublicIP () { local WIP=$(dig +short myip.opendns.com @resolver1.opendns.com) # echo "WIP = <$WIP>" > "/dev/tty" if [[ "$WIP" == *"not found"* || "$WIP" == *"no servers could be reached"* ]]; then WIP="Not Found" fi
echo $WIP}
function GetGW () { echo "$(route -n | grep eno2 | grep UG | sed -n 1,1p | awk '{print $2}')" }
function UpDownedEthernetDevices () { local State="" local LinkState=""
for Line in $(nmcli device | grep ethernet | awk '{print $1}'); do State="$(nmcli device | grep $Line | awk '{print $3}')" LinkState="$(ip address | grep eno2 | awk -F "," '{print $3}')" # echo "Line = <$Line> State = <$State> LinkState =<$LinkState>')" > "/dev/tty"
if [ "$State" == "disconnected" ]; then echo "Reconnecting $Line" > "/dev/tty" /usr/bin/nmcli device disconnect $Line /usr/bin/nmcli device connect $Line /usr/bin/nmcli connection down $Line /usr/bin/nmcli connection up $Line elif [[ $LinkState == "DOWN" ]]; then echo "Reattaching (link) $Line" > "/dev/tty" beesu "ip link set $Line down; ip link set $Line up;" echo "Reconnecting $Line" > "/dev/tty" /usr/bin/nmcli connection down $Line /usr/bin/nmcli connection up $Line fi done}
function DisplayStatus () { NotifySound
local Msg="" local GW="" local IP="" local InternetStatus="$(CheckInternetStatus)" local PublicIP="" # if [ -n "$(netstat -rn | grep -i $InetDev | grep -i UG)" ]; then if [ "$InternetStatus" == "UP" ]; then GW="$(GetGW)" IP="$(GetIP)" PublicIP="$(GetPublicIP)" # Msg+="$InetDev successfully started and is a gateway\n\n" Msg+="Internet is $InternetStatus\n" Msg+="IP=$IP\n" Msg+="gw=$GW\n" Msg+="Public IP=$PublicIP\n" echo -e $Msg > "/dev/tty" zenity --title "$Title Status" --info --text "$Msg" --width=250 else Msg="Internet ($InetDev) is down" echo -e $Msg > "/dev/tty" zenity --title "$Title Status" --info --text "$Msg" --width=200 fi}
Without looking at what your functions are actually doing:-
Things that I don't understand:
- Why do you use "$InetDev" in GetIP and "eno2 in GetGW and
UpDownedEthernetDevices?
Why do you use "-i" for grep in GetIP?
Why do you use "sed -n 1,1p" in GetGW?
Why do you use "route" in GetGW when you're using "ip" elsewhere
and "net-tools" isn't installed by default?
Use of awk
"grep search | awk {...}" is the same as "awk '/search/ {...}"
"grep search1 | grep search2 | awk {...}" is the same as "awk
'/search1/ && /search2/ {...}"
Forgot
5) Why do you use "/usr/bin/nmcli" 6 times and "nmcli" twice? There's no need for absolute paths.