【问题标题】:Pass json value through a script and replace with output通过脚本传递 json 值并替换为输出
【发布时间】:2019-10-11 12:40:21
【问题描述】:

我正在尝试将 IP 范围引入 SolarWinds,并在与我要标记的服务关联的每组 IP 范围上添加一个标签。虽然我觉得必须有更好的方法来做到这一点,因为最终它需要采用 XML 格式。

<AddressGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/IPAddressGroupsSchema.xsd">
  <AddressGroup enabled="true" description="Example">
    <Range from="13.107.6.152" to="13.107.6.153" />
  </AddressGroup>
  <AddressGroup enabled="true" description="Example 2">
    <Range from="150.224.0.0" to="150.224.131.255" />
    <Range from="150.224.140.0" to="150.224.255.255" />
  </AddressGroup>

基本上,我有一个 JSON 文件,我已经提取了所有不需要的信息。我无法提取 CIDR 地址,所以我有一个 Go 脚本,可以将 CIDR 表示法转换为 IP 范围。

{
  "ips": [
    "13.107.6.152/31",
    "13.107.18.10/31",
    "13.107.128.0/22",
    "23.103.160.0/20",
    "40.96.0.0/13",
    "40.104.0.0/15",
    "52.96.0.0/14",
    "131.253.33.215/32",
    "132.245.0.0/16",
    "150.171.32.0/22",
    "191.234.140.0/22",
    "204.79.197.215/32",
    "2603:1006::/40",
    "2603:1016::/40",
    "2603:1026::/40",
    "2603:1026:200::/39",
    "2603:1026:400::/39",
    "2603:1026:600::/44",
    "2603:1026:620::/44",
    "2603:1026:800::/44",
    "2603:1026:820::/45",
    "2603:1036::/39",
    "2603:1036:200::/40",
    "2603:1036:400::/40",
    "2603:1036:600::/40",
    "2603:1036:800::/38",
    "2603:1036:c00::/40",
    "2603:1046::/37",
    "2603:1046:900::/40",
    "2603:1056::/40",
    "2603:1056:400::/40",
    "2603:1056:600::/40",
    "2603:1096::/38",
    "2603:1096:400::/40",
    "2603:1096:600::/40",
    "2603:1096:a00::/39",
    "2603:1096:c00::/40",
    "2603:10a6:200::/40",
    "2603:10a6:400::/40",
    "2603:10a6:600::/40",
    "2603:10a6:800::/40",
    "2603:10d6:200::/40",
    "2620:1ec:4::152/128",
    "2620:1ec:4::153/128",
    "2620:1ec:c::10/128",
    "2620:1ec:c::11/128",
    "2620:1ec:d::10/128",
    "2620:1ec:d::11/128",
    "2620:1ec:8f0::/46",
    "2620:1ec:900::/46",
    "2620:1ec:a92::152/128",
    "2620:1ec:a92::153/128",
    "2a01:111:f400::/48"
  ],
  "serviceArea": "Exchange",
  "serviceAreaDisplayName": "Exchange Online"
}

还有一些,但我想做的是遍历所有 ip,将每个 ip 传递给 cidrls(一个 cidr 转换脚本),然后用输出替换它们,ip 范围.

jq -r '.ips[]' msftServices.json | xargs -n1 cidrls

输出如下所示,13.107.128.0 - 13.107.131.255

【问题讨论】:

    标签: json xml bash


    【解决方案1】:

    不确定这是否是您所追求的,但这会生成所有必要的&lt;Range&gt; 元素:

    jq -r '.ips[]' msftServices.json | xargs -n1 cidrls | while read lower ignore upper; do echo "<Range from=\"$lower\" to=\"$upper\" />"; done
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多