Guest User

Untitled

a guest
Nov 17th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. $ networksetup -listallnetworkservices
  2. An asterisk (*) denotes that a network service is disabled.
  3. Ethernet
  4. FireWire
  5. Wi-Fi
  6.  
  7. $ networksetup -listnetworkserviceorder
  8. An asterisk (*) denotes that a network service is disabled.
  9. (1) Ethernet
  10. (Hardware Port: Ethernet, Device: en0)
  11.  
  12. (2) FireWire
  13. (Hardware Port: FireWire, Device: fw0)
  14.  
  15. (3) Wi-Fi
  16. (Hardware Port: Wi-Fi, Device: en1)
  17.  
  18. ifconfig
  19.  
  20. #!/bin/bash
  21.  
  22. services=$(networksetup -listnetworkserviceorder | grep 'Hardware Port')
  23.  
  24. while read line; do
  25. sname=$(echo $line | awk -F "(, )|(: )|[)]" '{print $2}')
  26. sdev=$(echo $line | awk -F "(, )|(: )|[)]" '{print $4}')
  27. #echo "Current service: $sname, $sdev, $currentservice"
  28. if [ -n "$sdev" ]; then
  29. ifout="$(ifconfig $sdev 2>/dev/null)"
  30. echo "$ifout" | grep 'status: active' > /dev/null 2>&1
  31. rc="$?"
  32. if [ "$rc" -eq 0 ]; then
  33. currentservice="$sname"
  34. currentdevice="$sdev"
  35. currentmac=$(echo "$ifout" | awk '/ether/{print $2}')
  36. fi
  37. fi
  38. done <<< "$(echo "$services")"
  39.  
  40. if [ -n "$currentservice" ]; then
  41. echo $currentservice
  42. echo $currentdevice
  43. echo $currentmac
  44. else
  45. >&2 echo "Could not find current service"
  46. exit 1
  47. fi
  48.  
  49. $ sh networkservice.sh
  50. Wi-Fi
  51. en0
  52. 78:31:c1:c2:d7:6c
  53.  
  54. $ route get example.com | grep interface
  55. interface: en8
  56.  
  57. $ networksetup -listnetworkserviceorder | grep en8
  58. (Hardware Port: Broadcom NetXtreme Gigabit Ethernet Controller, Device: en8)
  59.  
  60. set public (dig +short myip.opendns.com @resolver1.opendns.com)
  61.  
  62. defaults read /Library/Preferences/SystemConfiguration/com.apple.airport.preferences |grep LastConnected -A 7
  63.  
  64. function netinfo -d "get network information"
  65.  
  66. # Get public ip address
  67. set public (dig +short myip.opendns.com @resolver1.opendns.com)
  68. set hostname (uname -n)
  69.  
  70. if test -z "$public" # We got an empty string, meaning:
  71. set public "No Internet connection available"
  72. end
  73.  
  74. echo ''
  75. echo " Public IP: $public"
  76. echo " Hostname: $hostname"
  77. echo ''
  78.  
  79. # Get all available hardware ports
  80. set ports (ifconfig -uv | grep '^[a-z0-9]' | awk -F : '{print $1}')
  81.  
  82. # Get for all available hardware ports their status
  83. for val in $ports
  84. set activated (ifconfig -uv $val | grep 'status: ' | awk '{print $2}')
  85.  
  86. # We want information about active network ports...
  87. if test $activated = 'active' ^/dev/null
  88. set ipaddress (ifconfig -uv $val | grep 'inet ' | awk '{print $2}')
  89.  
  90. # and of these, the ones with an IP-address assigned to it
  91. if test -n "$ipaddress" ^/dev/null
  92.  
  93. # Do we have an IP address?
  94. # Then give us the information
  95. set label (ifconfig -uv $val | grep 'type' | awk '{print $2}')
  96. set macaddress (ifconfig -uv $val | grep 'ether ' | awk '{print $2}')
  97. set quality (ifconfig -uv $val | grep 'link quality:' | awk '{print $3, $4}')
  98. set netmask (ipconfig getpacket $val | grep 'subnet_mask (ip):' | awk '{print $3}')
  99. set router (ipconfig getpacket $val | grep 'router (ip_mult):' | sed 's/.*router (ip_mult): {([^}]*)}.*/1/')
  100. set dnsserver (ipconfig getpacket $val | grep 'domain_name_server (ip_mult):' | sed 's/.*domain_name_server (ip_mult): {([^}]*)}.*/1/')
  101.  
  102. # Header for the network interfaces
  103. echo -n $label ; echo -n ' ('; echo -n $val ; echo ')'
  104. echo "--------------"
  105.  
  106. # Is this a WiFi associated port? If so, then we want the network name
  107. switch $label
  108. case Wi-Fi
  109. # Get WiFi network name
  110. set wifi_name (/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I | grep 'sSSID:' | sed 's/.*: //')
  111. echo " Network Name: $wifi_name"
  112. # Networkspeed for Wi-Fi
  113. set networkspeed (/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I | grep lastTxRate: | sed 's/.*: //' | sed 's/$/ Mbps/')
  114. case '*'
  115. # Networkspeed for other ports
  116. set networkspeed (ifconfig -uv $val | grep 'link rate:' | awk '{print $3, $4}')
  117. end
  118.  
  119. echo " IP-address: $ipaddress"
  120. echo " Subnet Mask: $netmask"
  121. echo " Router: $router"
  122. echo " DNS Server: $dnsserver"
  123. echo " MAC-address: $macaddress"
  124. echo "Network Speed: $networkspeed"
  125. echo " Link quality: $quality"
  126. echo ''
  127. end
  128.  
  129. # Don't display the inactive ports.
  130. else if test $activated = 'inactive' ^/dev/null
  131. end
  132. end
  133. end
Add Comment
Please, Sign In to add comment