简体   繁体   English

python 中的命令行 output 逐行读取,将行中的每个值放入数组并调用

[英]Command line output in python read line by line with each value in the line put into an array and recalled

I'm trying to capture the output of a command and have each line in an array so I can evaluate on a position in the line as a constant if that makes sense.我正在尝试捕获命令的 output 并将每一行放在一个数组中,因此如果有意义的话,我可以将行中的 position 作为常数进行评估。 It would treat the output almost as columns.它将 output 几乎视为列。

I've basically assigned a command output to a variable and split it on each line.我基本上已经将命令 output 分配给一个变量,并将其拆分为每一行。 I can do a split and get the first "column" but I can't seem to get past this.我可以进行拆分并获得第一个“列”,但我似乎无法超越这一点。

I've broken the code into sections to show the output I'm seeing.我已将代码分成几部分以显示我看到的 output。 I'm running an aureport to evaluate some login activity.我正在运行 aureport 来评估一些登录活动。

#!/usr/bin/python

import os
import platform
import fnmatch
import subprocess

results = subprocess.check_output("aureport -ts yesterday -te now  -au -i --failed", shell=True).splitlines()
print(results)


for line in results:
  print(line)

for line in results:
  arg = line.split(' ')
  item1 = arg[0]
  print(item1)
exit(0)

The results I get are:我得到的结果是:

['', 'Authentication Report', '============================================', '# date time acct host term exe success event', '============================================', '1. 00/00/00 06:49:24 some_user ? host /some/command no some_pid', '2. 00/00/00 06:49:27 some_user ? host /some/command no some_pid', '3. 00/00/00 06:49:29 some_user ? host /some/command no some_pid', '4. 00/00/00 07:10:44 some_user ? host /some/command no some_pid', '5. 00/00/00 07:10:49 some_user ? host /some/command no some_pid', '6. 00/00/00 07:10:53 some_user ? host /some/command no some_pid', '7. 00/00/00 07:11:14 some_user ? host /some/command no some_pid', '8. 00/00/00 07:11:16 some_user ? host /some/command no some_pid', '9. 00/00/00 07:11:25 some_user ? host /some/command no some_pid', '10. 00/00/00 06:19:44 some_user ? host /some/command no some_pid', '11. 00/00/00 06:19:47 some_user ? host /some/command no some_pid', '12. 00/00/00 06:20:59 some_user host command /some/command no some_pid', '13. 00/00/00 06:21:01 some_user ::1 command /some/command no some_pid']

Authentication Report
============================================
# date time acct host term exe success event
============================================
1. 00/00/00 06:49:24 some_user ? host /some/command no some_pid
2. 00/00/00 06:49:27 some_user ? host /some/command no some_pid
3. 00/00/00 06:49:29 some_user ? host /some/command no some_pid
4. 00/00/00 07:10:44 some_user ? host /some/command no some_pid
5. 00/00/00 07:10:49 some_user ? host /some/command no some_pid
6. 00/00/00 07:10:53 some_user ? host /some/command no some_pid
7. 00/00/00 07:11:14 some_user ? host /some/command no some_pid
8. 00/00/00 07:11:16 some_user ? host /some/command no some_pid
9. 00/00/00 07:11:25 some_user ? host /some/command no some_pid
10. 00/00/00 06:19:44 some_user ? host /some/command no some_pid
11. 00/00/00 06:19:47 some_user ? host /some/command no some_pid
12. 00/00/00 06:20:59 some_user host command /some/command no some_pid
13. 00/00/00 06:21:01 some_user ::1 command /some/command no some_pid

Authentication
============================================
#
============================================
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.

What I would like to do is create some evaluations on (as an example) the 'column' in the output that shows 'no' and then print output from the rest of the line.我想做的是在(例如)output 中显示“否”的“列”上创建一些评估,然后从 Z65E8800B5C6800AAD896F8AZ2B 行打印 output。 Example: if arg[7] == 'no': print(arg[4])示例: if arg[7] == 'no': print(arg[4])

What I can't seem to wrap my head around is getting the output evaluated on line by line so arg[7] always refers to the 'no' column etc. I did manage to get just the first column by splitting on a space however I'm pretty sure it's coincidence:)我似乎无法理解的是让 output 逐行评估,因此 arg[7] 总是指“否”列等。我确实设法通过拆分空间来获得第一列我很确定这是巧合:)

If I understand your question, all you need to do is put your if statement within your for loop.如果我理解你的问题,你需要做的就是把你的if语句放在你的for循环中。 Then it will evaluate the output line by line.然后它将逐行评估 output。 So your last section could look like this:因此,您的最后一部分可能如下所示:

for line in results[5:]:
    arg = line.split()
    if arg[7] == 'no':
        print(arg[4])

Note I changed line.split(' ') to line.split() as the default action of split() is to separate by whitespace.注意我将line.split(' ')更改为line.split()因为split()的默认操作是用空格分隔。

The [4:] removes the lines you aren't considering: '#####...' , etc. [4:]删除了您不考虑的行: '#####...'等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM