diff options
-rw-r--r-- | openssh-connectedpeers-html.lsp | 4 | ||||
-rw-r--r-- | openssh-model.lua | 55 |
2 files changed, 27 insertions, 32 deletions
diff --git a/openssh-connectedpeers-html.lsp b/openssh-connectedpeers-html.lsp index 363d726..7ee8fd9 100644 --- a/openssh-connectedpeers-html.lsp +++ b/openssh-connectedpeers-html.lsp @@ -23,8 +23,6 @@ for k,v in pairs(data.value) do io.write("<table>") io.write("<tr><td width='"..col1.."' style='font-weight:bold;'>Session user:</td><td>".. html.html_escape(v.tty[i].user) .. "</td></tr>\n") io.write("<tr><td width='"..col1.."' style='font-weight:bold;'>Session TTY:</td><td>".. html.html_escape(v.tty[i].tty) .. "</td></tr>\n") - io.write("<tr><td width='"..col1.."' style='font-weight:bold;'>Session Started:</td><td>".. html.html_escape(v.tty[i].time) .. "</td></tr>\n") - io.write("<tr><td width='"..col1.."' style='font-weight:bold;'>Session Idle:</td><td>".. html.html_escape(v.tty[i].idle) .. "</td></tr>\n") io.write("</table>") io.write("</td>\n") @@ -35,8 +33,6 @@ for k,v in pairs(data.value) do io.write("<table>") io.write("<tr><td width='"..col1.."' style='font-weight:bold;'>Session user:</td><td>No records</td></tr>\n") io.write("<tr><td width='"..col1.."' style='font-weight:bold;'>Session TTY:</td><td>No records</td></tr>\n") - io.write("<tr><td width='"..col1.."' style='font-weight:bold;'>Session Started:</td><td>No records</td></tr>\n") - io.write("<tr><td width='"..col1.."' style='font-weight:bold;'>Session Idle:</td><td>No records</td></tr>\n") io.write("<tr><td width='"..col1.."' style='font-weight:bold;'>Other:</td><td>This could be a sshfs session</td></tr>\n") io.write("</table>") diff --git a/openssh-model.lua b/openssh-model.lua index 7e8af51..a24d952 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 |