Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $ networksetup -listallnetworkservices
- An asterisk (*) denotes that a network service is disabled.
- Ethernet
- FireWire
- Wi-Fi
- $ networksetup -listnetworkserviceorder
- An asterisk (*) denotes that a network service is disabled.
- (1) Ethernet
- (Hardware Port: Ethernet, Device: en0)
- (2) FireWire
- (Hardware Port: FireWire, Device: fw0)
- (3) Wi-Fi
- (Hardware Port: Wi-Fi, Device: en1)
- ifconfig
- #!/bin/bash
- services=$(networksetup -listnetworkserviceorder | grep 'Hardware Port')
- while read line; do
- sname=$(echo $line | awk -F "(, )|(: )|[)]" '{print $2}')
- sdev=$(echo $line | awk -F "(, )|(: )|[)]" '{print $4}')
- #echo "Current service: $sname, $sdev, $currentservice"
- if [ -n "$sdev" ]; then
- ifout="$(ifconfig $sdev 2>/dev/null)"
- echo "$ifout" | grep 'status: active' > /dev/null 2>&1
- rc="$?"
- if [ "$rc" -eq 0 ]; then
- currentservice="$sname"
- currentdevice="$sdev"
- currentmac=$(echo "$ifout" | awk '/ether/{print $2}')
- fi
- fi
- done <<< "$(echo "$services")"
- if [ -n "$currentservice" ]; then
- echo $currentservice
- echo $currentdevice
- echo $currentmac
- else
- >&2 echo "Could not find current service"
- exit 1
- fi
- $ sh networkservice.sh
- Wi-Fi
- en0
- 78:31:c1:c2:d7:6c
- $ route get example.com | grep interface
- interface: en8
- $ networksetup -listnetworkserviceorder | grep en8
- (Hardware Port: Broadcom NetXtreme Gigabit Ethernet Controller, Device: en8)
- set public (dig +short myip.opendns.com @resolver1.opendns.com)
- defaults read /Library/Preferences/SystemConfiguration/com.apple.airport.preferences |grep LastConnected -A 7
- function netinfo -d "get network information"
- # Get public ip address
- set public (dig +short myip.opendns.com @resolver1.opendns.com)
- set hostname (uname -n)
- if test -z "$public" # We got an empty string, meaning:
- set public "No Internet connection available"
- end
- echo ''
- echo " Public IP: $public"
- echo " Hostname: $hostname"
- echo ''
- # Get all available hardware ports
- set ports (ifconfig -uv | grep '^[a-z0-9]' | awk -F : '{print $1}')
- # Get for all available hardware ports their status
- for val in $ports
- set activated (ifconfig -uv $val | grep 'status: ' | awk '{print $2}')
- # We want information about active network ports...
- if test $activated = 'active' ^/dev/null
- set ipaddress (ifconfig -uv $val | grep 'inet ' | awk '{print $2}')
- # and of these, the ones with an IP-address assigned to it
- if test -n "$ipaddress" ^/dev/null
- # Do we have an IP address?
- # Then give us the information
- set label (ifconfig -uv $val | grep 'type' | awk '{print $2}')
- set macaddress (ifconfig -uv $val | grep 'ether ' | awk '{print $2}')
- set quality (ifconfig -uv $val | grep 'link quality:' | awk '{print $3, $4}')
- set netmask (ipconfig getpacket $val | grep 'subnet_mask (ip):' | awk '{print $3}')
- set router (ipconfig getpacket $val | grep 'router (ip_mult):' | sed 's/.*router (ip_mult): {([^}]*)}.*/1/')
- set dnsserver (ipconfig getpacket $val | grep 'domain_name_server (ip_mult):' | sed 's/.*domain_name_server (ip_mult): {([^}]*)}.*/1/')
- # Header for the network interfaces
- echo -n $label ; echo -n ' ('; echo -n $val ; echo ')'
- echo "--------------"
- # Is this a WiFi associated port? If so, then we want the network name
- switch $label
- case Wi-Fi
- # Get WiFi network name
- set wifi_name (/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I | grep 'sSSID:' | sed 's/.*: //')
- echo " Network Name: $wifi_name"
- # Networkspeed for Wi-Fi
- set networkspeed (/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I | grep lastTxRate: | sed 's/.*: //' | sed 's/$/ Mbps/')
- case '*'
- # Networkspeed for other ports
- set networkspeed (ifconfig -uv $val | grep 'link rate:' | awk '{print $3, $4}')
- end
- echo " IP-address: $ipaddress"
- echo " Subnet Mask: $netmask"
- echo " Router: $router"
- echo " DNS Server: $dnsserver"
- echo " MAC-address: $macaddress"
- echo "Network Speed: $networkspeed"
- echo " Link quality: $quality"
- echo ''
- end
- # Don't display the inactive ports.
- else if test $activated = 'inactive' ^/dev/null
- end
- end
- end
Add Comment
Please, Sign In to add comment