-
Notifications
You must be signed in to change notification settings - Fork 1
/
wifi.nu
94 lines (83 loc) · 2.83 KB
/
wifi.nu
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/env nu
# wifi.nu
const PASSWORD_STORE_DIR = "~/.password-store"
const WIFI_PASSWORD_DIR = "wifi"
# Get wifi networks
export def get-networks [] {
let config = (config)
nmcli -f BSSID,SSID,MODE,RATE,SIGNAL,BARS,SECURITY,CHAN device wifi list |
str trim |
from ssv -m 2 |
rename -b {str replace -a '-' '' | str downcase} |
insert active {|row| $row.ssid == $config.general_connection } |
insert wpa {|row| ($row.security | str contains 'WPA') } |
insert name_fmt { |row|
$'($row.ssid) (if $row.wpa { $"\((ansi red_bold)secure(ansi reset))" }) (if $row.active { $"\((ansi green_bold)active(ansi reset))" })'
} |
uniq-by ssid
}
# Get current config
export def config [] {
nmcli device show |
from ssv -n |
transpose -r |
rename -b {str trim -c ':' | str downcase | str replace '.' '_'} |
into record
}
# Connect to wifi and optionally save to pass store
export def connect [] {
let networks = (get-networks)
let choice = ($networks | input list -fd name_fmt)
if ($choice | is-empty) { return }
if $choice.active { print $"($choice.ssid) already active"; return }
if not ($choice.wpa) {
let connection_status = (nmcli device wifi connect $choice.ssid | complete)
if $connection_status.stdout == 0 {
print $"(ansi green)Connected to ($choice.ssid)(ansi reset)"
} else {
print $"(ansi red)Failed to connect to ($choice.ssid)(ansi reset)"
}
return
}
mut password = if not (ls ($PASSWORD_STORE_DIR | path join $WIFI_PASSWORD_DIR) -s | where name == $"($choice.ssid).gpg" | is-empty) {
print "Pulling password from store"
(pass show $'wifi/($choice.ssid)')
} else {
(input "Enter password: ")
}
while not (enter-password $choice $password) {
$password = (input "Enter password again: ")
}
let pass_insert_status = (echo $password | pass insert $"($WIFI_PASSWORD_DIR)/($choice.ssid)" --echo | complete)
if $pass_insert_status.exit_code == 0 {
print $"Successfully inserted password into ($WIFI_PASSWORD_DIR)/($choice.ssid)"
} else {
print "(ansi red)Failed to insert password into store(ansi reset)"
}
print $"(ansi green)Connected to ($choice.ssid)(ansi reset)"
}
# Connect to password-protected network using nmcli
def enter-password [
choice: record
password: string
] -> bool {
let connection_status = (nmcli device wifi connect ($choice.ssid) password $password | complete)
if $connection_status.exit_code == 0 {
true
} else {
print $"(ansi red)Password incorrect or outdated(ansi reset)"
false
}
}
# Disconnect from wifi
export def disconnect [] {
nmcli device disconnect (config | get general_device)
}
# Get wifi networks
export def main [] {
get-networks
}
# Show wifi connection info
export def show [] {
nmcli device wifi show
}