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 02:53, Tom H 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?

Because I screwed up.  Good catch.  If $INetDev had changed,
I would have had some troubleshooting to do!  I fixed.


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

"-i" is case insensitive.  Do  do it out of habit.  Makes
so I don't have to constantly wonder about the case.

I use the method ".lc" in Raku (Perl 6) all the time for
the same reason.  I just don't trust the case.

When I am careening through web pages, I often
find the web developer will change case on me at
times.  So I plan for him.


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

Because I only wanted the first line.  Sometimes
with that commend you get two lines back.  I am
covering all my bases.


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

I like to mixed up all the different way of doing things.
Sort of like showing alternate examples as I go


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/ {...}"

I have a cheat sheet of awk examples. I tend to use what I remember first though. I should learn the "search" property
and stick it in the sheet.   Problem is that I am migrating
away from bash and into Raku (Perl 6) as I have a bazillion
times more power in Raku.  And Raku is just plain fun to
program in.

I should post these things here more often.  You are a great
second pair of eyes!

-T


Here is my latest:

This is from another program.  I find it almost impossible
to find my gateway if the device is down.  In the program,
all the devices need to be up anyway, so I up anyone that
is down.  I use both "device" and "connection" as I
find that if cone does not work, the other will.  I also
"down" first as sometimes I have found "up" error out
on me if I don't first down.  So, I am covering my bases.

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
}

UpDownedEthernetDevices
eth1=$(netstat -rn | grep UG | awk '{print $8}' | sed -n 1,1p)

if [ -z $eth1 ]; then
   eth1=eno2
   echo "Unable to find an Internet connected device, defaulting to $eth1"
fi


This is the updated functions I originally posted.  They
contain fixes you pointed out and tabs in the status
function to pretty it up:

InetDev=eno2

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 $InetDev | grep UG | sed -n 1,1p | awk '{print $2}')"
}


function UpDownedEthernetDevices () {
   local State=""
   local LinkState=""

for Line in $(/usr/bin/nmcli device | grep ethernet | awk '{print $1}'); do
       State="$(/usr/bin/nmcli device | grep $Line | awk '{print $3}')"
       LinkState="$(ip address | grep $InetDev | 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+="\tInternet is $InternetStatus\n\n"
      Msg+="IP\t\t= $IP\n"
      Msg+="gw\t\t= $GW\n"
      Msg+="Public IP\t= $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
}
_______________________________________________
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