/ scripts / network.sh
network.sh
 1  #!/usr/bin/env bash
 2  # setting the locale, some users have issues with different locales, this forces the correct one
 3  export LC_ALL=en_US.UTF-8
 4  
 5  current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
 6  source $current_dir/utils.sh
 7  
 8  # set your own hosts so that a wifi is recognised even without internet access
 9  HOSTS=$(get_tmux_option "@dracula-network-hosts" "google.com github.com example.com")
10  wifi_label=$(get_tmux_option "@dracula-network-wifi-label" "")
11  ethernet_label=$(get_tmux_option "@dracula-network-ethernet-label" "Ethernet")
12  
13  get_ssid()
14  {
15    # Check OS
16    case $(uname -s) in
17      Linux)
18        SSID=$(iw dev | sed -nr 's/^\t\tssid (.*)/\1/p')
19        if [ -n "$SSID" ]; then
20          echo "$wifi_label$SSID"
21        else
22          echo "$ethernet_label"
23        fi
24        ;;
25  
26      Darwin)
27        local wifi_network=$(ipconfig getsummary en0 | awk -F ' SSID : '  '/ SSID : / {print $2}')
28        local airport=$(networksetup -getairportnetwork en0 | cut -d ':' -f 2)
29  
30        if [[ $airport != "You are not associated with an AirPort network." ]]; then
31          echo "$wifi_label$airport" | sed 's/^[[:blank:]]*//g'
32        elif [[ $wifi_network != "" ]]; then
33          echo "$wifi_label$wifi_network" | sed 's/^[[:blank:]]*//g'
34        else
35          echo "$ethernet_label"
36        fi
37        ;;
38  
39      CYGWIN*|MINGW32*|MSYS*|MINGW*)
40        # leaving empty - TODO - windows compatability
41        ;;
42  
43      *)
44        ;;
45    esac
46  
47  }
48  
49  main()
50  {
51    network="$(get_tmux_option "@dracula-network-offline-label" "Offline")"
52    for host in $HOSTS; do
53      if ping -q -c 1 -W 1 "$host" &>/dev/null; then
54        network="$(get_ssid)"
55        break
56      fi
57    done
58  
59    echo "$network"
60  }
61  
62  #run main driver function
63  main