added network

This commit is contained in:
Paul Wilde 2023-11-23 09:29:59 +00:00
parent f17a160450
commit d0142a966b
2 changed files with 0 additions and 79 deletions

View file

@ -1,13 +0,0 @@
# Package
version = "0.1.0"
author = "Paul Wilde"
description = "Display NIC IP address and link to manage NICs"
license = "GPL-3.0-or-later"
srcDir = "src"
bin = @["netwurrk"]
# Dependencies
requires "nim >= 1.6.6"

View file

@ -1,66 +0,0 @@
import ../../globurrl
import std/[os,osproc,strutils,sequtils]
const default_fg = black
const default_bg = purple
const mng_cmd = "alacritty -e nmtui-connect"
const nics: seq[string] = @["wlan0","bridge0", "enp3s0","wlp2s0","enp0s20f0u3"]
proc getIP(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 getOnlineState(nic: string): string =
try:
let oper = readFile("/sys/class/net/" & nic & "/operstate")
let state = strip(oper)
return "[" & state & "]"
except:
echo "Error getting operstate for " & nic & " : ", getCurrentExceptionMsg()
return ""
proc getConnState(nic: string): (string, string) =
let state = getOnlineState(nic)
let ip = getIP(nic)
if state == "[down]" or ip == "":
return ("disconnected", state)
return (ip, state)
proc getObject(): Info =
var data = newInfo("Netwurrk")
data.selected_bg = default_bg
data.selected_fg = default_fg
# i3bar stuff
data.border = default_bg
return data
proc getNetInfo*(my_nics: seq[string]) =
var my_nic_states: seq[string] = @[]
for nic in my_nics:
let (ip, state) = getConnState(nic)
my_nic_states.add(nic & ":" & state & " " & ip)
let data = getObject()
let args = concat(my_nic_states,@["manage"])
let option = outputData(data, args)
if option in my_nic_states:
discard execCmd(mng_cmd)
case option:
of "manage":
discard execCmd(mng_cmd)
proc getNics*(): seq[string] =
var my_nics: seq[string] = @[]
for nic in os.walkDir("/sys/class/net/"):
let n = replace(nic.path, "/sys/class/net/", "")
my_nics.add(n)
if len(my_nics) > 0:
return my_nics
return @["no-nic"]
proc main() =
getNetInfo(getNics())
if isMainModule:
main()