Hey guys, I want to use a a quick bash script/command to determine what network interface is connected to the internet so I can pipe it out to become a variable, in order so the user does not have to manually type it in each time or have to 'hardcode' the variable into the script.
I know about checking ifconfig etc of course, just looking for another way.
Many thanks and apologies for the kinda double post!
For LINUX, if you don't have to know the IP addresses of the interfaces, but just to enquire the status, mii-tool / ethtool also work. These commands might require root privilege to execute.
ip route get 8.8.8.8
8.8.8.8 via 192.168.10.1 dev eth0 src 192.168.10.33
cache
This tests what route is used to connect to a public IP, like this Google DNS IP
gawk version (rs limitation)
ip route get 8.8.8.8 | awk 'NR==2 {print $1}' RS="dev"
eth0
You need to find the word after dev, and this can vary on the line, depending if computer is behind a firewall/router, or connected directly on the net.
EDIT: a more portable version
ip route get 8.8.8.8 | awk -F"dev " 'NR==1 {split($2,a," ");print a[1]}'