radare2rc, dwmcolors, vimrc
[dotfiles_afify.git] / .scripts / network
blob022d9510a2cc4a4d57ec9ae2d04804c66c66cc83
1 #!/usr/bin/env bash
3 # TODO if no internet
5 wifi_icon="直"
6 eth_icon=""
7 other_icon="禍"
8 bpm=1048576 # 1024*1024
9 up_interfaces=$(ifconfig | grep "UP,BROADCAST,RUNNING" |\
10 sed 's/://' | awk '{print $1}')
11 up_type=
12 ssid_strength=
14 check_type(){
15 case $(uname) in
16 Linux) case $1 in
17 wlp*) up_type=wlan ;;
18 enp*) up_type=eth ;;
19 *) up_type=other ;;
20 esac ;;
21 OpenBSD) up_type=$(ifconfig $1 | grep "groups" | awk '{print $2}') ;;
22 esac
25 get_ssid_strength(){
26 if [[ "$up_type" == "wlan" ]]; then
27 case $(uname) in
28 Linux)
29 wifi_ssid=$(iw dev $1 link | grep SSID | cut -d " " -f 2-)
30 wifi_strength=$(grep "^\s*w" /proc/net/wireless |\
31 awk '{print int($3 * 100 / 70) "%"}')
32 ssid_strength='\n'$wifi_ssid' '$wifi_strength;;
34 OpenBSD) ssid_strength=$(echo "$int" | grep join |\
35 awk '{print "\n"$3"\t"int((-0.0154*$8*$8)-(0.3794*$8)+98.182)"%"}');;
36 esac
37 else
38 ssid_strength=
43 print_status(){
44 for each_interface in $up_interfaces; do
45 # case $(uname) in
46 # Linux) total_in=$(
47 # cat /sys/class/net/$each_interface/statistics/rx_bytes);;
48 # OpenBSD) total_in=$(netstat -b -I $each_interface |\
49 # egrep "[a-z0-9]{2}:" | awk '{print $5}');;
50 # esac
51 check_type $each_interface
52 case $up_type in
53 egress) icon=$other_icon ;;
54 wlan) icon=$wifi_icon ;;
55 eth) icon=$eth_icon ;;
56 *) icon=$other_icon ;;
57 esac
58 # in_sum=$(($total_in / $bpm))
59 # result+=$icon' '$in_sum' '
60 result+=$icon' '
61 done
62 echo $result
65 notify_status(){
66 for each_interface in $up_interfaces; do
67 int=$(ifconfig $each_interface)
68 ip=$(echo "$int" | grep 'inet ' | awk '{print $2}')
69 check_type $each_interface
70 get_ssid_strength $each_interface
71 result+="$up_type\t($each_interface)$ssid_strength\n$ip\n\n"
72 done
73 notify-send "Network details" "$result"
76 print_ips(){
77 [ "$(uname)" = "Linux" ] && ifconfig | grep -A1 "$up_interfaces" | awk '/inet/ {print $2}'
78 [ "$(uname)" = "OpenBSD" ] && ifconfig | grep -A7 "$up_interfaces" | awk '/inet/ {print $2}'
81 case $1 in
82 notify) notify_status ;;
83 ips) print_ips ;;
84 *) print_status ;;
85 esac