summaryrefslogtreecommitdiffstats
path: root/openssh-model.lua
diff options
context:
space:
mode:
Diffstat (limited to 'openssh-model.lua')
-rw-r--r--openssh-model.lua55
1 files changed, 27 insertions, 28 deletions
diff --git a/openssh-model.lua b/openssh-model.lua
index d672960..11bb508 100644
--- a/openssh-model.lua
+++ b/openssh-model.lua
@@ -127,51 +127,50 @@ function mymodule.list_conn_peers()
local output = cfe({ type="structure", value={}, label="Connected peers" })
local netstat = {}
local ps = {}
- local who = {}
config = mymodule.read_config()
- local f = modelfunctions.run_executable({"netstat", "-lnaW"})
+
+ local f = modelfunctions.run_executable({"ps"})
+ for i,line in ipairs(format.search_for_lines(f, "sshd:")) do
+ local pid, user, tty = string.match(line, "^%s*(%d+)%s+%S+%s+%S+%s+%S+%s+([^@ ]+)@?(%S*)")
+ if pid then
+ ps[pid] = {user=user, tty=tty}
+ end
+ end
+
+ f = modelfunctions.run_executable({"netstat", "-tnp"})
local flines = format.search_for_lines(f, "ESTABLISHED")
- local g = modelfunctions.run_executable({"netstat", "-laW"})
+ local g = modelfunctions.run_executable({"netstat", "-t"})
local glines = format.search_for_lines(g, "ESTABLISHED")
for i,line in ipairs(flines) do
- local loc, peer = string.match(line, "^%S+%s+%S+%s+%S+%s+(%S+)%s+(%S+)")
+ local loc, peer, pid = string.match(line, "^%S+%s+%S+%s+%S+%s+(%S+)%s+(%S+)%s+%S+%s+(%d+)")
if loc then
peer = string.match(peer, "%d+%.%d+%.%d+%.%d+")
if string.find(loc, ":"..config.value.Port.value.."$") and peer then
if not netstat[peer] then
local name = string.match(glines[i], "^%S+%s+%S+%s+%S+%s+%S+%s+(%S+)")
name = string.gsub(name, ":.*", "")
- netstat[peer] = {cnt=0, name=name}
+ netstat[peer] = {cnt=0, name=name, tty={}}
end
netstat[peer].cnt = netstat[peer].cnt + 1
+ if ps[pid] then
+ -- For root, the pid will match and contain the tty
+ -- For other users, we will find the tty in another process soon after
+ netstat[peer].tty[#netstat[peer].tty+1] = {user=ps[pid].user, tty=ps[pid].tty}
+ if ps[pid].tty == "" then
+ for j=tostring(pid)+1, tostring(pid)+5 do
+ local p = tostring(j)
+ if ps[p] and ps[p].user == ps[pid].user then
+ netstat[peer].tty[#netstat[peer].tty].tty = ps[p].tty
+ break
+ end
+ end
+ end
+ end
end
end
end
- f = modelfunctions.run_executable({"ps"})
- for i,line in ipairs(format.search_for_lines(f, "sshd:")) do
- table.insert(ps, string.match(line,"@(%S+)"))
- end
-
- local who = modelfunctions.run_executable({"who"})
for peer,v in pairs(netstat) do
- if not (netstat[peer].tty) then netstat[peer].tty = {} end
- for line in string.gmatch(who, "[^\n]+") do
- if string.find(line, peer) or (v.name ~= "" and string.find(line, v.name)) then
- for j,l in ipairs(ps) do
- if string.find(line, l) then
- local user,tty,idle,time = string.match(line, "^(%S*)%s*(%S*)%s*(%S*)%s*(%S*%s*%S*%s*%S*)")
- table.insert(netstat[peer].tty, {
- user=user,
- tty=tty,
- idle=idle,
- time=time,
- })
- break
- end
- end
- end
- end
output.value[#output.value+1] = v
output.value[#output.value].host = peer
end