🔒 Repository is read-only – file editing is disabled.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
#!/bin/sh
# pagan-wifi – wybór sieci Wi-Fi w PaganLinux (NetworkManager + nmcli).
#
# Uruchamiane z menu/pulpitu w sesji live, ale działa też w zainstalowanym
# systemie. Bez zależności poza nmcli (jest w NetworkManagerze) i xfce4-terminal
# jako hostem okna.
#
# Komunikaty są dwujęzyczne (PL/EN) – język wybierany z LC_ALL/LC_MESSAGES/LANG,
# domyślnie angielski (gdy język nieustawiony).
#
# Tryby:
# pagan-wifi – interaktywny wybór sieci
# pagan-wifi --list – sama lista wykrytych sieci (do diagnostyki/testów)
# pagan-wifi --status – stan interfejsów i adresy IP
set -u
# ── Język (PL/EN) ───────────────────────────────────────────────────────────
_pw_detect_lang() {
_l="${LC_ALL:-${LC_MESSAGES:-${LANG:-}}}"
case "$_l" in
pl|pl_*|pl.*|PL|PL_*|PL.*) printf '%s' pl ;;
*) printf '%s' en ;;
esac
}
PW_LANG="$(_pw_detect_lang)"
# t <klucz> – tekst interfejsu w wybranym języku (nieznany klucz = sam klucz).
t() {
if [ "$PW_LANG" = en ]; then
case "$1" in
status_ifaces) printf '%s' "=== Interfaces (nmcli device) ===" ;;
status_ips) printf '%s' "=== IP addresses ===" ;;
status_conn) printf '%s' "=== Connectivity ===" ;;
inet_ok) printf '%s' "Internet: OK (1.1.1.1 replies)" ;;
inet_fail) printf '%s' "Internet: no reply from 1.1.1.1" ;;
prompt_pass) printf '%s' 'Password for "%s" (hidden): ' ;;
connecting) printf '%s' 'Connecting to "%s"...' ;;
connected) printf '%s' "Connected." ;;
connect_fail) printf '%s' "Could not connect. Check the password or try again." ;;
no_nmcli) printf '%s' "nmcli not found. Install/start NetworkManager: sudo systemctl start NetworkManager" ;;
no_iface) printf '%s' "No Wi-Fi adapter detected." ;;
no_iface_hint1) printf '%s' "Check the adapter is visible: lsusb / ip link" ;;
no_iface_hint2) printf '%s' "And that its driver loaded: dmesg | grep -i firmware" ;;
iface) printf '%s' "Wi-Fi adapter: %s" ;;
scanning) printf '%s' "Scanning for networks (this may take a moment)..." ;;
no_networks) printf '%s' "No Wi-Fi networks found." ;;
choose) printf '%s' 'Select a network number (or "q" to quit): ' ;;
bad_choice) printf '%s' "Invalid choice." ;;
no_such) printf '%s' "No network number %s." ;;
help_title) printf '%s' "pagan-wifi - choose a Wi-Fi network (NetworkManager)" ;;
help_usage) printf '%s' " pagan-wifi interactive network chooser" ;;
help_list) printf '%s' " pagan-wifi --list list detected networks" ;;
help_status) printf '%s' " pagan-wifi --status interface state and IP addresses" ;;
*) printf '%s' "$1" ;;
esac
else
case "$1" in
status_ifaces) printf '%s' "=== Interfejsy (nmcli device) ===" ;;
status_ips) printf '%s' "=== Adresy IP ===" ;;
status_conn) printf '%s' "=== Łączność ===" ;;
inet_ok) printf '%s' "Internet: OK (1.1.1.1 odpowiada)" ;;
inet_fail) printf '%s' "Internet: brak odpowiedzi z 1.1.1.1" ;;
prompt_pass) printf '%s' 'Hasło do "%s" (nie będzie widoczne): ' ;;
connecting) printf '%s' 'Łączenie z "%s"...' ;;
connected) printf '%s' "Połączono." ;;
connect_fail) printf '%s' "Nie udało się połączyć. Sprawdź hasło albo spróbuj ponownie." ;;
no_nmcli) printf '%s' "Brak polecenia nmcli. Zainstaluj/uruchom NetworkManager: sudo systemctl start NetworkManager" ;;
no_iface) printf '%s' "Nie wykryto karty Wi-Fi." ;;
no_iface_hint1) printf '%s' "Sprawdź, czy adapter jest widoczny: lsusb / ip link" ;;
no_iface_hint2) printf '%s' "Oraz czy sterownik się załadował: dmesg | grep -i firmware" ;;
iface) printf '%s' "Karta Wi-Fi: %s" ;;
scanning) printf '%s' "Szukam sieci (chwilę to potrwa)..." ;;
no_networks) printf '%s' "Nie znaleziono żadnej sieci Wi-Fi." ;;
choose) printf '%s' 'Numer sieci (lub "q" aby wyjść): ' ;;
bad_choice) printf '%s' "Niepoprawny wybór." ;;
no_such) printf '%s' "Nie ma sieci o numerze %s." ;;
help_title) printf '%s' "pagan-wifi – wybór sieci Wi-Fi (NetworkManager)" ;;
help_usage) printf '%s' " pagan-wifi interaktywny wybór sieci" ;;
help_list) printf '%s' " pagan-wifi --list lista wykrytych sieci" ;;
help_status) printf '%s' " pagan-wifi --status stan interfejsów i adresy IP" ;;
*) printf '%s' "$1" ;;
esac
fi
}
# tfmt <klucz> [argi...] – tekst z podstawieniem (dane użytkownika jako
# argumenty printf, nie jako format – bezpieczne dla SSID z „%”).
tfmt() {
_k="$1"; shift
# shellcheck disable=SC2059
printf "$(t "$_k")" "$@"
}
msg() { printf '%s\n' "$*"; }
err() { printf '%s\n' "$*" >&2; }
have_nmcli() {
command -v nmcli >/dev/null 2>&1
}
# Nazwa interfejsu Wi-Fi (puste, jeśli brak karty).
wifi_iface() {
nmcli -t -f DEVICE,TYPE device 2>/dev/null | awk -F: '$2 == "wifi" { print $1; exit }'
}
# Lista sieci: "SIGNAL<TAB>SECURITY<TAB>SSID", posortowana malejąco po sygnale.
#
# Uwaga na format: `nmcli -t` rozdziela pola dwukropkiem, a SSID może dwukropki
# zawierać. `-e no` wyłącza escapowanie (inaczej nmcli zapisuje je jako \:) i
# wtedy dwoma ostatnimi polami są zawsze SYGNAŁ i ZABEZPIECZENIA, a SSID to
# wszystko przed nimi.
list_networks() {
nmcli -e no -t -f SSID,SIGNAL,SECURITY device wifi list --rescan yes 2>/dev/null | \
awk -F: '
NF >= 3 {
sec = $NF
sig = $(NF - 1)
ssid = ""
for (i = 1; i < NF - 1; i++) {
ssid = ssid (i > 1 ? ":" : "") $i
}
if (ssid == "" || sig == "") {
next
}
if (!(ssid in best) || sig + 0 > best[ssid] + 0) {
best[ssid] = sig
secs[ssid] = sec
}
}
END {
for (s in best) {
printf "%s\t%s\t%s\n", best[s], secs[s], s
}
}
' | sort -rn
}
show_status() {
msg "$(t status_ifaces)"
nmcli -t -f DEVICE,TYPE,STATE,CONNECTION device 2>/dev/null | tr ':' '\t' || true
msg ""
msg "$(t status_ips)"
if command -v ip >/dev/null 2>&1; then
ip -4 -brief addr 2>/dev/null || ip -4 addr 2>/dev/null || true
fi
msg ""
msg "$(t status_conn)"
if ping -c1 -W3 1.1.1.1 >/dev/null 2>&1; then
msg "$(t inet_ok)"
else
msg "$(t inet_fail)"
fi
}
connect_network() {
_ssid="$1"
_sec="$2"
_pass=""
if [ "$_sec" != "--" ] && [ "$_sec" != "" ]; then
tfmt prompt_pass "$_ssid"
if command -v stty >/dev/null 2>&1; then
stty -echo 2>/dev/null || true
read -r _pass
stty echo 2>/dev/null || true
printf '\n'
else
read -r _pass
fi
fi
msg "$(tfmt connecting "$_ssid")"
if [ -n "$_pass" ]; then
if nmcli device wifi connect "$_ssid" password "$_pass"; then
_rc=0
else
_rc=1
fi
else
if nmcli device wifi connect "$_ssid"; then
_rc=0
else
_rc=1
fi
fi
_pass=""
if [ "$_rc" -eq 0 ]; then
msg "$(t connected)"
else
err "$(t connect_fail)"
fi
msg ""
show_status
return "$_rc"
}
interactive() {
if ! have_nmcli; then
err "$(t no_nmcli)"
return 1
fi
_iface="$(wifi_iface)"
if [ -z "$_iface" ]; then
err "$(t no_iface)"
err "$(t no_iface_hint1)"
err "$(t no_iface_hint2)"
msg ""
show_status
return 1
fi
msg "$(tfmt iface "$_iface")"
msg "$(t scanning)"
_list="$(list_networks)"
if [ -z "$_list" ]; then
err "$(t no_networks)"
return 1
fi
# Numerowana lista (zachowujemy SSID i zabezpieczenia w plikach tymczasowych).
_tmp="$(mktemp)" || return 1
printf '%s\n' "$_list" | awk -F'\t' '{ printf "%2d) %-32s %3s%% %s\n", NR, $3, $1, $2 }'
printf '%s\n' "$_list" > "$_tmp"
printf '\n%s' "$(t choose)"
read -r _choice
case "$_choice" in
q|Q|"")
rm -f "$_tmp"
return 0
;;
esac
case "$_choice" in
*[!0-9]*)
err "$(t bad_choice)"
rm -f "$_tmp"
return 1
;;
esac
_entry="$(sed -n "${_choice}p" "$_tmp")"
rm -f "$_tmp"
if [ -z "$_entry" ]; then
err "$(tfmt no_such "$_choice")"
return 1
fi
_sig="$(printf '%s' "$_entry" | cut -f1)"
_sec="$(printf '%s' "$_entry" | cut -f2)"
_ssid="$(printf '%s' "$_entry" | cut -f3-)"
connect_network "$_ssid" "$_sec"
return $?
}
case "${1:-}" in
--list)
list_networks
;;
--status)
show_status
;;
--help|-h)
msg "$(t help_title)"
msg "$(t help_usage)"
msg "$(t help_list)"
msg "$(t help_status)"
;;
*)
interactive
;;
esac