<NME> wifi
<BEF> #!/usr/bin/env bash


_W_DEVICE=${_W_DEVICE:-en0}

help(){
    cat<<__EOF__
    usage:  m wifi [ scan | off | on | connect | help ]

    Examples:
      m wifi status                  # wifi status
      m wifi scan                    # scan wifi
      m wifi showpassword [ESSID]    # show wifi network password (default: current)
      m wifi ls                      # list known wifi networks
      m wifi list                    # list known wifi networks
      m wifi forget ESSID            # forget a wifi network
      m wifi history                 # wifi connection history
      m wifi off                     # turn off your wifi
      m wifi on                      # turn on your wifi
      m wifi connect ESSID PASSWORD  # join a wifi network
      m wifi connect ESSID           # join a wifi network (prompt for password)
__EOF__
}

wifi_scan(){
    sudo /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -s
}

wifi_history(){
    /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -s
}

wifi_history(){
    defaults read /Library/Preferences/SystemConfiguration/com.apple.airport.preferences | grep LastConnected -A 7
}

wifi_showpassword(){
    if [ -z "$1" ]
    then
        ssid="`/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | awk '/ SSID/ {print substr($0, index($0, $2))}'`"
    else
        ssid=$1
    fi
    security find-generic-password -D "AirPort network password" -a "$ssid" -gw
}

wifi_off(){
    networksetup -setairportpower ${_W_DEVICE} off
}

wifi_on(){
    networksetup -setairportpower ${_W_DEVICE} on
}

connect_network(){
    [ "$#" -lt 1  ] && help
    NETWORK="$1"
    if [ "$#" -eq 1  ]; then
        read -s -r -p "Password: " PASSWORD
    elif [ "$#" -eq 2 ]; then
        PASSWORD="$2"
    fi
    networksetup -setairportnetwork "${_W_DEVICE}" "${NETWORK}" "${PASSWORD}"
}

wifi_list_known(){
    networksetup -listpreferredwirelessnetworks ${_W_DEVICE} | grep -v ^Preferred
}

wifi_forget(){
    if [ -z "$1" ]; then
        help
    else
        NETWORK="$1"
        sudo networksetup -removepreferredwirelessnetwork ${_W_DEVICE} "${NETWORK}"
    fi
}

case $1 in
    help)
        help
        ;;
    scan)
        wifi_scan
        ;;
    status)
        wifi_status
        ;;
    off)
        wifi_off
        ;;
    on)
        wifi_on
        ;;
    list|ls)
        wifi_list_known
        ;;
    connect)
        shift
        connect_network "$@"
        ;;
    history)
        shift
        wifi_history
        ;;
    showpassword)
        shift
        wifi_showpassword "$@"
        ;;
    forget)
        shift
        wifi_forget "$@"
        ;;
    *)
        help
        ;;
esac

<MSG> Merge pull request #20 from MiteshShah/master

Does not required sudo.
<DFF> @@ -23,7 +23,7 @@ wifi_status(){
 }
 
 wifi_scan(){
-    sudo /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -s
+    /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -s
 }
 
 wifi_history(){

{'addition_count': 1, 'commit_subject': 'Merge pull request #20 from MiteshShah/master', 'deletion_count': 1, 'file_extension': '', 'lang': 'wifi', 'license': 'mit', 'repo_name': 'rgcr/m-cli'}