python compile命令_Python:如何使用compile和spli解析linux命令的输出

我需要用重新编译并拆分以接收cmd并打印出ip地址(最后一列)和日期和时间,并将其转换为epoch时间。

我用的只是重新编译但有人告诉我要使用split命令来简化。。只是想找些指导?

这就是输出的样子host:~ # last -a -F | egrep -v "boot|wtmp|tty"

root pts/2 Fri Jun 19 10:32:13 2015 still logged in xx.x.xx.xx

root pts/0 Fri Jun 19 08:22:29 2015 still logged in xx.xx.xx.xx

root pts/5 Thu Jun 18 10:09:30 2015 - Thu Jun 18 17:20:52 2015 (07:11) xx.xx.xx.xx

root pts/4 Thu Jun 18 09:53:33 2015 - Thu Jun 18 17:04:53 2015 (07:11) xx.xx.xx.xx

last_re = re.compile(r'(?P\S+)\s+(?P\/.+)\s(?P\S+)\s+(?P)\s+(?P\d+)\s+(?P(\d\:\d)\s+(?P(\s|-)\s+(?P(\d\:\d)\s+(?P)\s+(?P(\(\d\:\d\))\s+(?P(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$)')

cmd = 'last -a -F | egrep -v "boot|wtmp|tty"'

try:

status, output = commands.getstatusoutput(cmd)

print last_re;

if not status:

output_lines = output.split('\n')

m = last_re.search(output_lines[1])

if m:

print "" % (m.group('day'), m.group('month'), m.group('stime'), m.group('ipaddress'))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值