Skip to content

Instantly share code, notes, and snippets.

@pkutzner
Created October 28, 2014 15:12
Show Gist options
  • Save pkutzner/bcd53571b93a380c3b9c to your computer and use it in GitHub Desktop.
Save pkutzner/bcd53571b93a380c3b9c to your computer and use it in GitHub Desktop.
OSX Network Shell Aliases
alias airport="/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport"
alias bssid="networksetup -getairportnetwork $(networksetup -listallhardwareports | grep -A 2 'Hardware Port: Wi-Fi' | grep 'Device:' | awk '{print $2}')"
alias ssid="bssid"
# Use alias below with (on|off) to turn airport card on or off from CL.
alias wifi="networksetup -setairportpower $(networksetup -listallhardwareports | grep -A 2 'Hardware Port: Wi-Fi' | grep 'Device:' | awk '{print $2}')"
alias wifi-info="networksetup -getinfo Wi-Fi"
alias apinfo="airport -I"
alias known-networks="networksetup -listpreferrednetworks $(networksetup -listallhardwareports | grep -A 2 'Hardware Port: Wi-Fi' | grep 'Device:' | awk '{print $2}')"
alias wifi-disconnect="sudo airport -z"
alias wifi-networks="airport -s"
wifi-connect() {
local hardwareport=$(networksetup -listallhardwareports | grep -A 2 'Hardware Port: Wi-Fi' | grep 'Device:' | awk '{print $2}')
if [ $# -eq 0 ]; then
echo "Usage: $0 <bssid> [password]"
return 1
fi
networksetup -setairportnetwork "$hardwareport" "$1" "$2"
}
set-mtu() {
if [ $# -lt 2 ]; then
echo "Usage: $0 <interface> <mtu>"
return 1
fi
networksetup -setMTU "$1" "$2"
}
get-mtu() {
if [ $# -eq 0 ]; then
local ethernet=$(networksetup -listallhardwareports | grep -A 2 'Hardware Port: Ethernet' | grep 'Device:' | awk '{print $2}')
local wifi=$(networksetup -listallhardwareports | grep -A 2 'Hardware Port: Wi-Fi' | grep 'Device:' | awk '{print $2}')
echo "1. Ethernet: $ethernet"
echo "2. Wi-Fi: $wifi"
printf "Choose Interface: "
read selection
case "$selection" in
1)
networksetup -getMTU $ethernet
;;
2)
networksetup -getMTU $wifi
;;
*)
echo "Invalid option."
return 1
;;
esac
else
networksetup -getMTU "$1"
fi
}
manual() {
if [ $# -lt 4 ]; then
echo "Usage: $0 <Ethernet|Wi-Fi> <ip> <netmask> <gateway>"
return 1
fi
networksetup -setmanual "$1" "$2" "$3" "$4"
}
dhcp() {
if [ $# -eq 0 ]; then
echo "Usage: $0 <Ethernet|Wi-Fi> ['Empty']"
echo " Specifying 'Empty' will clear the DHCP client ID"
return 1
case "$1" in
[Ww]i-[Ff]i|[Ee]thernet)
networksetup -setdhcp "$1" "$2"
;;
*)
echo "Invalid option."
return 1
;;
esac
}
get-media() {
# Display current media setting for <interface>
if [ $# -eq 0 ]; then
echo "Usage: $0 <interface>"
return 1
fi
networksetup -getMedia "$1"
}
list-media() {
# List all available media settings for <interface>
if [ $# -eq 0 ]; then
echo "Usage: $0 <interface>"
return 1
fi
networksetup -listValidMedia "$1"
}
set-media() {
if [ $# -lt 3 ]; then
echo "Usage: $0 <interface> <media> <duplex>"
echo " Use 'list-media <interface>' to get list of valid media for <interface>"
echo " Where <media>=(1000|100baseT|10baseTX) and <duplex>=(full|half-duplex)"
return 1
fi
local ineterface="$1"
local media="$2"
shift; shift;
networksetup -setMedia "$interface" "$media" "$*"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment