Re: What is wrong with my `nmcli device status`?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 2020-08-22 05:25, Tom H wrote:
On Sat, Aug 22, 2020 at 11:53 AM Tom H <tomh0665@xxxxxxxxx> wrote:

On Sat, Aug 22, 2020 at 1:57 AM ToddAndMargo via users
<users@xxxxxxxxxxxxxxxxxxxxxxx> 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:

1) Why do you use "$InetDev" in GetIP and "eno2 in GetGW and
UpDownedEthernetDevices?

2) Why do you use "-i" for grep in GetIP?

3) Why do you use "sed -n 1,1p" in GetGW?

4) Why do you use "route" in GetGW when you're using "ip" elsewhere
and "net-tools" isn't installed by default?


Use of awk

1) "grep search | awk {...}" is the same as "awk '/search/ {...}"

2) "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.


Missed that.  Now they are all absolute paths.  Good
catch.  Thank you!

I use absolute paths when I will sometimes be operating
outside the shell.  It is a habit of mine to not trust
$PATH from the shell.  I do it all the time as a
"just in case".

I will go over the other four questions in a little bit.



_______________________________________________
users mailing list -- users@xxxxxxxxxxxxxxxxxxxxxxx
To unsubscribe send an email to users-leave@xxxxxxxxxxxxxxxxxxxxxxx
Fedora Code of Conduct: https://docs.fedoraproject.org/en-US/project/code-of-conduct/
List Guidelines: https://fedoraproject.org/wiki/Mailing_list_guidelines
List Archives: https://lists.fedoraproject.org/archives/list/users@xxxxxxxxxxxxxxxxxxxxxxx



[Index of Archives]     [Older Fedora Users]     [Fedora Announce]     [Fedora Package Announce]     [EPEL Announce]     [EPEL Devel]     [Fedora Magazine]     [Fedora Summer Coding]     [Fedora Laptop]     [Fedora Cloud]     [Fedora Advisory Board]     [Fedora Education]     [Fedora Security]     [Fedora Scitech]     [Fedora Robotics]     [Fedora Infrastructure]     [Fedora Websites]     [Anaconda Devel]     [Fedora Devel Java]     [Fedora Desktop]     [Fedora Fonts]     [Fedora Marketing]     [Fedora Management Tools]     [Fedora Mentors]     [Fedora Package Review]     [Fedora R Devel]     [Fedora PHP Devel]     [Kickstart]     [Fedora Music]     [Fedora Packaging]     [Fedora SELinux]     [Fedora Legal]     [Fedora Kernel]     [Fedora OCaml]     [Coolkey]     [Virtualization Tools]     [ET Management Tools]     [Yum Users]     [Yosemite News]     [Gnome Users]     [KDE Users]     [Fedora Art]     [Fedora Docs]     [Fedora Sparc]     [Libvirt Users]     [Fedora ARM]

  Powered by Linux