walx4r
walx4r

Reputation: 1

Sh script to output unused interfaces on linux

I asked the dark side and here's what it printed.....

#!/bin/bash

for interface in $(ip addr show | awk '/^[0-9]+:/ {print $2}' | tr -d :)
do
  if ! ip addr show $interface | awk '/inet / {print $2}' | grep -q . ; then
    echo $interface
  fi
done

I want to add n+ variable directly so the output will be the interfaces that is not used by the system,

Done

Upvotes: 0

Views: 64

Answers (1)

walx4r
walx4r

Reputation: 1

#!/bin/bash

count=0
for interface in $(ip addr show | awk '/^[0-9]+:/ {print $2}' | tr -d :)
do
  if ! ip addr show $interface | awk '/inet / {print $2}' | grep -q . ; then
    free_interfaces[$count]=$interface
    count=$((count + 1))
  fi
done

case $count in
  0)
    echo "No usable interface found."
    exit 1
    ;;
  1)
    DEFIF=${free_interfaces[0]}
    echo "The interface $DEFIF will be used."
    ;;
  *)
    echo "Available interfaces to select: "
    PS3="Press a number to select an interface to use (1-$count): "
    select interface in "${free_interfaces[@]}"; do
      DEFIF=$interface
      break
    done
    echo "The interface $DEFIF will be used."
    ;;
esac

Upvotes: 0

Related Questions