Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Network status monitor for BitBar (https://getbitbar.com/)
#!/bin/bash
PING_TIMEOUT=2
PING_ADDRESS=8.8.8.8
PING_STATUS=false
EXTERNAL_IP_LABEL='IP Address (external): '
ACQUIRE_EXTERNAL_IP='/usr/local/bin/wget -qO- http://ipecho.net/plain'
EXTERNAL_IP_ADDRESS="$(eval "$ACQUIRE_EXTERNAL_IP")"
EXTERNAL_IP="${EXTERNAL_IP_LABEL}${EXTERNAL_IP_ADDRESS}"
VPN_STATUS=false
VPN_INTERFACE='utun1'
VPN_CONNECTED='/sbin/ifconfig | egrep -A1 $VPN_INTERFACE | grep inet'
DNSCRYPT_STATUS=false
DNS_CRYPT_TEST='dig debug.opendns.com txt | egrep -A1 "dnscrypt enabled"'
AD_BLOCK_STATUS=false
AD_BLOCK_TEST='curl -L -s -o /dev/null -I -w "%{http_code}" http://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js'
ACTIVE_ADAPTER=$(ifconfig | /usr/local/bin/pcregrep -M -o '^[^\t:]+:([^\n]|\n\t)*inet.*broadcast' | egrep -o -m 1 '^[^\t:]+')
INTERNAL_IP_ADDRESS="$(eval "ipconfig getifaddr "$ACTIVE_ADAPTER"")"
INTERNAL_IP_LABEL='IP Address (internal): '
INTERNAL_IP="${INTERNAL_IP_LABEL}${INTERNAL_IP_ADDRESS}"
FIREWALL_ENABLED_TEST=$(/usr/libexec/ApplicationFirewall/socketfilterfw --getglobalstate)
FIREWALL_ENABLED=false
STEALTH_MODE_ENABLED_TEST=$(/usr/libexec/ApplicationFirewall/socketfilterfw --getstealthmode)
STEALTH_MODE_ENABLED=false
REVERSE_SSH_TUNNEL_TEST="$(eval "lsof -i -n | egrep '\<ssh\>'")"
REVERSE_SSH_TUNNEL_STATUS=false
BLUETOOTH_TEST="$(defaults read /Library/Preferences/com.apple.Bluetooth.plist ControllerPowerState)"
BLUETOOTH_ENABLED=true
ROUTER_ADDRESS="$(route -n get default | grep gateway | perl -pe 's/gateway/IP Address (router)/g')"
INTERFACE="$(networksetup -listnetworkserviceorder | grep -i "${ACTIVE_ADAPTER}" | perl -pe 's/^(\(Hardware Port: )(.+)(, Device: )(.+)\)$/Interface: $2/g')"
DEVICE="Device: ${ACTIVE_ADAPTER}"
# Check Connectivity
if ping -c 1 -t $PING_TIMEOUT -q $PING_ADDRESS > /dev/null 2>&1; then
PING_STATUS=true
fi
# Check Firewall
if [[ $FIREWALL_ENABLED_TEST == *"enabled"* ]]; then
FIREWALL_ENABLED=true
fi
# Check Stealth Mode
if [[ $STEALTH_MODE_ENABLED_TEST == *"enabled"* ]]; then
STEALTH_MODE_ENABLED=true
fi
# Check VPN
if [ -n "$(eval "$VPN_CONNECTED")" ]; then
VPN_STATUS=true
fi
# Check DNSCrypt
if [ -n "$(eval "$DNS_CRYPT_TEST")" ]; then
DNSCRYPT_STATUS=true
fi
# Check Ad Blocking
if [ $(eval "$AD_BLOCK_TEST") != 200 ]; then
AD_BLOCK_STATUS=true
fi
# Check Reverse SSH Tunnel
if [[ $REVERSE_SSH_TUNNEL_TEST == *":ssh (ESTABLISHED)"* ]]; then
REVERSE_SSH_TUNNEL_STATUS=true
fi
# Check Bluetooth
if [ $BLUETOOTH_TEST = 0 ]; then
BLUETOOTH_ENABLED=false
fi
# Traffic Lights
if [ $PING_STATUS = true ] && [ $VPN_STATUS = true ] && [ $DNSCRYPT_STATUS = true ] && [ $AD_BLOCK_STATUS = true ] && [ $FIREWALL_ENABLED = true ] && [ $STEALTH_MODE_ENABLED = true ] && [ $REVERSE_SSH_TUNNEL_STATUS = true ] && [ $BLUETOOTH_ENABLED = false ]; then
# Green - all good
echo "●|color=#00bb00"
echo "---"
else
if [ $PING_STATUS = true ] && [ $VPN_STATUS = true ] && [ $FIREWALL_ENABLED = true ] && [ $STEALTH_MODE_ENABLED = true ] && [ $REVERSE_SSH_TUNNEL_STATUS = true ]; then
# Amber - proceed with caution
echo "●|color=#ffbf00"
echo "---"
else
# Red - stop!
echo "●|color=#990000"
echo "---"
fi
fi
# Detail
if [ $PING_STATUS = true ]; then
echo "Internet Connection ↑|color=#777777"
echo $EXTERNAL_IP
else
echo "Internet Connection ↓|color=#990000"
fi
echo $INTERNAL_IP
echo $ROUTER_ADDRESS
echo $INTERFACE
echo $DEVICE
echo "---"
if [ $BLUETOOTH_ENABLED = true ]; then
echo "Bluetooth ↑|color=#990000"
else
echo "Bluetooth ↓|color=#777777"
fi
echo "---"
if [ $FIREWALL_ENABLED = true ]; then
echo "Firewall ↑|color=#777777"
else
echo "Firewall ↓|color=#990000"
fi
if [ $STEALTH_MODE_ENABLED = true ]; then
echo "Stealth Mode ↑|color=#777777"
else
echo "Stealth Mode ↓|color=#990000"
fi
echo "---"
if [ $VPN_STATUS = true ]; then
echo "VPN ↑|color=#777777"
else
echo "VPN ↓|color=#990000"
fi
if [ $DNSCRYPT_STATUS = true ]; then
echo "DNSCrypt ↑|color=#777777"
else
echo "DNSCrypt ↓|color=#990000"
fi
if [ $AD_BLOCK_STATUS = true ]; then
echo "Ad Blocking ↑|color=#777777"
else
echo "Ad Blocking ↓|color=#990000"
fi
if [ $REVERSE_SSH_TUNNEL_STATUS = true ]; then
echo "Reverse SSH Tunnel ↑|color=#777777"
else
echo "Reverse SSH Tunnel ↓|color=#990000"
fi
@PerpetualBeta

This comment has been minimized.

Copy link
Owner Author

commented Dec 29, 2016

@PerpetualBeta

This comment has been minimized.

Copy link
Owner Author

commented Jan 7, 2017

Dependency: pcregrep
Homebrew: brew install pcre

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.