简体   繁体   English

从 Python 中的 json 中提取值

[英]Extract value from json in Python

I am extracting the value input from string using python.我正在使用 python 从字符串中提取值输入。

{"eventid":"cowrie.command.input","input":"cd /dev/shm; cat .s || cp /bin/echo .s; /bin/busybox KUHJY","message":"CMD: cd /dev/shm; cat .s || cp /bin/echo .s; /bin/busybox KUHJY","sensor":"cowrieHoneypot2","timestamp":"2021-05-06T00:20:09.535217Z","src_ip":"76.250.199.133","session":"e3e61e8606ed"}
{"eventid":"cowrie.command.input","input":"tftp; wget; /bin/busybox KUHJY","message":"CMD: tftp; wget; /bin/busybox KUHJY","sensor":"cowrieHoneypot2","timestamp":"2021-05-06T00:20:09.729719Z","src_ip":"76.250.199.133","session":"e3e61e8606ed"}

Now, what regex should I use as in one string the value of the input is:现在,我应该在一个字符串中使用什么正则表达式,输入的值是:

cd /dev/shm; cat .s || cp /bin/echo .s; /bin/busybox KUHJY

Whereas, the value of the input in the next string is:而下一个字符串中的输入值为:

tftp; wget; /bin/busybox KUHJY

So, how can I simply extract the value of Input?那么,我怎样才能简单地提取 Input 的值呢? Some sample strings are given in this file .文件中给出了一些示例字符串。 Thanks in advance.提前致谢。

This really seems like more of a problem of parsing your JSON content than using regex:与使用正则表达式相比,这似乎更像是解析 JSON 内容的问题:

import json

text = '{"eventid":"cowrie.command.input","input":"cd /dev/shm; cat .s || cp /bin/echo .s; /bin/busybox KUHJY","message":"CMD: cd /dev/shm; cat .s || cp /bin/echo .s; /bin/busybox KUHJY","sensor":"cowrieHoneypot2","timestamp":"2021-05-06T00:20:09.535217Z","src_ip":"76.250.199.133","session":"e3e61e8606ed"}'
json = json.loads(text)
inp = json["input"]
print(inp)

This prints:这打印:

cd /dev/shm; cat .s || cp /bin/echo .s; /bin/busybox KUHJY

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

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