get centos uptime of a server in datetime format python
def get_uptime(session=None):
"""
Get the uptime of system in secs
:param session: VM session or remote session object, None for host
:return: uptime of system in float, None on error
"""
cmd = "cat /proc/uptime"
if session:
uptime = session.cmd_output(cmd)
else:
try:
uptime = process.run(cmd, shell=True).stdout_text
except process.CmdError:
return None
return float(uptime.split()[0])