Skip to content

Instantly share code, notes, and snippets.

@o0-o
Last active May 7, 2019 23:27
Show Gist options
  • Save o0-o/82b4d3bf0b24563b419ad41aa6a68679 to your computer and use it in GitHub Desktop.
Save o0-o/82b4d3bf0b24563b419ad41aa6a68679 to your computer and use it in GitHub Desktop.
[Network Interface Cards] Prints NICs installed on the host #Shell
# net_nics.sh
set -o pipefail &&
( #macos
system_profiler SPEthernetDataType |
grep "BSD name" -B 11 | #hardware NICs have BSD names (excludes virtual interaces and bridges)
grep -v -E "Name|Type|Bus|ID|Sub|Link" | #remove lines we're not concerned with
sed 's/BSD name://g' | #remove labels
sed 's/://g' | #remove colons
tr -d '\n' | #remove new lines
awk '{ gsub("--","\n"); print $0 }' | #replace -- with new lines
awk '{ printf $NF"\t"; $NF=""; print $0 }' | #reorder (interface first)
sed 's/[[:space:]]$//g' | #remove trailing space
sed 's/\(.*\) /\1 /g' || #add tab between make and model
# bsd
pciconf -lv |
grep -A 2 "class=0x020000" | #only network devices
tr -s "\n" " " | #replace new lines with spaces
awk -F' -- ' '{$1=$1}1' OFS='\n' | #replace -- with new lines
sed 's/ device = //g' | #remove label
sed 's/@.*= //g' | #remove pci info
tr -s "'" "\t" || #replace quotes with tab
# linux (lspci)
( while read -r interface; do
echo "$(
lspci -Dm -d "::0200" | #lists network pci devices
awk -vFPAT='([^ ]*)|("[^"]+")' '{ print $1,$3,$4 }' )" | #parse pci domain, make and model
sed "s/$( echo "${interface}" |
awk '{ print $1}' #replace pci domain
)/$( echo "${interface}" |
awk '{ print $2}' #with interface name
)/" |
grep "$( echo "${interface}" |
awk '{ print $2}' #only print lines that have been substituted
)"
done < <( ls -l /sys/class/net |
grep "$( lspci -Dm -d "::0200" |
awk '{print $1 }' #search network interfaces for pci address of nics
)" |
awk -F/ '{ print $(NF-2),$NF }' #parse pci address and interface name
) |
sed 's/ "/"/g' | #clean up extraneous space
tr -s '"' '\t' #add tabs
)
) 2>/dev/null ||
exit 1 #failure
exit 0 #success
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment