~whynothugo/dotfiles

ref: 3f722af25eaf8e64680238808c2535ac10f0fe74 dotfiles/home/.local/bin/net -rwxr-xr-x 2.5 KiB
3f722af2Hugo Osvaldo Barrera zsh: set theme for terminal emulator at startup 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/sh
#
# Show network status, at various layers.

BOLD=$(tput bold)
GREEN=$(tput setaf 2)
RED=$(tput setaf 1)
RESET=$(tput sgr0)

success() {
  echo "$GREEN$BOLD${RESET}$*"
}

failure() {
  echo "$RED$BOLD${RESET}$*"
}

CONNECTED=$(iwctl station list | grep connected)
if [ -z "$CONNECTED" ]; then
  failure "Not connected to any network."
  exit 1
else
  success "Host $BOLD$(hostname)$RESET is connected."
fi

# FIXME: Only shows first work of networks with spaces:
WIFI=$(iwctl station wlan0 show | grep "Connected network" | awk '{ print $3 }')
if [ -z "$WIFI" ]; then
  failure "Not connected to any network."
  exit 1
else
  success "Connected to wifi ${BOLD}$WIFI${RESET}."
fi

# # XXX: libcurl has the dns-servers feature disabled.
# WIFI_DNS=$(cat /run/dhcpcd/nameservers | grep -Pv "^# ")
# if ! CAP=$(curl --dns-servers "$WIFI_DNS" --silent captive.apple.com); then
#   failure "HTTP traffic is failing (and no reachable captive portal found)."
#   exit 3
# fi
#
# if [ "$CAP" = "<HTML><HEAD><TITLE>Success</TITLE></HEAD><BODY>Success</BODY></HTML>" ]; then
#   success "No captive portal detected."
# else
#   failure "Captive portal or weird firewall detected."
#   exit 4
# fi

# TODO: check for a routable IP
# TODO: check for a gateway

DNS=false
if drill whynothugo.nl > /dev/null; then
  success "DNS (whynothugo.nl) works."
  DNS=true
else
  failure "DNS (whynothugo.nl) resolution failing."
fi

if drill example.com > /dev/null; then
  success "DNS (example.com) works."
  DNS=true
else
  failure "DNS (example.com) resolution failing."
fi

if [ "$DNS" = "false" ]; then
  exit 2
fi

# TODO: I should run this command, and swallot its output. Only show it on error.
# XXX: Actually, all commands in this file should be in parallel, and we should
# swallow its output. Whenever one returns non-zero, we render its output and
# exit code.
# orange(warn) background and black text for header (step, command) and regular
# colours for stdout and red for stderr
#curl --connect-timeout 30 http://httpforever.com/

if ! CAP=$(curl --silent captive.apple.com); then
  failure "HTTP traffic is failing (or there's an odd captive portal around."
  exit 3
fi

if [ "$CAP" = "<HTML><HEAD><TITLE>Success</TITLE></HEAD><BODY>Success</BODY></HTML>" ]; then
  success "No captive portal detected."
else
  failure "Captive portal or weird firewall detected."
  exit 4
fi

success "All good, network seems functional! 🎉"

# TODO: try DDG for really weird network filters.
# TODO: try TPB to check censoring.