Advertisement
Guest User

Network Addresses

a guest
Mar 22nd, 2012
543
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.48 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Get the list of network devices and their corresponding interfaces in
  4. # service order priority.  The sed commands at the end first extracts
  5. # the names of the devices and their corresponding interface names,
  6. # then removes illegal characters and replace them with "_".
  7. devlist="$(networksetup -listnetworkserviceorder | grep Hardware | \
  8.    sed -e 's/^(H.*rt: \(.*\), Device: \(.*\))/\1/' \
  9.        -e 's/[()\*#]//g' -e 's/[ -]/_/g')"
  10.  
  11. iflist="$(networksetup -listnetworkserviceorder | grep Hardware | \
  12.    sed -e 's/^(H.*rt: \(.*\), Device: \(.*\))/\1=\2/' \
  13.        -e 's/[()\*#]//g' -e 's/[ -]/_/g')"
  14.  
  15. for iface in ${iflist}; do
  16.     # skip FireWire and PPTP
  17.     if [ $iface != "FireWire=fw0" -a $iface != "PPTP=" ]; then
  18.         eval export ${iface}
  19.     fi
  20. done
  21.  
  22. RED_FG="\033[31m"
  23. NORMAL="\033[0m"
  24.  
  25. # The original tip could be implemented inside a for loop like this:
  26. # (the bang in the environment variable does an indirect lookup)
  27. for dev in ${devlist}; do
  28.     # skip FireWire and PPTP
  29.     if [ $dev != "FireWire" -a $dev != "PPTP" ]; then
  30.         echo -n "${dev} on ${!dev}: "
  31.         IP=$(ifconfig -m ${!dev} | grep "inet " | awk '{print $2'})
  32.         echo -e "${IP:-${RED_FG}INACTIVE${NORMAL}}"
  33.     fi
  34. done
  35.  
  36. TIMEOUT=10
  37.  
  38. EXTERNAL_IP=$(curl --connect-timeout $TIMEOUT -s http://checkip.dyndns.com/ \
  39.     | cut -d ':' -f2 | sed -e 's#</body></html>##g')
  40.  
  41. # external IP address
  42. echo External IP: ${EXTERNAL_IP:-${TIMEOUT} second timeout encountered}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement