skip to Main Content

I want to get the output only the interface which is down based on the configured interfaces in the network-scripts directory.

for i in $(ls -1 /etc/sysconfig/network-scripts/ifcfg-e*|sed 's/.*ifcfg-//g'); do grep -i 'down' /sys/class/net/$i/operstate; echo $i;done

How can I list only the interfaces which is down based on the input of the for iteration?

Thank you

2

Answers


  1. Chosen as BEST ANSWER

    Actually this one does the job:

    for i in $(ls -1 /etc/sysconfig/network-scripts/ifcfg-e*|sed 's/.*ifcfg-//g'); do grep -i 'down' /sys/class/net/$i/operstate &> /dev/null;[ $? -eq "0" ] && echo $i;done|awk '{printf "%s ",$i} END {print ""}'
    

  2. I suggest with bash, its Parameter Expansion and CentOS 7 / RHEL 7:

    for i in /etc/sysconfig/network-scripts/ifcfg-e*; do 
      i=$(basename "$i")
      grep -qi 'down' "/sys/class/net/${i#*-}/operstate" && echo "${i#*-}"
    done
    

    ${i#*-} removes from string ifcfg-ens33 ifcfg- to get only interface name.

    Update:

    for i in /etc/sysconfig/network-scripts/ifcfg-e*; do
      grep -qi 'down' "/sys/class/net/${i##*-}/operstate" && echo "${i##*-}"
    done
    

    ${i##*-} removes from string /etc/sysconfig/network-scripts/ifcfg-ens33 /etc/sysconfig/network-scripts/ifcfg- to get only interface name.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search