2022-04-28 21:58:40 +02:00
|
|
|
import base
|
2022-02-26 22:44:00 +01:00
|
|
|
import std/os
|
|
|
|
import std/osproc
|
|
|
|
import strutils
|
|
|
|
|
2022-03-21 16:59:52 +01:00
|
|
|
const nics: seq[string] = @["wlan0", "enp3s0","wlp2s0","enp0s20f0u3"]
|
2022-02-26 22:44:00 +01:00
|
|
|
|
|
|
|
proc get_ip(nic: string): string =
|
|
|
|
let cmd = "ifconfig " & nic & " | grep inet | awk -F\" \" '{print $2}' | head -1 | awk '{print $1}'"
|
|
|
|
let ip = execCmdEx(cmd)
|
|
|
|
return strip(ip.output)
|
|
|
|
|
|
|
|
proc get_online_state(nic: string): string =
|
|
|
|
let oper = readFile("/sys/class/net/" & nic & "/operstate")
|
|
|
|
let state = strip(oper)
|
|
|
|
return "[" & state & "]"
|
|
|
|
|
|
|
|
proc get_net(nic: string): (string, string) =
|
|
|
|
let state = get_online_state(nic)
|
|
|
|
let ip = get_ip(nic)
|
|
|
|
if state == "[down]" or ip == "":
|
|
|
|
return ("disconnected", state)
|
|
|
|
return (ip, state)
|
|
|
|
|
2022-04-28 21:58:40 +02:00
|
|
|
proc getObject(conn: string, nic: string): Info =
|
|
|
|
var data = newInfo()
|
2022-04-28 16:52:45 +02:00
|
|
|
data.title = "IP :"
|
2022-03-21 16:59:52 +01:00
|
|
|
data.full_text = conn
|
|
|
|
data.border = purple
|
2022-04-28 21:58:40 +02:00
|
|
|
data.selected_background = purple
|
|
|
|
data.selected_color = black
|
2022-02-26 22:44:00 +01:00
|
|
|
return data
|
|
|
|
|
2022-04-28 16:52:45 +02:00
|
|
|
proc get_net_info*(nic: string) =
|
2022-02-26 22:44:00 +01:00
|
|
|
var last_ip = ""
|
|
|
|
var last_state = ""
|
|
|
|
while true:
|
|
|
|
let (ip, state) = get_net(nic)
|
|
|
|
if ip != last_ip or state != last_state:
|
2022-04-28 13:34:09 +02:00
|
|
|
let data = getObject(state & " " & ip, nic)
|
2022-04-28 16:52:45 +02:00
|
|
|
let args = @["nmtui-connect"]
|
|
|
|
let option = outputJSON(data, args)
|
|
|
|
case option:
|
|
|
|
of "nmtui-connect":
|
|
|
|
discard execCmd("alacritty -e nmtui-connect")
|
2022-02-26 22:44:00 +01:00
|
|
|
last_ip = ip
|
|
|
|
last_state = state
|
2022-04-28 16:52:45 +02:00
|
|
|
if stoploop:
|
|
|
|
break
|
2022-02-26 22:44:00 +01:00
|
|
|
sleep(1000)
|
|
|
|
|
|
|
|
|
2022-04-28 16:52:45 +02:00
|
|
|
proc get_nic*(): string =
|
2022-02-26 22:44:00 +01:00
|
|
|
for nic in nics:
|
|
|
|
if dirExists("/sys/class/net/" & nic):
|
|
|
|
return nic
|
|
|
|
return "no-nic"
|
|
|
|
|
|
|
|
proc main() =
|
|
|
|
let mynic = get_nic()
|
|
|
|
if dirExists("/sys/class/net/" & mynic):
|
2022-04-28 16:52:45 +02:00
|
|
|
get_net_info(mynic)
|
2022-03-21 16:59:52 +01:00
|
|
|
else:
|
|
|
|
echo "No NIC"
|
2022-02-26 22:44:00 +01:00
|
|
|
|
|
|
|
main()
|