dots

git clone git://mattcarlson.org/repos/dots.git
Log | Files | Refs

net.sh (3085B)


      1 #!/bin/sh
      2 #
      3 # net
      4 
      5 ERROR_ICON=''
      6 ETH_ICON=''
      7 WIFI_HIGH_ICON=''
      8 WIFI_MED_ICON=''
      9 WIFI_LOW_ICON=''
     10 WIFI_NO_ICON=''
     11 
     12 get_interface() {
     13     # get path to interface
     14     interface="$(for dev in /sys/class/net/*; do
     15                      case "${1}" in
     16                          # wifi will have a wireless/ dir
     17                          'wifi') [ -d "${dev}/wireless/" ] && [ "$(cat "${dev}/carrier")" -eq 1 ] && printf '%s\n' "${dev}" ;;
     18                          'eth') [ -d "${dev}/device/" ] && [ ! -d "${dev}/wireless/" ] && [ "$(cat "${dev}/carrier")" -eq 1 ] && printf '%s\n' "${dev}" ;;
     19                      esac
     20               done | head -n 1)"
     21     # isolate interface name
     22     interface="${interface##*/}"
     23 }
     24 
     25 get_local_ip() { local_ip="$(ip a | grep -i "\<${1}\>" | awk '{ for (i = 1; i <= NF; i++) { if ($i ~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\/[0-9]*/) { print $i } } }')" ; }
     26 
     27 get_ip_addresses() {
     28     get_interface 'wifi' && get_local_ip "${interface}"
     29     local_wifi_ip="${local_ip:-N/A}"
     30 
     31     get_interface 'eth' && get_local_ip "${interface}"
     32     local_eth_ip="${local_ip:-N/A}"
     33 
     34     public_ip="$(curl ifconfig.me/ip)"
     35     public_ip="${public_ip:-N/A}"
     36 }
     37 
     38 get_wifi() {
     39     get_interface 'wifi'
     40 
     41     # get ssid using iw
     42     ssid="$(iw dev "${interface}" info | grep -i '\<ssid\>' | awk '{ print $2 }')"
     43 
     44     # no ssid means no wifi
     45     [ -z "${ssid}" ] && return 1
     46 
     47     # Get wifi strength
     48     get_wifi_strength
     49 
     50     # one, two, or three bars depending on strength
     51     case "${wifi_strength}" in
     52       3[0-9]|4[0-9]|50)     WIFI_ICON="${WIFI_HIGH_ICON}" ;;
     53       5[1-9]|6[0-9]|7[0-9]) WIFI_ICON="${WIFI_MED_ICON}"  ;;
     54       8[0-9])               WIFI_ICON="${WIFI_LOW_ICON}"  ;;
     55       90)                   WIFI_ICON="${WIFI_NO_ICON}"   ;;
     56     esac
     57 
     58     wifi="${WIFI_ICON} ${ssid}"
     59 }
     60 
     61 get_eth() {
     62     get_interface 'eth'
     63 
     64     # eth unplugged or not working
     65     [ -z "${interface}" ] && unset ETH_ICON
     66 
     67     eth="${ETH_ICON}"
     68 }
     69 
     70 # measured in |dBm|
     71 get_wifi_strength() { [ -e '/proc/net/wireless' ] && wifi_strength=$(grep -i "\<${interface}\>" '/proc/net/wireless' | awk '{ print $4 }' | sed 's/[^0-9]*//g') ; }
     72 
     73 bar() {
     74     get_wifi
     75 
     76     get_eth
     77 
     78     # no wifi or eth and print error
     79     [ -z "${wifi}" ] && [ -z "${eth}" ] && printf '%s\n' "${ERROR_ICON}" && return 1
     80 
     81     # space needed between wifi and eth if eth is available
     82     # no eth but wifi
     83     if [ -z "${eth}" ]; then printf '%s\n' "${wifi}"
     84     # no wifi but eth
     85     elif [ -z "${wifi}" ]; then  printf '%s\n' "${eth}"
     86     # both wifi and eth
     87     else printf '%s\n' "${wifi} ${eth}"
     88     fi
     89 }
     90 
     91 open() { iwd-dmenu ; }
     92 
     93 main() {
     94     # called from bar
     95     [ ${#} -eq 0 ] && bar
     96 
     97     # bar usage
     98     case ${BLOCK_BUTTON} in
     99         1) open             ;;
    100         3) get_ip_addresses
    101            herbe "$(printf '%s\n%s\n%s' "Wifi IP: ${local_wifi_ip}" "Eth IP: ${local_eth_ip}" "Public IP: ${public_ip}")"
    102            ;;
    103     esac
    104 
    105     while getopts 'o' opt; do
    106         case "${opt}" in
    107             # open if o flag used
    108             o) open   ;;
    109             *) return ;;
    110         esac
    111     done
    112 }
    113 
    114 main "${@}"