Skip to content

Instantly share code, notes, and snippets.

@carlashley
Created February 6, 2017 22:22
Show Gist options
  • Save carlashley/5c64731cbcc479adb8be15d7fc895394 to your computer and use it in GitHub Desktop.
Save carlashley/5c64731cbcc479adb8be15d7fc895394 to your computer and use it in GitHub Desktop.
Get the active interface for a given route
#!/bin/sh
# Get network interface. We sleep so that the interface has a chance to come up!
# Substitute the 0.0.0.0 address if you have a specific route that you want to test, however this may impact
# portability if a machine spends more time without access to that route than with.
# Give the interface a chance to come up - useful if this is used in any boot up processes
while [ $(route get 0.0.0.0 2>/dev/null | grep -c interface) != 1 ]; do
sleep 5
done
# Actual interface
interface=$(route get 0.0.0.0 2>/dev/null | awk '/interface: / {print $2}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment