0

コードは次のとおりです。

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} など。

助けてください。前もって感謝します。

4

4 に答える 4

0

各出力を新しい変数に割り当てるのではなく、単にリストとして収集します。末尾を削除することを忘れないでください\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'}
于 2013-05-23T09:33:01.003 に答える
0

「=」で出力を分割します

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))
于 2013-05-23T09:11:41.840 に答える
0
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
于 2013-05-23T09:18:06.163 に答える