diff --git a/nselib/dhcp.lua b/nselib/dhcp.lua index 8044df7ad..f40b2484f 100644 --- a/nselib/dhcp.lua +++ b/nselib/dhcp.lua @@ -224,24 +224,7 @@ local function read_time(data, pos, length) end pos, result = bin.unpack(">I", data, pos) - -- This code was mostly taken from snmp-sysdescr.nse. It should probably be abstracted into stdnse.lua [TODO] - local days, hours, minutes, seconds, htime, mtime, stime - days = math.floor(result / 86400) - htime = math.fmod(result, 86400) - hours = math.floor(htime / 3600) - mtime = math.fmod(htime, 3600) - minutes = math.floor(mtime / 60) - seconds = math.fmod(mtime, 60) - - local dayLabel - - if days == 1 then - dayLabel = "day" - else - dayLabel = "days" - end - - return pos, string.format("%d %s, %d:%02d:%02d", days, dayLabel, hours, minutes, seconds) + return pos, stdnse.format_time(result) end ---Read a list of static routes. Each of them are a pair of IP addresses, a destination and a diff --git a/nselib/msrpc.lua b/nselib/msrpc.lua index 6d061cef5..bdee9b8cb 100644 --- a/nselib/msrpc.lua +++ b/nselib/msrpc.lua @@ -4682,13 +4682,7 @@ function get_server_stats(host) -- Get the period and convert it to a proper time offset stats['period'] = os.time() - stats['start'] - if(stats['period'] > 60 * 60 * 24) then - stats['period_str'] = string.format("%dd%dh%02dm%02ds", stats['period'] / (60*60*24), (stats['period'] % (60*60*24)) / 3600, (stats['period'] % 3600) / 60, stats['period'] % 60) - elseif(stats['period'] > 60 * 60) then - stats['period_str'] = string.format("%dh%02dm%02ds", stats['period'] / 3600, (stats['period'] % 3600) / 60, stats['period'] % 60) - else - stats['period_str'] = string.format("%02dm%02ds", stats['period'] / 60, stats['period'] % 60) - end + stats.period_str = stdnse.format_time(stats.period) -- Combine the 64-bit values stats['bytessent'] = bit.bor(bit.lshift(stats['bytessent_high'], 32), stats['bytessent_low']) diff --git a/scripts/gkrellm-info.nse b/scripts/gkrellm-info.nse index 288133d17..229f835ec 100644 --- a/scripts/gkrellm-info.nse +++ b/scripts/gkrellm-info.nse @@ -80,25 +80,6 @@ local function getOrderPos(tag) return 1 end -local function minutesToUptime(value) - local days = math.floor(value / (60 * 24)) - local htime = math.fmod(value, (60 * 24)) - local hours = math.floor(htime / 60) - local mtime = math.fmod(htime, 60) - local minutes = math.floor(mtime) - local output = "" - if ( days > 0 ) then - output = output .. ("%d days, "):format(days) - end - if ( hours > 0 ) then - output = output .. ("%d hours, "):format(hours) - end - if ( minutes > 0 ) then - output = output .. ("%d minutes"):format(minutes) - end - return output -end - local function decodeTag(tag, lines) local result = { name = long_names[tag] } local order @@ -131,7 +112,7 @@ local function decodeTag(tag, lines) "version" == tag ) then return ("%s: %s"):format(long_names[tag], lines[1]) elseif ( "uptime" == tag ) then - return ("%s: %s"):format(long_names[tag], minutesToUptime(lines[1])) + return ("%s: %s"):format(long_names[tag], stdnse.format_time(lines[1] * 60)) elseif ( "mem" == tag ) then local total, used = table.unpack(stdnse.strsplit("%s", lines[1])) if ( not(total) or not(used) ) then diff --git a/scripts/smb-enum-sessions.nse b/scripts/smb-enum-sessions.nse index 922e2cea9..cea9ea107 100644 --- a/scripts/smb-enum-sessions.nse +++ b/scripts/smb-enum-sessions.nse @@ -304,23 +304,15 @@ action = function(host) local time = sessions[i]['time'] if(time == 0) then time = "[just logged in, it's probably you]" - elseif(time > 60 * 60 * 24) then - time = string.format("%dd%dh%02dm%02ds", time / (60*60*24), (time % (60*60*24)) / 3600, (time % 3600) / 60, time % 60) - elseif(time > 60 * 60) then - time = string.format("%dh%02dm%02ds", time / 3600, (time % 3600) / 60, time % 60) else - time = string.format("%02dm%02ds", time / 60, time % 60) + time = stdnse.format_time(time) end local idle_time = sessions[i]['idle_time'] if(idle_time == 0) then idle_time = "[not idle]" - elseif(idle_time > 60 * 60 * 24) then - idle_time = string.format("%dd%dh%02dm%02ds", idle_time / (60*60*24), (idle_time % (60*60*24)) / 3600, (idle_time % 3600) / 60, idle_time % 60) - elseif(idle_time > 60 * 60) then - idle_time = string.format("%dh%02dm%02ds", idle_time / 3600, (idle_time % 3600) / 60, idle_time % 60) else - idle_time = string.format("%02dm%02ds", idle_time / 60, idle_time % 60) + idle_time = stdnse.format_time(idle_time) end table.insert(sessions_output, string.format("%s is connected from %s for %s, idle for %s", sessions[i]['user'], sessions[i]['client'], time, idle_time)) diff --git a/scripts/snmp-sysdescr.nse b/scripts/snmp-sysdescr.nse index 0122f16e7..7f2702f06 100644 --- a/scripts/snmp-sysdescr.nse +++ b/scripts/snmp-sysdescr.nse @@ -1,8 +1,8 @@ -local math = require "math" local nmap = require "nmap" local shortport = require "shortport" local snmp = require "snmp" local string = require "string" +local stdnse = require "stdnse" description = [[ Attempts to extract system information from an SNMP version 1 service. @@ -97,24 +97,7 @@ action = function(host, port) return end - local days, hours, minutes, seconds, htime, mtime, stime - days = math.floor(uptime / 8640000) - htime = math.fmod(uptime, 8640000) - hours = math.floor(htime / 360000) - mtime = math.fmod(htime, 360000) - minutes = math.floor(mtime / 6000) - stime = math.fmod(mtime, 6000) - seconds = stime / 100 - - local dayLabel - - if days == 1 then - dayLabel = "day" - else - dayLabel = "days" - end - - result = result .. "\n" .. string.format(" System uptime: %d %s, %d:%02d:%05.2f (%s timeticks)", days, dayLabel, hours, minutes, seconds, tostring(uptime)) + result = result .. "\n" .. string.format(" System uptime: %s (%s timeticks)", stdnse.format_time(uptime, 100), tostring(uptime)) return result end