python练习-(秒转时分秒,时分秒转秒)-对比linux中文件的上次更改时间跟当前时间相差多久。
具体代码如下>
import paramiko,re,datetime,time
ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname="10.124.198.75",port=8122,username="admin",password='****',timeout=2)
stdin,stdout,stderr=ssh.exec_command("stat /etc/passwd | grep Change")
stdin_sys,stdout_sys,stderr_sys=ssh.exec_command("date +%s")
change_time=re.search('Change: (.+)\\.',stdout.read().decode('utf-8')).group(1)
time1=datetime.datetime.strptime(change_time,"%Y-%m-%d %H:%M:%S")
change_time_new=str(time.mktime(time1.timetuple())).strip('.0')
sys_time=stdout_sys.read().decode('utf-8').strip()
time_stay=int(sys_time) - int(change_time_new)
print("距离上次更改:",datetime.timedelta(seconds=time_stay))
详解>
import paramiko,re,datetime,time #连接linux服务器
ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #允许连接不在hosts文件中的主机
ssh.connect(hostname="10.124.198.75",port=8122,username="admin",password='****') #执行命令,获取到当前的系统时间(秒),和对应文件的修改时间。
#命令执行完后,会存储在stdout里面。
stdin,stdout,stderr=ssh.exec_command("stat /etc/passwd | grep Change")
stdin_sys,stdout_sys,stderr_sys=ssh.exec_command("date +%s") #读取stdout中的内容,并进行正则匹配,得到想要文件更改时间,并存储到变量中,格式如:(2019-01-23 16:44:15)
change_time=re.search('Change: (.+)\\.',stdout.read().decode('utf-8')).group(1) #将时间转换为秒,,"%Y-%m-%d %H:%M:%S" 此处的定义根据我们时间的格式进行更改(2019-01-23 16:44:15)
time1=datetime.datetime.strptime(change_time,"%Y-%m-%d %H:%M:%S") change_time_new=str(time.mktime(time1.timetuple())).strip('.0') #获取系统当前时间,并将当前时间减去上次修改的时间(秒 - 秒)
sys_time=stdout_sys.read().decode('utf-8').strip()
time_stay=int(sys_time) - int(change_time_new) #秒转换为可识别的时间(距离上次更改: 41 days, 21:30:03)
print("距离上次更改:",datetime.timedelta(seconds=time_stay))
#执行测试
距离上次更改,41天22小时3分钟41秒

python练习-(秒转时分秒,时分秒转秒)-对比linux中文件的上次更改时间跟当前时间相差多久。的更多相关文章
- PHP 将秒数转换成时分秒
将秒数转换成时分秒,PHP提供了一个函数gmstrftime,不过该函数仅限于24小时内的秒数转换.对于超过24小时的秒数,我们应该怎么让其显示出来呢,例如 34:02:02 $seconds = 3 ...
- H面试程序(1)编写一个函数,要求输入年月日时分秒,输出该年月日时分秒的 下一秒
编写一个函数,要求输入年月日时分秒,输出该年月日时分秒的下一秒. 如输入 2004 年 12 月 31 日 23 时 59 分 59 秒,则输出 2005年 1 月 1 日 0 时 0 分 0 秒. ...
- PHP函数gmstrftime()将秒数转换成天时分秒
http://yangjunwei.com/a/930.html PHP函数gmstrftime()将秒数转换成天时分秒 一个应用场景需要用到倒计时的时分秒,比如新浪微博授权有效期剩余: 7天16 ...
- Why Python's Integer Division Floors ---- python int(6/-132)时答案不一致,向下取整
leetcode150题中有一个步骤: int(6/-132) == 0 or ==-1? 在自己本地python3环境跑是int(6/-132) =0,但是提交的时候确实-1. 查找相关资料解惑: ...
- Java 获取各时区时间,获取当前时间到格林威治时间1970年01月01日00时00分00秒的秒数
格林威治时间即UTC/GMT时间,1970年01月01日00时00分00秒(即UTC+8的北京时间1970年01月01日08时00分00秒)计算代码如下: /** * 获取指定时间到格林威治时间的秒数 ...
- python解释器的安装;python2与python3同时在环境变量中时的解决方案
新文档 html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,addres ...
- python使用xlrd读取excel数据时,整数变小数的解决办法
python使用xlrd读取excel数据时,整数变小数: 解决方法: 1.有个比较简单的就是在数字和日期的单元格内容前加上一个英文的逗号即可.如果数据比较多,也可以批量加英文逗号的前缀(网上都有方法 ...
- python基础:re模块匹配时贪婪和非贪婪模式
python贪婪和非贪婪 正则表达式通常用于在文本中查找匹配的字符串.Python里数量词默认是贪婪的(在少数语言里也可能是默认非贪婪),总是尝试匹配尽可能多的字符:非贪婪则相反,总是尝试匹配尽可能少 ...
- javascript 获得以秒计的视频时长
<!DOCTYPE html> <html> <body> <h3>演示如何访问 VIDEO 元素</h3> <video id=&q ...
随机推荐
- persistence.xml模板配置
1.右键创建的persistence.xml 2.选择2.0版本的模板 3.复制右侧代码,写入xml文件中 <?xml version="1.0" encoding=&quo ...
- 【url ---lib___】笔趣阁(抓取斗罗大陆完整)和(三寸天堂)
# coding=gbk #因为在黑屏下执行,所以代码会使用GBK url='http://www.biquge.info/10_10218/' UA={"User-Agent": ...
- 【Git】三、版本回退&撤消修改&文件删除
提要 //查看git操作日志 $ git log //单行格式查看操作日志 $ git log --pretty=oneline //还原操作到上一次版本,有几个^就上几次 $ git reset - ...
- How to Change the Default Theme Appearance [editing with no theme]
Windows Live Writer has the ability to allow users to see their blog as it'd be posted as they are c ...
- Django阶段总结与Ajax
一.路由控制 二.视图层 三.模板层 四.模型层.单表操作.多表操作 五.什么是ajax 一.路由控制 补充点(什么是web应用?) 网站:BS架构应用程序:B是浏览器 S:server(实现了ws ...
- 将服务端select设置为非阻塞,处理更多业务
服务端代码: #include<WinSock2.h> #include<Windows.h> #include<vector> #include<stdio ...
- tensorflow模型的保存与恢复,以及ckpt到pb的转化
转自 https://www.cnblogs.com/zerotoinfinity/p/10242849.html 一.模型的保存 使用tensorflow训练模型的过程中,需要适时对模型进行保存,以 ...
- (转)VMware虚拟机三种网络模式的区别及配置方法;
我的一点实际经验理解桥接和NAT 桥接是虚拟机完全作为一个独立的地址接在局域网中,NAT是虚拟机依赖宿主主机地址转换的一种方式 例子我的虚拟机如果用桥接模式,连接外部网站如百度时会提示此pc没有装公司 ...
- log4j2 日志打两遍的问题
在使用log4j2的时候,一般都需要不同的日志分类打印不同的日志等级,如下面的配置 <!-- 用于指定log4j自动重新配置的监测间隔时间,单位是秒 --> <configurati ...
- hdu3715 Go Deeper[二分+2-SAT]/poj2723 Get Luffy Out[二分+2-SAT]
这题转化一下题意就是给一堆形如$a_i + a_j \ne c\quad (a_i\in [0,1],c\in [0,2])$的限制,问从开头开始最多到哪条限制全是有解的. 那么,首先有可二分性,所以 ...