Skip to content

Instantly share code, notes, and snippets.

@dkittell
Last active March 26, 2024 21:39
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dkittell/34fe7b7422323fba546948b7448933a5 to your computer and use it in GitHub Desktop.
Save dkittell/34fe7b7422323fba546948b7448933a5 to your computer and use it in GitHub Desktop.
#!/bin/sh
# SetDNS.sh
#
#
# Created by David Kittell on 6/14/19.
#
# Variables - Start
sExternalIPService="http://dns.kittell.net/ip.php"
sExternalMACALService="http://dns.kittell.net/macaltext.php?address="
sFunctionsDirectory="/Users/dkittell/Documents/SourceCode/sh/Functions/" # Make sure to update to a valid directory.
# Get remote/public IP address
remoteip=$(curl -sSf -m 5 --connect-timeout 5 $sExternalIPService)
# Get computer name
computername=$(scutil --get ComputerName)
# Get serial number
sSerialNumber=$(system_profiler SPHardwareDataType |grep "Serial Number (system)" |awk '{print $4}' | cut -d/ -f1)
#echo $sSerialNumber
# Variables - Stop
# Run Once - Start
#mkdir -p $sFunctionsDirectory
#cd $sFunctionsDirectory
#wget https://gist.githubusercontent.com/dkittell/fcc82cef813fdc606a518867a33aa81a/raw/723b5161b784756086914affc30ff0baa749c51b/UnixIdentification.sh
#cd ~/
# Run Once - Stop
# Functions - Start
# Function to convert IP Subnet Mask to CIDR
mask2cdr ()
{
# Assumes there's no "255." after a non-255 byte in the mask
local x=${1##*255.}
set -- 0^^^128^192^224^240^248^252^254^ $(( (${#1} - ${#x})*2 )) ${x%%.*}
x=${1%%$3*}
echo $(( $2 + (${#x}/4) ))
}
IPUpStatus()
{
if ping -c 1 $1 &> /dev/null
#if ping -c 1 8.8.8.8 &> /dev/null
#if ping -c 1 2a0d:2a00:1:abab:23b9:93ef:2da9:dd2f &> /dev/null
then
rc=1
else
rc=0
fi
#rc=$(ping -c 1 -m 5 "$1" | grep "100.0% packet loss")
echo $rc
}
DHCPDNS()
{
#sudo clear
sudo networksetup -setwebproxystate "$ActiveNetworkName" Off
sudo networksetup -setsecurewebproxystate "$ActiveNetworkName" Off
sudo networksetup -setv6automatic "$ActiveNetworkName"
sudo networksetup -setdnsservers "$ActiveNetworkName" empty
sudo networksetup -setdhcp "$ActiveNetworkName"
sudo networksetup -setsearchdomains "$ActiveNetworkName" empty
sudo networksetup -setproxybypassdomains "$ActiveNetworkName" empty
sudo dscacheutil -flushcache
sudo killall -HUP mDNSResponder
say "DNS Updated for $ActiveNetworkName"
echo "\n DNS Updated"
DHCPActive=$(networksetup -getinfo "$ActiveNetworkName" | grep DHCP)
# echo $DHCPActive
dnsserver=$(networksetup -getdnsservers "$ActiveNetworkName" )
# echo $dnsserver
if [[ -z $dnsserver || $DHCPActive == "DHCP Configuration" ]]; then
if [[ $DHCPActive ]]; then
echo " DNS Server: Set With DHCP"
else
echo " DNS Server: Unknown"
fi
else
echo " DNS Server: $dnsserver"
fi
}
StaticDNS()
{
if [[ ! -z "$1" ]]; then
echo "\nValidating $1 is accessible..."
IPUpStatus "$1"
if [[ $rc == 1 ]] ; then
sudo networksetup -setv6automatic "$ActiveNetworkName"
sudo networksetup -setdhcp "$ActiveNetworkName" empty # Disable DHCP if it's on
sudo networksetup -setwebproxystate "$ActiveNetworkName" Off
sudo networksetup -setsecurewebproxystate "$ActiveNetworkName" Off
sudo networksetup -setdnsservers "$ActiveNetworkName" empty
sudo networksetup -setsearchdomains "$ActiveNetworkName" empty
sudo networksetup -setproxybypassdomains "$ActiveNetworkName" empty
if [[ ! -z "$2" ]]; then
echo "Validating $2 is accessible..."
IPUpStatus "$1"
if [[ $rc == 1 ]] ; then
sudo networksetup -setdnsservers "$ActiveNetworkName" $1 $2 $3 $4
else
sudo networksetup -setdnsservers "$ActiveNetworkName" $1
fi
else
sudo networksetup -setdnsservers "$ActiveNetworkName" $1
fi
sudo dscacheutil -flushcache
sudo killall -HUP mDNSResponder
say "DNS Updated for $ActiveNetworkName"
echo "\n DNS Updated"
DHCPActive=$(networksetup -getinfo "$ActiveNetworkName" | grep DHCP)
dnsserver=$(networksetup -getdnsservers "$ActiveNetworkName" | cut -d/ -f1|tr '\n' ' ')
if [[ -z $dnsserver ]]; then
if [[ $DHCPActive ]]; then
echo " DNS Server: Set With DHCP"
else
echo " DNS Server: Unknown"
fi
else
echo " DNS Server: $dnsserver"
fi
else
echo "Supplied DNS is not valid, setting DHCP"
DHCPDNS
fi
fi
}
NetworkInfo ()
{
# List all Network ports
NetworkPorts=$(ifconfig -uv | grep '^[a-z0-9]' | awk -F : '{print $1}')
echo "$computername"
echo "--------------"
echo " Computer OS: $(sh $sFunctionsDirectory/UnixIdentification.sh)"
echo " Computer Name: $computername"
echo " Current User Name: $(whoami)"
echo " Serial Number: $sSerialNumber"
if [[ $remoteip ]]; then
echo "Remote IP v4 Address: $remoteip\n"
else
echo "Remote IP v4 Address: Unable To Determine\n"
fi
echo "Active Network Port(s):"
for val in $(echo $NetworkPorts); do # Get for all available hardware ports their status
activated=$(ifconfig -uv "$(echo $val)" | grep 'status: ' | awk '{print $2}')
# Get VPN Information - Start
if [[ ":${NetworkPorts[*]}:" =~ "ppp0" ]]; then
VPNActivated=$(ifconfig -uv "ppp0" | grep 'state availability' | grep 'true')
if [ ! -z "VPNActivated" ]; then
#echo "VPN Active"
VPNipaddress=$(ifconfig -uv "ppp0" | grep 'inet' | cut -d ' ' -f2)
# VPNipaddressTranslation=$(ifconfig -uv "ppp0" | grep 'inet' | cut -d ' ' -f4)
#echo $VPNipaddress
fi
fi
if [[ ":${NetworkPorts[*]}:" =~ "gpd0" ]]; then
VPNActivated=$(ifconfig -uv "gpd0" | grep 'state availability' | grep 'true')
if [ ! -z "VPNActivated" ]; then
#echo "VPN Active"
VPNipaddress=$(ifconfig -uv "gpd0" | grep 'inet' | cut -d ' ' -f2 | sed 's/^[ \t]*//;s/[ \t]*$//')
#echo $VPNipaddress
fi
fi
# Get VPN Information - Stop
if [ "$activated" == "active" ]; then
label=$(ifconfig -uv "$(echo $val)" | grep 'type' | awk '{print $2}')
# echo $label
# ActiveNetwork=$(route get default | grep interface | awk '{print $2}')
# ActiveNetworkName=$(networksetup -listallhardwareports | grep -B 1 "$label" | awk '/Hardware Port/{ print }'|cut -d " " -f3- | uniq)
# ActiveNetworkName=$(networksetup -listnetworkserviceorder | grep $ActiveNetwork -B1 | head -1 | cut -d ")" -f2 | sed "s/^[ \t]*//")
ActiveNetworkName=$(networksetup -listnetworkserviceorder | grep "$val" -B1 | grep -v "$val" | cut -d ')' -f2 | sed 's/^[ ]*//;s/[ ]*$//')
#echo $ActiveNetwork
#echo $ActiveNetworkName
state=$(ifconfig -uv "$val" | grep 'status: ' | awk '{print $2}')
#echo $state
ipaddress=$(ifconfig -uv "$val" | grep 'inet ' | awk '{print $2}' | cut -d/ -f1|tr '\n' ' ')
# echo $ipaddress
ipaddressv6=$(ifconfig "$val" | grep inet6 | grep 'scopeid' | cut -d ' ' -f2 | cut -d '%' -f1 | cut -d/ -f1|tr '\n' ' ')
netmask=$(ipconfig getpacket "$val" | grep 'subnet_mask (ip):' | awk '{print $3}' | tr -d '[:space:]')
#echo $netmask
if [[ -z $(ifconfig -uv "$val" | grep 'link rate: ' | awk '{print $3, $4}' | sed 'N;s/\n/ up /' ) ]]; then
networkspeed="$(ifconfig -uv "$val" | grep 'link rate: ' | cut -d ':' -f2 | sed 's/^[ \t]*//;s/[ \t]*$//')"
else
networkspeed="$(ifconfig -uv "$val" | grep 'link rate: ' | awk '{print $3, $4}' | sed 'N;s/\n/ up /' ) down"
fi
quality=$(ifconfig -uv "$val" | grep 'link quality:' | awk '{print $3, $4}')
#echo $quality
macaddress=$(ifconfig -uv "$val" | grep 'ether ' | awk '{print $2}')
#echo $macaddress
macal=$(curl -s "$sExternalMACALService$macaddress" | sed 's/^[ ]*//;s/[ ]*$//')
#echo $macal
router=$(ipconfig getpacket "$val" | grep 'router (ip_mult):' | sed 's/.*router (ip_mult): {\([^}]*\)}.*/\1/')
#echo $router
DHCPActive=$(networksetup -getinfo "$ActiveNetworkName" | grep DHCP)
#echo $DHCPActive
#dnsserver=$(networksetup -getdnsservers "$ActiveNetworkName" | awk '{print $1, $2}' | sed 'N;s/\n//' )
dnsserver=$(networksetup -getdnsservers "$ActiveNetworkName" | cut -d/ -f1|tr '\n' ' ')
#echo $dnsserver
if [[ ! -z "$netmask" ]]; then
if [[ $ipaddress ]]; then
# Is this a WiFi associated port? If so, then we want the network name
if [ "$label" = "Wi-Fi" ]; then
WiFiName=$(/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I | grep '\sSSID:' | sed 's/.*: //')
#echo $WiFiName
echo " Network Name: $WiFiName"
fi
echo " Interface: $ActiveNetworkName ($val)"
echo " MAC Address: $macaddress ($macal)\n"
if [[ ! -z "$ipaddressv6" ]]; then
echo " IP Address v6: $ipaddressv6"
fi
echo " IP Address v4: $ipaddress"
echo " Subnet Mask: $netmask"
echo " Router: $router"
echo " IP CIDR: $ipaddress/$(mask2cdr $netmask)\n"
echo " Network Speed: $networkspeed"
echo " Link quality: $quality"
if [ ! -z "$VPNipaddress" ]; then
echo " VPN IP Address: $VPNipaddress\n"
fi
if [[ -z $dnsserver || $DHCPActive == "DHCP Configuration" ]]; then
if [[ $DHCPActive ]]; then
echo " DNS Server: Set With DHCP"
else
echo " DNS Server: Unknown"
fi
else
echo " DNS Server: $dnsserver"
fi
case "$1" in
"dhcp")
DHCPDNS
;;
"home")
# StaticDNS 10.40.20.3
# StaticDNS 185.228.168.80 185.228.169.80 # IPv4 CleanBrowsing
StaticDNS 185.228.168.80 185.228.169.80 2a0d:2a00:1:abab:23b9:93ef:2da9:dd2f 2a0d:2a00:2:abab:23b9:93ef:2da9:dd2f
;;
"cleanbrowsing")
# StaticDNS 185.228.168.80 185.228.169.80 # IPv4 CleanBrowsing
StaticDNS 185.228.168.80 185.228.169.80 2a0d:2a00:1:abab:23b9:93ef:2da9:dd2f 2a0d:2a00:2:abab:23b9:93ef:2da9:dd2f
;;
"opendns")
StaticDNS 208.67.222.222 208.67.220.220
;;
"google")
StaticDNS 8.8.8.8 8.8.4.4
;;
*)
sConfig="0"
;;
esac
echo "--------------"
fi
fi
fi
done
}
# Functions - Stop
# Check Online Status - Start
#nc -z 8.8.8.8 53 >/dev/null 2>&1
#online=$?
#if [ $online -eq 0 ]; then
# echo "Online, continuing...\n"
#
NetworkInfo $(echo $1 | awk '{print tolower($0)}')
#
# remoteip=$(dig +short myip.opendns.com @resolver1.opendns.com)
# echo "Remote IP: $remoteip"
#
#else
# echo "Sorry you are offline, please connect and try again."
# exit
#fi
# Check Online Status - Stop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment