Search code examples
bashshellscriptingcentosrhel7

I want to check only the network interfaces status on the server which is listed in the command output on linux bash


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


Solution

  • 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.