├── network-info.png ├── README.md ├── LICENSE └── network-info.sh /network-info.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Peter-Moller/network-info/HEAD/network-info.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # network-info 2 | A bash script for OS X that details information about the network 3 | 4 | ![Screendump of network_info](network-info.png) 5 | 6 | ----- 7 | 8 | Overview of how the script works: 9 | --------------------------------- 10 | 11 | Data is gathered using the commands `ifconfig`, `networksetup`, `airport` and `scutil`. The network interfaces that the computer knows about is presented in the order they are used. `scutil` is used for VPN, but the solutions is not entirely satisfying (since VPN:s aren't easily placed in a priority chain). `airport` is used to get info on the wireless interface. With the `-w` option, only the wireless network information is presented. 12 | 13 | Default interface is found with `netstat -rn` 14 | 15 | One of the more useful parts of the script is the “Quality” assessment of the wireless network. This is calculated by subtract the noice level from the signal strength and then apply the following labels to the difference: 16 | 17 | | Difference | Assessment | 18 | |------------|------------| 19 | | \>30: | Excellent | 20 | | 20-30: | Good | 21 | | 10-20: | Poor | 22 | | <10: | Unusable | 23 | 24 | These values are assigned by me through experience and they seems to be reasonable. 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /network-info.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # Script to list information about network interfaces on Mac OS X [10.9] 4 | # 5 | # Copyright 2015 Peter Möller 6 | # 7 | # Licensed under the Apache License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | # 19 | # Version: 20 | VER="1.6" 21 | 22 | help() { 23 | echo 24 | echo "Usage: $0 [-w]" 25 | echo 26 | echo "-w: Only show the wireless information." 27 | echo 28 | echo "If run by root: datafiles in /Library/cs.lth.se/OpenPorts (Mac) or /usr/share/cs.lth.se/OpenPorts (Linux) are created, but no output." 29 | echo "If run by any other user: output is displayed based on those datafiles." 30 | echo 31 | echo "This script is supposed to be used in conjunction with a launchd-component, se.lth.cs.open_ports," 32 | echo "that creates the datafiles in /Library/OpenPorts every two minutes. The use of GeekTool to display the result" 33 | echo "is also part of the idea behind this script!" 34 | exit 0 35 | } 36 | 37 | # Read the parameters: 38 | while getopts ":hwd" opt; do 39 | case $opt in 40 | w ) WiFiOnly=t;; 41 | d ) debug=t;; 42 | \?|h ) help;; 43 | esac 44 | done 45 | 46 | 47 | # Find where the script resides. If it's a link, get *that* directory 48 | if [ -L "${BASH_SOURCE[0]}" ]; then 49 | DirName="$(dirname $(readlink ${BASH_SOURCE[0]}))" 50 | else 51 | DirName="$(dirname ${BASH_SOURCE[0]})" 52 | fi 53 | # What is the name of the script? 54 | ScriptName="$(basename ${BASH_SOURCE[0]})" 55 | 56 | 57 | 58 | # Basic settings: 59 | # PREFIX points to where the data files are stored. 60 | DEFAULT_INTERFACE="$(netstat -rn | egrep -A3 "Internet:" | egrep "^default" | head -1 | awk '{print $NF}')" 61 | MY_IP_ADDRESS="$(ifconfig $DEFAULT_INTERFACE | grep "inet " | awk '{ print $2 }')" 62 | #DOMAIN="`ipconfig getpacket en0 | grep 'domain_name (string)' | awk '{ print $3 }'`" 63 | DOMAIN="$(hostname | cut -d\. -f2-7)" 64 | MTIME60m="-mtime -60m" 65 | MTIME120m="-mtime +120m" 66 | MTIME7d="-mtime -7d" 67 | # NAT has content if we are on a private net (^192.168.|^172.16.|^10.) and empty otherwise 68 | NAT="$(echo $MY_IP_ADDRESS | egrep "^192.168.|^172.16.|^10.")" 69 | 70 | # (Colors can be found at http://en.wikipedia.org/wiki/ANSI_escape_code, http://graphcomp.com/info/specs/ansi_col.html and other sites) 71 | Reset="\e[0m" 72 | ESC="\e[" 73 | RES="0" 74 | BoldFace="1" 75 | ItalicFace="3" 76 | UnderlineFace="4" 77 | SlowBlink="5" 78 | 79 | BlackBack="40" 80 | RedBack="41" 81 | GreenBack="42" 82 | YellowBack="43" 83 | BlueBack="44" 84 | CyanBack="46" 85 | WhiteBack="47" 86 | 87 | BlackFont="30" 88 | RedFont="31" 89 | GreenFont="32" 90 | YellowFont="33" 91 | BlueFont="34" 92 | CyanFont="36" 93 | WhiteFont="37" 94 | 95 | # Colors 96 | ActiveTextColor="$GreenFont" 97 | ActiveBackColor="$GreenBack" 98 | InactiveTextColor="$BlackFont" 99 | InactiveBackColor="$WhiteBack" 100 | DisabledTextColor="$WhiteFont" 101 | DisabledBackColor="$WhiteBack" 102 | 103 | # Reset all colors 104 | BGColor="$RES" 105 | Face="$RES" 106 | FontColor="$RES" 107 | 108 | 109 | # ---------------------------------------------------------------------------------------------------- 110 | # Print warnings 111 | 112 | # If we don't have an IP-address ($DEFAULT_INTERFACE = "") warn the user!! 113 | if [ -z "$DEFAULT_INTERFACE" ]; then 114 | printf "${ESC}${RedFont}mWARNING: No IP-address detected!!!\n\n$Reset" 115 | fi 116 | 117 | # Find out if is IPv6 is configured 118 | if [ -z "$(/sbin/ifconfig $DEFAULT_INTERFACE | grep inet6)" ]; then 119 | IPv6="f" 120 | else 121 | IPv6="t" 122 | fi 123 | 124 | # End print warnings 125 | # ---------------------------------------------------------------------------------------------------- 126 | 127 | 128 | # Prepare what is to be presented regarding the IP-address (if we are behind a NAT it should be prented) 129 | #if [ "$(less $EXTERN)" = "$MY_IP_ADDRESS" ]; then 130 | # IP_ADDRESS_Display="$MY_IP_ADDRESS" 131 | #else 132 | # IP_ADDRESS_Display="NAT: $MY_IP_ADDRESS / $(less $EXTERN)" 133 | #fi 134 | 135 | # Find out which system version we are running 136 | SW_VERS="$(sw_vers -productName) $(sw_vers -productVersion)" 137 | ComputerName="$(networksetup -getcomputername)" 138 | 139 | # Find out if it's a server 140 | # First step: does the name fromsw_vers include "server"? 141 | if [ -z "$(echo "$SW_VERS" | grep -i server)" ]; then 142 | # If not, it may still be a server. Beginning with OS X 10.8 all versions include the command serverinfo: 143 | serverinfo --software 1>/dev/null 144 | # Exit code 0 = server; 1 = NOT server 145 | ServSoft=$? 146 | if [ $ServSoft -eq 0 ]; then 147 | # Is it configured? 148 | serverinfo --configured 1>/dev/null 149 | ServConfigured=$? 150 | if [ $ServConfigured -eq 0 ]; then 151 | SW_VERS="$SW_VERS ($(serverinfo --productname) $(serverinfo --shortversion))" 152 | else 153 | SW_VERS="$SW_VERS ($(serverinfo --productname) $(serverinfo --shortversion) - unconfigured)" 154 | fi 155 | fi 156 | fi 157 | 158 | # Interesting options to networksetup: 159 | # 160 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 161 | # 162 | # $ networksetup -listnetworkserviceorder 163 | # An asterisk (*) denotes that a network service is disabled. 164 | # (1) Thunderbolt Ethernet 165 | # (Hardware Port: Thunderbolt Ethernet, Device: en1) 166 | # 167 | # (2) Wi-Fi 168 | # (Hardware Port: Wi-Fi, Device: en0) 169 | # 170 | # (3) Bluetooth DUN 171 | # (Hardware Port: Bluetooth DUN, Device: Bluetooth-Modem) 172 | 173 | # (*) USB Ethernet 174 | # (Hardware Port: USB-Ethernet, Device: en5) 175 | # 176 | # (4) Bluetooth PAN 177 | # (Hardware Port: Bluetooth PAN, Device: en2) 178 | # 179 | # (5) Thunderbolt Bridge 180 | # (Hardware Port: Thunderbolt Bridge, Device: bridge0) 181 | # 182 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 183 | # 184 | # $ networksetup -listallhardwareports 185 | # 186 | # Hardware Port: Bluetooth DUN 187 | # Device: Bluetooth-Modem 188 | # Ethernet Address: N/A 189 | # 190 | # Hardware Port: Thunderbolt Ethernet 191 | # Device: en1 192 | # Ethernet Address: 40:6c:8f:3d:6b:b3 193 | # 194 | # Hardware Port: Wi-Fi 195 | # Device: en0 196 | # Ethernet Address: 14:10:9f:ce:fd:95 197 | # 198 | # Hardware Port: Bluetooth PAN 199 | # Device: en2 200 | # Ethernet Address: N/A 201 | # 202 | # Hardware Port: Thunderbolt 1 203 | # Device: en3 204 | # Ethernet Address: 32:00:19:a6:f6:20 205 | # 206 | # Hardware Port: Thunderbolt 2 207 | # Device: en4 208 | # Ethernet Address: 32:00:19:a6:f6:21 209 | # 210 | # Hardware Port: Thunderbolt Bridge 211 | # Device: bridge0 212 | # Ethernet Address: N/A 213 | # 214 | # VLAN Configurations 215 | # =================== 216 | # 217 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 218 | # 219 | # $ networksetup -getinfo "Ethernet 1" 220 | # DHCP Configuration 221 | # IP address: 130.235.16.211 222 | # Subnet mask: 255.255.254.0 223 | # Router: 130.235.16.1 224 | # Client ID: 225 | # IPv6: Automatic 226 | # IPv6 IP address: none 227 | # IPv6 Router: none 228 | # Ethernet Address: 00:3e:e1:be:06:59 229 | # 230 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 231 | # 232 | # $ scutil --nc status 'VPN (L2TP) mot CS' 233 | # Connected 234 | # Extended Status { 235 | # IPv4 : { 236 | # Addresses : { 237 | # 0 : 130.235.16.35 238 | # } 239 | # DestAddresses : { 240 | # 0 : 130.235.16.20 241 | # } 242 | # InterfaceName : ppp0 243 | # NetworkSignature : VPN.RemoteAddress=130.235.16.20 244 | # Router : 130.235.16.20 245 | # ServerAddress : 130.235.16.20 246 | # } 247 | # PPP : { 248 | # CommRemoteAddress : 130.235.16.20 249 | # ConnectTime : 213787 250 | # IPCPCompressionVJ : 0 251 | # LCPCompressionACField : 1 252 | # LCPCompressionPField : 1 253 | # LCPMRU : 1500 254 | # LCPMTU : 1280 255 | # Status : 8 256 | # } 257 | # Status : 2 258 | # } 259 | # 260 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 261 | # 262 | # $ scutil --nc status "Thunderbolt Ethernet" 263 | # No service 264 | # 265 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 266 | # 267 | # 268 | # What I am looking for to print: 269 | # 270 | # The usual head + networksetup -getcomputername (gives "Peters MBPr") 271 | # Priority order Hardware Port Interface IPv4-address Status/Dynamic Subnet mask Router MAC-address Media Speed 272 | # 1. Thunderbolt Ethernet en1 192.168.1.114 DHCP 255.255.255.0 192.168.1.1 40:6c:8f:3d:6b:b3 1000 273 | # 4 22 9 17 8 17 17 19 5 274 | # | | | | | | | | | 275 | # 1. Thunderbolt Ethernet en1 192.168.173.208 Static 255.255.255.128 192.168.173.1 XX:XX:XX:XX:XX:XX 276 | # 12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 277 | # 1 2 3 4 5 6 7 8 9 10 11 12 13 14 278 | FormatStringInterfaces="%-4s%-22s%-17s%-17s%-15s%-17s%-17s%-19s%-5s" 279 | 280 | 281 | 282 | # Print the head 283 | printf "${ESC}${BlackBack};${WhiteFont}mHostname:${ESC}${WhiteBack};${BlackFont}m $(hostname) ${Reset} ${ESC}${BlackBack};${WhiteFont}mComputer Name:${ESC}${WhiteBack};${BlackFont}m ${ComputerName} ${Reset} ${ESC}${BlackBack};${WhiteFont}mRunning:${ESC}${WhiteBack};${BlackFont}m $SW_VERS ${Reset} ${ESC}${BlackBack};${WhiteFont}mDate & time:${ESC}${WhiteBack};${BlackFont}m $(date +%F", "%R) ${Reset}\n" 284 | echo 285 | 286 | # Print the wired network info only if WiFiOnly ≠ t 287 | if [ ! "$WiFiOnly" = "t" ]; then 288 | # Get the info 289 | NetworkInfoFile="/tmp/NetworkInterfaces_$$.txt" 290 | # networksetup -listnetworkserviceorder | grep -A 1 "^([0-9])\ " | grep "[a-z][0-9])$" | cut -d: -f2,3 | sed -e 's/, Device//g' -e 's/)//g' -e 's/^ //g' > $NSOfile 291 | networksetup -listnetworkserviceorder | egrep "^\([0-9\*]*\)\ " | sed -e 's/^(//g' -e 's/) /:/' | egrep -v "\*" > $NetworkInfoFile 292 | # Example: 293 | # 1:Bluetooth DUN 294 | # 2:Ethernet 1 295 | # 3:Ethernet 2 296 | # 4:Display Ethernet 297 | # 5:Display FireWire 298 | # 6:Wi-Fi 299 | # *:Bluetooth PAN 300 | # 7:Thunderbolt Bridge 301 | 302 | # Print the title line 303 | printf "${ESC}${ItalicFace}mExplanation: ${ESC}${ActiveTextColor};${ItalicFace}mActive interface${Reset}, ${ESC}${InactiveTextColor};${ItalicFace}mInactive interface${Reset}, ${ESC}${DisabledTextColor};${ItalicFace}mDisabled interface${Reset}\n" 304 | echo 305 | printf "${ESC}${UnderlineFace};${YellowFont}m${FormatStringInterfaces}${Reset}\n" "#" "Hardware Port" "Interface" "IPv4-address" "Config." "Subnet mask" "Router" "MAC-address" "Media Speed" 306 | 307 | # Read the file and print the output 308 | exec 4<"$NetworkInfoFile" 309 | while IFS=: read -u 4 IFNum IFName 310 | do 311 | # Determine of a VPN connection is active: 312 | # Sample output from the command: '* (Connected) 199F727B-C91D-4A6E-8A0B-4745179895F2 IPSec "VPN (Cisco IPSec)" [IPSec]' 313 | if [ -n "$(scutil --nc list | egrep "Connected")" -a "$IFName" = "$(scutil --nc list | egrep "Connected" | cut -d\" -f2)" ]; then 314 | Interface="$(netstat -rn | egrep -A3 "Internet:" | egrep default | head -1 | awk '{print $NF}')" 315 | # Ex: 'utun5' 316 | MediaSpeed="Unknown" 317 | Configuration="Unknown" 318 | IPaddress="$(ifconfig $Interface | egrep "\-\->" | awk '{print $4}')" 319 | # Ex: '10.190.1.1' 320 | SubnetMaskTemp="$(ifconfig $Interface | egrep "\-\->" | awk '{print $NF}' | sed -e 's/^0x//')" 321 | # Ex: 'ffffffff' 322 | SubNetP1="$(echo "ibase=16; $(echo $SubnetMaskTemp | cut -c1-2 | tr '[:lower:]' '[:upper:]')" | bc)" 323 | SubNetP2="$(echo "ibase=16; $(echo $SubnetMaskTemp | cut -c3-4 | tr '[:lower:]' '[:upper:]')" | bc)" 324 | SubNetP3="$(echo "ibase=16; $(echo $SubnetMaskTemp | cut -c5-6 | tr '[:lower:]' '[:upper:]')" | bc)" 325 | SubNetP4="$(echo "ibase=16; $(echo $SubnetMaskTemp | cut -c7-8 | tr '[:lower:]' '[:upper:]')" | bc)" 326 | SubnetMask="${SubNetP1}.${SubNetP2}.${SubNetP3}.${SubNetP4}" 327 | # Ex: '255.255.255.255' 328 | Router="Unknown" 329 | # Ex: " 130.235.16.1" 330 | #MACaddress="$(networksetup -getinfo "$IFName" | grep "^Ethernet Address" | cut -d: -f2-7)" 331 | MACaddress="Unknown" 332 | # Ex: " 00:3e:e1:be:06:59" 333 | Status="$(scutil --nc status "$IFName" | head -1)" 334 | # Ex: 'Connected' 335 | else 336 | Interface="$(networksetup -listallhardwareports 2>/dev/null | grep -A1 "Hardware Port: $IFName" | tail -1 | awk '{print $2}')" 337 | # Ex: en0 338 | MediaSpeed="$(networksetup -getMedia "$IFName" 2>/dev/null | grep "^Active" | cut -d: -f2-)" 339 | # Ex: "1000baseT" or "autoselect" 340 | Configuration="$(networksetup -getinfo "$IFName" 2>/dev/null | grep Configuration | awk '{print $1}')" 341 | # Ex: "DHCP" or "Manual" 342 | IPaddress="$(networksetup -getinfo "$IFName" 2>/dev/null | grep "^IP address" | cut -d: -f2)" 343 | # Ex: " 130.235.16.211" 344 | SubnetMask="$(networksetup -getinfo "$IFName" 2>/dev/null | grep "^Subnet mask" | cut -d: -f2)" 345 | # Ex: " 255.255.254.0" 346 | Router="$(networksetup -getinfo "$IFName" 2>/dev/null | grep "^Router" | cut -d: -f2)" 347 | # Ex: " 130.235.16.1" 348 | #MACaddress="$(networksetup -getinfo "$IFName" | grep "^Ethernet Address" | cut -d: -f2-7)" 349 | MACaddress="$(ifconfig "$Interface" 2>/dev/null | grep "ether\ " | awk '{print $2}')" 350 | # Ex: " 00:3e:e1:be:06:59" 351 | Status="$(ifconfig "$Interface" 2>/dev/null | grep "status:\ " | awk '{print $2}')" 352 | # Ex: "active" 353 | fi 354 | 355 | # Set colors for printing) 356 | if [ "$IFNum" = "*" ]; then 357 | TextColor="${DisabledTextColor}" 358 | elif [ "$Status" = "active" -o "$Status" = "Connected" ]; then 359 | TextColor="${ActiveTextColor}" 360 | [ "$IFName" = "Wi-Fi" ] && IFName="Wi-Fi (details below)" 361 | elif [ ! "$Status" = "active" ]; then 362 | TextColor="${InactiveTextColor}" 363 | fi 364 | printf "${ESC}${TextColor}m${FormatStringInterfaces}${Reset}\n" "$IFNum" "$IFName" "$Interface" "${IPaddress# }" "$Configuration" "${SubnetMask# }" "${Router# }" "${MACaddress# }" "${MediaSpeed}" 365 | done 366 | echo 367 | # Clean up the $NSOfile 368 | /bin/rm "$NetworkInfoFile" 2>/dev/null 369 | 370 | fi 371 | 372 | # Print extra information about WiFi - if it is configured 373 | # Get data with: 374 | # /System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -I 375 | # Sample output: 376 | # agrCtlRSSI: -51 <-- Interesting 377 | # agrExtRSSI: 0 378 | # agrCtlNoise: -87 <-- Interesting 379 | # agrExtNoise: 0 380 | # state: running 381 | # op mode: station 382 | # lastTxRate: 104 383 | # maxRate: 144 <-- Interesting 384 | # lastAssocStatus: 0 385 | # 802.11 auth: open 386 | # link auth: wpa2-psk <-- Interesting 387 | # BSSID: 58:98:35:37:8d:81 388 | # SSID: WppW <-- Interesting 389 | # MCS: 13 390 | # channel: 1 <-- Interesting 391 | # 392 | # Values for signal strength and noice: 393 | # Signal strength: -60: 5 bars 394 | # 395 | # Signal Strength (*not* RSSI!!!): 396 | # -80 dBm equals 10 pW of received radio power. 397 | # -70 dBm equals 100 pW. This is 10 times stronger than -80 dBm. 398 | # -60 dBm equals 1000 pW, or 1 nW. This is 10 times stronger than -70 dBm. 399 | # -50 dBm equals 10000 pW, or 10 nW, or 10^-5 mW, or 0.00000001 W or 10^-8 W 400 | # -40 dBm equals 100 nW, or 0.0001 mW, or 10^-7 W 401 | # -30 dBm equals 1000 nW, or 0.001 mW, or 10^6 W, 0.000001 W 402 | # -20 dBm equals 0.01 mW, or 10^5 W, or 0.00001 W 403 | # -10 dBm equals 0.1 mW, 10^4 W, or 0.0001 W 404 | # 405 | # My own notices about RSSI: 406 | # -73: still good (5 bars) 407 | # -33: just next to the base station 408 | # 409 | # Noice Level: -82: low? Should be 20 dB lover than the signal 410 | # From: http://www.noah.org/wiki/WiFi_notes 411 | # The Signal to Noise Ratio (SNR) in WiFi can be characterized by the following values: 412 | # >40 dB excellent signal; almost always full speed. 413 | # 25 - 40 dB good signal; usually full speed, but may sometimes drop to lower speed. 414 | # 15 - 25 dB moderate signal; fast, but not always full speed. 415 | # 10 - 15 dB lowest useful signal; slow data speeds; may sometime loose association. 416 | # <10 dB AP may be detectable, but rarely useful signal; rarely maintains association. 417 | 418 | # First, determine if WiFi is active: 419 | WiFiDevice="$(networksetup -listallhardwareports | grep -A1 "^Hardware Port: Wi-Fi" | tail -1 | awk '{print $NF}')" # Ex: WiFiDevice=en1 420 | NetworkPower="$(networksetup -getairportpower $WiFiDevice | awk '{print $NF}')" # Ex: NetworkPower=On 421 | if [ "$NetworkPower" = "On" ]; then 422 | # See if we should use the old system: 423 | AP="$(/System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -I)" 424 | if [ -n "$(echo "$AP" | grep "AirPort: Off")" ]; then 425 | # Gather information 426 | SSID="$(echo "$AP" | grep "\ SSID" | cut -d: -f2- | sed 's/ //')" # SSID=WppW 427 | Auth="$(echo "$AP" | grep "link auth" | awk '{print $3}' | tr '[:lower:]' '[:upper:]')" # Auth=WPA2-PSK 428 | MaxRate="$(echo "$AP" | grep "maxRate" | awk '{print $2}') Mbps" # MaxRate='144 Mbps' 429 | SignalStrength="$(echo "$AP" | grep "agrCtlRSSI" | awk '{print $2}')" # SignalStrength=-44 430 | Noice="$(echo "$AP" | grep "agrCtlNoise" | awk '{print $2}')" # Noice=-92 431 | Channel="$(echo "$AP" | grep "channel" | awk '{print $2}')" # Channel=1 432 | [ "${Channel%%,*}" -gt 15 ] && Frequency="5" || Frequency="2.4" 433 | # Else, use the modern approach 434 | elif [ -n "$(echo "$AP" | grep "WARNING: The airport command line tool is deprecated")" ]; then 435 | # Use 'system_profiler' to get WiFi details: 436 | # But only if 'jq' is present 437 | if type -p jq &>/dev/null; then 438 | printf "... investigating WiFi...\r" 439 | # WiFiDetails="$(system_profiler -json SPAirPortDataType basic | jq --arg iface "$WiFiDevice" '.SPAirPortDataType[].spairport_airport_interfaces[] | select(._name == $iface) | .spairport_current_network_information')" 440 | WiFiDetails="$(system_profiler -json SPAirPortDataType basic | jq -r --arg iface "$WiFiDevice" '.SPAirPortDataType[].spairport_airport_interfaces[]? | select(._name == $iface) | .spairport_current_network_information // empty')" 441 | printf "%25s" " " # Print 25 spaces to overwrite the previous text. Must add a '\r' later! 442 | # Ex: WiFiDetails='{ 443 | # "_name": "eduroam", 444 | # "spairport_network_channel": "52 (5GHz, 40MHz)", 445 | # "spairport_network_country_code": "SE", 446 | # "spairport_network_mcs": 9, 447 | # "spairport_network_phymode": "802.11ac", 448 | # "spairport_network_rate": 400, 449 | # "spairport_network_type": "spairport_network_type_station", 450 | # "spairport_security_mode": "spairport_security_mode_wpa2_enterprise", 451 | # "spairport_signal_noise": "-66 dBm / -97 dBm" 452 | # }' 453 | SSID="$(echo "$WiFiDetails" | jq -r '._name')" # Ex: SSID=eduroam 454 | Auth="$(echo "$WiFiDetails"|jq -r '.spairport_security_mode'|rev|cut -d'_' -f1-2|rev|sed 's/_/ /;s/wpa/WPA/')" # Ex: Auth='WPA2 enterprise' 455 | MaxRate="$(echo "$WiFiDetails" | jq -r '.spairport_network_rate')" # Ex: MaxRate=400 456 | SignalStrength="$(echo "$WiFiDetails" | jq -r '.spairport_signal_noise' | awk '{print $1}')" # Ex: SignalStrength=-66 457 | Noice="$(echo "$WiFiDetails" | jq -r '.spairport_signal_noise' | cut -d\/ -f2 | awk '{print $1}')" # Ex: Noice=-97 458 | Channel="$(echo "$WiFiDetails" | jq -r '.spairport_network_channel' | awk '{print $1}')" # Ex: Channel=52 459 | Frequency="$(echo "$WiFiDetails" | jq -r '.spairport_network_channel' | cut -d\( -f2 | grep -Eo "^[0-9.]")" # Ex: Frequency=5 460 | else 461 | # OK, so then we will use 'wdutil' instead 462 | ### Note 1: requires 'sudo' 463 | ### Note 2: will NOT reveal SSID! 464 | if [ "$EUID" -eq 0 ]; then 465 | WDINFO="$(wdutil info | awk '/^WIFI/,/^BLUETOOTH/' | grep -E "^\s*RSSI\s*: |^\s*Noise\s*: |^\s*Tx Rate\s*: |^\s*Security\s*: |^\s*Channel\s*: |^\s*SSID\s*: ")" 466 | # WDINFO=' SSID : 467 | # RSSI : -64 dBm 468 | # Noise : -99 dBm 469 | # Tx Rate : 400.0 Mbps 470 | # Security : WPA2 Enterprise 471 | # Channel : 5g52/40' 472 | SSID="$(echo "$WDINFO" | grep "\ SSID" | cut -d: -f2- | sed 's/ //')" # SSID='' 473 | Auth="$(echo "$WDINFO" | grep "Security" | cut -d: -f2- | sed 's/ //')" # Auth='WPA2 Enterprise' 474 | MaxRate="$(echo "$WDINFO" | grep "Tx Rate" | cut -d: -f2- | sed 's/ //')" # MaxRate='400.0 Mbps' 475 | SignalStrength="$(echo "$WDINFO" | grep "RSSI" | awk '{print $3}')" # SignalStrength=-64 476 | Noice="$(echo "$WDINFO" | grep "Noise" | awk '{print $3}')" # Noice=-99 477 | Channel="$(echo "$WDINFO" | grep "Channel" | cut -d\/ -f2)" # Channel=40 478 | if [ "$(echo "$WDINFO" | grep "Channel" | cut -d: -f2 | cut -c2-3)" = "5g" ]; then 479 | Frequency="5" 480 | else 481 | Frequency="2.4" 482 | fi 483 | else 484 | echo "Command 'jq' not present and you are not running with superuser priviledges (needed by 'wdutil')" 485 | echo "So we cant displayu any information about WiFi. Sorry!" 486 | echo "(install 'jq' and try again!)" 487 | exit 0 488 | fi 489 | fi 490 | fi 491 | 492 | # Calculate the Signal to Noice Ration and set a text for it 493 | SNR="$(expr ${SignalStrength:-0} - ${Noice:-0})" 494 | if [ $SNR -gt 30 ]; then 495 | SNRText="Excellent" 496 | elif [ $SNR -gt 20 ]; then 497 | SNRText="Good" 498 | elif [ $SNR -gt 10 ]; then 499 | SNRText="Poor" 500 | else 501 | SNRText="Unusable" 502 | fi 503 | 504 | 505 | # Prepare the text blocks (it's easier this way) 506 | HeadBack="$BlueBack" 507 | HeadText="$WhiteFont" 508 | DataBack="$WhiteBack" 509 | DataText="$BlueFont" 510 | SSID_block="${ESC}${HeadBack};${HeadText}mSSID:${Reset}${ESC}${DataBack};${DataText}m ${SSID:- --no SSID chosen--} ${Reset} " 511 | Auth_block="${ESC}${HeadBack};${HeadText}mAuth:${Reset}${ESC}${DataBack};${DataText}m ${Auth} ${Reset} " 512 | Max_block="${ESC}${HeadBack};${HeadText}mMax Rate:${Reset}${ESC}${DataBack};${DataText}m ${MaxRate} ${Reset} " 513 | Signal_block="${ESC}${HeadBack};${HeadText}mSignal Strength:${Reset}${ESC}${DataBack};${DataText}m ${SignalStrength} dB ${Reset} " 514 | Noice_block="${ESC}${HeadBack};${HeadText}mNoice:${Reset}${ESC}${DataBack};${DataText}m ${Noice} dB ${Reset} " 515 | QualityBlock="${ESC}${HeadBack};${HeadText}mQuality:${Reset}${ESC}${DataBack};${DataText}m ${SNRText} ${Reset} " 516 | Channel_block="${ESC}${HeadBack};${HeadText}mChannel:${Reset}${ESC}${DataBack};${DataText}m ${Channel} ${Reset} " 517 | Frequency_block="${ESC}${HeadBack};${HeadText}mFrequency:${Reset}${ESC}${DataBack};${DataText}m ${Frequency} GHz ${Reset} " 518 | 519 | # Print the information 520 | printf "${ESC}${WhiteFont};${BoldFace};${BlackBack}m\rWi-Fi details:${Reset} \n" 521 | printf "${SSID_block}${Auth_block}${Max_block}${Signal_block}${Noice_block}${QualityBlock}${Channel_block}${Frequency_block}\n" 522 | fi 523 | 524 | exit 0 525 | --------------------------------------------------------------------------------