簡體   English   中英

如何從命令提示符中的值存儲在一個空的python字典中?

[英]how to store values from command prompt in an empty python dictionary?

這是代碼:

from subprocess import Popen, PIPE

p1 = Popen(["sysctl", "-a"], stdout=PIPE)
p2 = Popen(["grep", "net.ipv4.icmp_echo_ignore_all"], stdin=p1.stdout, stdout=PIPE)
output = p2.communicate()[0]
print output

p1 = Popen(["sysctl", "-a"], stdout=PIPE)
p3 = Popen(["grep", "net.ipv4.icmp_echo_ignore_broadcasts"], stdin=p1.stdout, stdout=PIPE)
output1 = p3.communicate()[0]
print output1

p1 = Popen(["sysctl", "-a"], stdout=PIPE)
p4 = Popen(["grep", "net.ipv4.ip_forward"], stdin=p1.stdout, stdout=PIPE)
output2 = p4.communicate()[0]
print output2

p1 = Popen(["sysctl", "-a"], stdout=PIPE)
p5 = Popen(["grep", "net.ipv4.tcp_syncookies"], stdin=p1.stdout, stdout=PIPE)
output3 = p5.communicate()[0]
print output3

p1 = Popen(["sysctl", "-a"], stdout=PIPE)
p6 = Popen(["grep", "net.ipv4.conf.all.rp_filter"], stdin=p1.stdout, stdout=PIPE)
output4 = p6.communicate()[0]
print output4

p1 = Popen(["sysctl", "-a"], stdout=PIPE)
p7 = Popen(["grep", "net.ipv4.conf.all.log.martians"], stdin=p1.stdout, stdout=PIPE)
output5 = p7.communicate()[0]
print output5

p1 = Popen(["sysctl", "-a"], stdout=PIPE)
p8 = Popen(["grep", "net.ipv4.conf.all.secure_redirects"], stdin=p1.stdout, stdout=PIPE)
output6 = p8.communicate()[0]
print output6

p1 = Popen(["sysctl", "-a"], stdout=PIPE)
p9 = Popen(["grep", "net.ipv4.conf.all.send_redirects"], stdin=p1.stdout, stdout=PIPE)
output7 = p9.communicate()[0]
print output7

p1 = Popen(["sysctl", "-a"], stdout=PIPE)
p10 = Popen(["grep", "net.ipv4.conf.all.accept_source_route"], stdin=p1.stdout,  stdout=PIPE)
output8 = p10.communicate()[0]
print output8

p1 = Popen(["sysctl", "-a"], stdout=PIPE)
p11 = Popen(["grep", "net.ipv4.conf.all.accept_redirects"], stdin=p1.stdout, stdout=PIPE)
output9 = p11.communicate()[0]
print output9


p1 = Popen(["sysctl", "-a"], stdout=PIPE)
p12 = Popen(["grep", "net.ipv4.tcp_max_syn_backlog"], stdin=p1.stdout, stdout=PIPE)
output10 = p12.communicate()[0]
print output10

current_kernel_para = dict()            #new dictionary to store the above kernel parameters

上面程序的輸出是:

net.ipv4.icmp_echo_ignore_all = 0

net.ipv4.icmp_echo_ignore_broadcasts = 1

net.ipv4.ip_forward = 0

net.ipv4.tcp_syncookies = 1

net.ipv4.conf.all.rp_filter = 0

net.ipv4.conf.all.log_martians = 0

net.ipv4.conf.all.secure_redirects = 1

net.ipv4.conf.all.send_redirects = 1

net.ipv4.conf.all.accept_source_route = 0

net.ipv4.conf.all.accept_redirects = 1

net.ipv4.tcp_max_syn_backlog = 512

我想將這些值存儲在字典“ current_kernel_para”中。 所需的輸出是:{net.ipv4.icmp_echo_ignore_all:0,net.ipv4.icmp_echo_ignore_broadcasts:1}等。

請幫忙。 提前致謝。

您可以將字符串拆分為“ =”,然后將第一個用作鍵,將第二個標記用作值。

在'='上分割輸出

x = output.split(' = ')

這將給:

['net.ipv4.conf.all.send_redirects', '1']

然后,您可以將所有這些列表加在一起並使用:

x = ['net.ipv4.icmp_echo_ignore_all', '0', 'net.ipv4.conf.all.send_redirects', '1'...]
dict_x = dict(x[i:i+2] for i in range(0, len(x), 2))
current_kernel_para = {}
paras = ["net.ipv4.icmp_echo_ignore_all",
        "net.ipv4.icmp_echo_ignore_broadcasts", #...
       ]
for para in paras:
    p1 = Popen(["sysctl", "-a"], stdout=PIPE)
    p2 = Popen(["grep", para], stdin=p1.stdout, stdout=PIPE)
    output = p2.communicate()[0]
    output.strip()
    k, v = map(strip, output.split('='))
    current_kernel_para[k] = v

無需將每個輸出分配給新變量,只需將其收集為列表即可。 記住要刪除尾隨\\n

outputs = []

p1 = Popen(["sysctl", "-a"], stdout=PIPE)
p2 = Popen(["grep", "net.ipv4.icmp_echo_ignore_all"], stdin=p1.stdout, stdout=PIPE)
outputs.append(p2.communicate()[0].strip('\n'))

p1 = Popen(["sysctl", "-a"], stdout=PIPE)
p3 = Popen(["grep", "net.ipv4.icmp_echo_ignore_broadcasts"], stdin=p1.stdout, stdout=PIPE)
outputs.append(p3.communicate()[0].strip('\n'))

這給

>>> outputs
['net.ipv4.icmp_echo_ignore_all = 0', 'net.ipv4.icmp_echo_ignore_broadcasts = 1']

然后,可以在=處拆分列表中的每個字符串,並將結果收集為list of lists

outputs_list=[x.split('=') for x in outputs]

>>> outputs_list
[['net.ipv4.icmp_echo_ignore_all ', ' 0'], ['net.ipv4.icmp_echo_ignore_broadcasts ', ' 1']]

很好,但是有前導/后綴空間。 讓我們也去掉那些

outputs_list=[[y.strip() for y in x.split('=')] for x in outputs]

這給

>>> outputs_list
[['net.ipv4.icmp_echo_ignore_all', '0'], ['net.ipv4.icmp_echo_ignore_broadcasts', '1']]

也將dict()構造函數傳遞給它以形成字典

>>> dict(outputs_list)
{'net.ipv4.icmp_echo_ignore_all': '0', 'net.ipv4.icmp_echo_ignore_broadcasts': '1'}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM