Bash scrippet to extract all eth* addresses from /etc/network/interfaces
Posted 12-02-2014 at 08:03 AM by catkin
I looked for such a script on the 'net and did not find so here's a solution, tessted with the edge case of an address line at the end of the file:
Code:
#!/bin/bash eth_regex=^eth true=true false= address= have_iface_flag=$false while read -r word1 word2 word3 word4 _ do case $word1 in iface ) [[ $word2 =~ $eth_regex && $word4 != dhcp ]] \ && have_iface_flag=$true ;; address ) if [[ $have_iface_flag ]]; then eth_addresses+=($word2) have_iface_flag=$false fi ;; esac done < /etc/network/interfaces [[ $have_iface_flag && $address != '' ]] && eth_addresses+=($address) if [[ $eth_addresses != '' ]]; then for ((i=0;i<${#eth_addresses[*]};i++)) do echo "eth_addresses[$i]: ${eth_addresses[i]}" done fi
Total Comments 0