如何通过选择key:value对来获取json横幅广告

时间:2019-05-19 05:59:03

标签: json jq

我有一个json横幅文件“ extract_3month_fromshodan.json”,其中包含多个标签的键值,即ip_str = XXX.XX.XXX.XX,port = 80,timestamp =“ 2018-08-11T04:56:17.312039 “,数据=“ 210!连接成功”等。这样,文件中的横幅将包含近400个IP。

source / json标语(extract_3month_fromshodan.json)文件的示例:

{
  "asn": "AS17676",
  "hash": -619087650,
  "ip": 2120548325,
  "isp": "Softbank BB",
  "transport": "udp",
  "data": "HTTP/1.1 200 OK\r\nCache-Control: max-age=120\r\nST:  upnp:rootdevice\r\nUSN: uuid:12342409-1234-1234-5678-ee1234cc5678::upnp:rootdevice\r\nEXT:\r\nServer: miniupnpd/1.0 UPnP/1.0\r\nLocation: http://192.168.2.1:52869/picsdesc.xml\r\n\r\n",
  "port": 1900,
  "hostnames": [
  "softbank126100255229.bbtec.net"
 ],
  "location": {
  "city": "Toyota",
  "region_code": "01",
  "area_code": null,
  "longitude": 137.14999999999998,
  "country_code3": "JPN",
  "latitude": 35.08330000000001,
  "postal_code": "457-0844",
  "dma_code": null,
  "country_code": "JP",
  "country_name": "Japan"
 },
  "timestamp": **"2018-09-12T15:42:34.012436",**
  "domains": [
  "bbtec.net"
 ],
  "org": "XXXXXX BB",
  "os": null,
  "_shodan": {
  "crawler": "d264629436af1b777b3b513ca6ed1404d7395d80",
  "options": {},
  "module": "upnp",
  "id": null
 },
  "opts": {},
  "ip_str": **"126.100.255.229"**
}

{
 "asn": "AS17676",
 "hash": 1371060454,
 "ip": 2120509894,
 "isp": "Softbank BB",
 "transport": "udp",
 "data": "HTTP/1.1 200 OK\r\nCache-Control: max-age=1800\r\nST: upnp:rootdevice\r\nUSN: uuid:63041253-1019-2006-1228-00018efed688::upnp:rootdevice\r\nEXT:\r\nServer: OS 1.0 UPnP/1.0 Realtek/V1.3\r\nLocation: http://192.168.2.1:52881/simplecfg.xml\r\n\r\n",
 "port": 1900,
 "hostnames": [
 "softbank126100105198.bbtec.net"
],
 "location": {
 "city": "Yamashitacho",
 "region_code": "18",
 "area_code": null,
 "longitude": 130.55,
 "country_code3": "JPN",
 "latitude": 31.58330000000001,
 "postal_code": "892-0816",
 "dma_code": null,
 "country_code": "JP",
 "country_name": "Japan"
},
 "timestamp": **"2018-08-11T04:56:17.312039"**,
 "domains": [
 "bbtec.net"
],
 "org": "Softbank BB",
 "os": null,
 "_shodan": {
 "crawler": "6ff540e4d43ec69d8de2a7b60e1de2d9ddb406dc",
 "options": {},
 "module": "upnp",
 "id": null
 },
 "opts": {},
 "ip_str": **"126.100.105.198"**
}

现在,我想通过过滤参数ip_str =“ 126.100.105.198”和timestamp =“ 2018-08-11T04:56:17.312039”从上面的源json文件(extract_3month_fromshodan.json)中获取另一个新的json横幅。 每个ip_str和时间戳的迭代值将来自单独的.csv和/或.txt文件。并且需要将输出(已过滤的横幅)保存为json格式。

我到目前为止所做的:

jq '. | select (.timestamp="2018-08-11T04:56:17.312039") | select(.ip_str==""12X.10X.XXX.X9X")' extract_3month_fromshodan.json > all.json

这样,我需要获取几乎290倍的ip_str,时间戳值,这些值保存在csv和.txt文件中。我所做的是针对单个ip_str和时间戳。但是我无法将上述命令作为循环运行。

预期输出:

我应该从主json横幅(包含500多个IP)中提取/过滤json横幅,包括所有相关字段,其中包含290个IP和时间戳(保存在csv或txt文件中)。提取应自动完成,例如按一个/一组代码执行循环命令。循环的值(时间戳和ip_str)将来自.csv或.txt文件。

对于此处的迷你用例(从两个中过滤出1个),在输入中,我有两个IP的输入标题,即126.100.255.229和126.100.105.198。现在,在运行循环命令后,我应该获得如下所示的ip_str = 126.100.105.198的横幅,其时间戳= 2018-08-11T04:56:17.312039。在实际情况下,我将在一个json文件中拥有超过500个IP和时间戳的横幅,我必须过滤290个IP和时间戳。

输出:

{
  "asn": "AS17676",
  "hash": 1371060454,
  "ip": 2120509894,
  "isp": "Softbank BB",
  "transport": "udp",
  "data": "HTTP/1.1 200 OK\r\nCache-Control: max-age=1800\r\nST: upnp:rootdevice\r\nUSN: uuid:63041253-1019-2006-1228-00018efed688::upnp:rootdevice\r\nEXT:\r\nServer: OS 1.0 UPnP/1.0 Realtek/V1.3\r\nLocation:   http://192.168.2.1:52881/simplecfg.xml\r\n\r\n",
  "port": 1900,
  "hostnames": [
  "softbank126100105198.bbtec.net"
],
  "location": {
  "city": "Yamashitacho",
  "region_code": "18",
  "area_code": null,
  "longitude": 130.55,
  "country_code3": "JPN",
  "latitude": 31.58330000000001,
  "postal_code": "892-0816",
  "dma_code": null,
  "country_code": "JP",
  "country_name": "Japan"
},
  "timestamp": "2018-08-11T04:56:17.312039",
  "domains": [
  "bbtec.net"
],
  "org": "Softbank BB",
  "os": null,
  "_shodan": {
  "crawler": "6ff540e4d43ec69d8de2a7b60e1de2d9ddb406dc",
  "options": {},
  "module": "upnp",
  "id": null
 },
  "opts": {},
  "ip_str": **"126.100.105.198"**
}

实际结果:

通过运行上面的代码,我将获得基于单个组合的过滤器参数(这里是ip_str和timestamp)的过滤器输出/ json横幅。      jq'。 |选择(.timestamp =“ 2018-08-11T04:56:17.312039”)| select(.ip_str ==“ 126.100.105.198”)'extract_3month_fromshodan.json> all.json

实际问题:

但是问题是我必须为IP手动运行以上代码290次,这有些麻烦。因此,如何使用此命令,使其可以自动重复执行其他290次。

1 个答案:

答案 0 :(得分:0)

您似乎缺少的难题是能够使用诸如--arg之类的命令行选项将值传递到jq中。因此,以下内容应使您摆脱困境:

while read -r ts ip
do
  jq --arg ts "$ts" --arg ip "$ip" '
    select(.timestamp==$ts and .ip_str==$ip)
  ' extract_3month_fromshodan.json
done < <(cat<<EOF     
2018-08-11T04:56:17.312039 126.100.105.198
EOF
)

inputfile.txt

因此,如果inputfile.txt包含上述ts-ip对,则可以这样写:

while read -r ts ip
do
  jq --arg ts "$ts" --arg ip "$ip" '
    select(.timestamp==$ts and .ip_str==$ip)
  ' extract_3month_fromshodan.json
done < inputfile.txt