我怎样才能用他的值加入一个字符串? [英] How can I join a string with his value?

查看:98
本文介绍了我怎样才能用他的值加入一个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我从SSH命令的输出中得到了这个字符串(还有很多具有所有卷和日期的节点:

First of all, I have this string from the output of an SSH command (there are a lot of more nodes with all the volumes and dates:

vserver           volume                                           last-success-op-end      
----------------- ------------------------------------------------ ------------------------ 
xxx_xxx_xxx_xxxxx trident_pvc_387e46bc_7fad_4424_95d4_ab15a3e156a8 Mon Jun 10 16:52:18 2020 
xxx_xxx_xxx_xxxxx trident_pvc_42816b6e_cd61_4929_a7c2_41de3f593c23 Mon Jun 15 16:52:35 2020 
xxx_xxx_xxx_xxxxx trident_pvc_5932a33a_ca9f_4131_8d2b_e465f195c633 Mon Jun 15 16:52:29 2020 
xxx_xxx_xxx_xxxxx trident_pvc_769d0605_1964_4dfe_9792_1d84e331519f Mon Jun 15 18:25:30 2020 

然后,我想将所有最后一次运行结束时间超过7天的服务器作为服务器,并接收如下输出:

Then, I would like to take all those servers where their last-success-op-end is older than 7 days, and receive an output like this one:

xxx_xxx_xxx_xxxxx.trident_pvc_387e46bc_7fad_4424_95d4_ab15a3e156a8= 7 days;

我尝试这样做:

import subprocess
import argparse
import sys
import re
import datetime
from subprocess import check_output

command = #ssh command that returns me that string 
output = check_output(command, shell=True)

#Here I take all the dates
dates_1 = str(re.findall('(Mon.*|Sun.*|Tue.*|Wed.*|Thu.*|Fri.*|Sat.*|Sun.*)', output)).replace("\\r", "").replace(" '", "'").replace("'", "").replace("[", "").replace("]", "")
dateslist = dates_1.split(",")


dates_list = [datetime.datetime.strptime(date, "%a %b %d %H:%M:%S %Y").date() for date in dateslist]

now = datetime.datetime.now().date()

deltas = [now-d for d in dates_list]

delta_days = [td.days for td in deltas]

results = map(int, delta_days)


print (results)

这样,我收到这样的输出一个:

With that, I receive an output like this one:

[3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 4, 4, 4, 3, 5, 5, 4, 4, 4, 4, 4, 4, 0, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 17, 17, 0, 0, 0, 0, 0, 10, 8, 3, 3, 4, 4, 4, 4, 4, 3, 7, 56, 0, 2, 28, 17, 1, 4, 2, 0, 2, 2, 2, 37, 2, 2, 2, 6, 2, 2, 2, 3, 2, 0, 2, 0, 2, 7, 0, 0, 1032, 0, 0, 26, 4, 3, 4, 4, 4, 0, 6, 4, 645, 241, 141, 141, 322, 303, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

但是我没有找到有关如何将这些值关联到他的vserver +卷并打印出来的信息...

But I am not finding information about how could I associate those values to his vserver + volume and print it...

有办法吗?

非常感谢您的帮助。

推荐答案

您的结果列表是一个列表,该列表的长度与从 ssh 命令输出。

Your results list is an a list that is equally long as the amount of lines you get from your ssh command output.

如果可以从输出以及列表,您可以使用列表理解和条件条件来选择您感兴趣的列表:

if you can retrieve the vserver + volume from the output as well as a list, you can use a list comprehension and a conditional to select the ones you are interested in:

vserver = str(re.findall(.... # here you would need to identify all vserver accesses
vserver_select = [vs for (vs, i) in enumerate(vserver) if result[i] > 7]

这将选择所有7天以上的服务器访问权限。

This selects all server accesses that are older than 7 days.

这篇关于我怎样才能用他的值加入一个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆