【问题标题】:Sending jq with argjson via ssh通过 ssh 使用 argjson 发送 jq
【发布时间】:2019-10-17 08:27:54
【问题描述】:

我正在尝试通过 ssh 为这个 JSON 运行 jq 命令:

{
  "nodes": {
    "app": {
      "nodes": 1,
      "is_manager": true,
      "ip": [
        "0.0.0.0"
      ],
      "cpus": 16,
      "memory": 64
    },
    "data": {
      "nodes": 1,
      "ip": [
        "0.0.0.0"
      ],
      "cpus": 16,
      "memory": 64
    },
    "analysis": {
      "nodes": 1,
      "ip": [
        "0.0.0.0"
      ],
      "cpus": 16,
      "memory": 64
    },
    "elastic_kafka_1": {
      "nodes": 1,
      "ip": [
        "0.0.0.0"
      ],
      "cpus": 16,
      "memory": 64
    },
    "elastic_kafka_2": {
      "nodes": 1,
      "ip": [
        "0.0.0.0"
      ],
      "cpus": 16,
      "memory": 64
    },
    "elastic_kafka_3": {
      "nodes": 1,
      "ip": [
        "0.0.0.0"
      ],
      "cpus": 16,
      "memory": 64
    },
    "master": {
      "nodes": 1,
      "ip": [
        "0.0.0.0"
      ],
      "cpus": 16,
      "memory": 64
    }
  }
}

这就是我要运行的:

ssh -o StrictHostKeyChecking=no -i key.pem user@"172.13.1.23"
"jq -Rn --argjson original_doc \"\$(<nodes.json)\" '
  input | split(\"\u0000\") as \$ips
  | \$original_doc
  | .nodes.app.ip = \$ips[0]
  | .nodes.data.ip = \$ips[1]
  | .nodes.analysis.ip = \$ips[2]
  | .nodes.elastic_kafka_1.ip = \$ips[3]
  | .nodes.elastic_kafka_2.ip = \$ips[4]
  | .nodes.elastic_kafka_3.ip = \$ips[5]
  | .nodes.master.ip = \$ips[6]
' < <(printf '%s\0' \"\${GCP_INSTANCES[@]}\") > test.json && mv test.json nodes.json"

这是一个输出:

{
  "nodes": {
    "app": {
      "nodes": 1,
      "is_manager": true,
      "ip": "",
      "cpus": 16,
      "memory": 64
    },
    "data": {
      "nodes": 1,
      "ip": "",
      "cpus": 16,
      "memory": 64
    },
    "analysis": {
      "nodes": 1,
      "ip": null,
      "cpus": 16,
      "memory": 64
    },
    "elastic_kafka_1": {
      "nodes": 1,
      "ip": null,
      "cpus": 16,
      "memory": 64
    },
    "elastic_kafka_2": {
      "nodes": 1,
      "ip": null,
      "cpus": 16,
      "memory": 64
    },
    "elastic_kafka_3": {
      "nodes": 1,
      "ip": null,
      "cpus": 16,
      "memory": 64
    },
    "master": {
      "nodes": 1,
      "ip": null,
      "cpus": 16,
      "memory": 64
    }
  }
}

如您所见,由于 ssh 的一些语法问题或其他原因,jq 无法正常工作。

我在本地测试了这个命令,没有 ssh,它可以正常工作。

我认为问题出在 printf '%s\0' 上,但不知道我做错了什么。

【问题讨论】:

  • 一般来说,不要试图引用shell命令来远程运行——相反,让shell自己来做。
  • @CharlesDuffy 你能举个例子吗?
  • nodes.json 应该在本地机器还是远程机器上?
  • @CharlesDuffy nodes.json 在远程机器上
  • 顺便说一句,我认为这个问题作为一个整体是 stackoverflow.com/questions/47558482/… 的重复——在这种情况下是 awk 而在这种情况下是 jq,但根本问题是相同的。

标签: bash ssh jq


【解决方案1】:

确保正确完成所有引用的最简单方法是让 shell 为您完成,通过使用 declare -f 生成已在本地定义的函数的文本表示,并使用 declare -p 生成函数需要访问的任何局部变量的文本表示。因此:

doRemoteWork() {
  jq -Rn --argjson original_doc "$(<nodes.json)" '
    input | split("\u0000") as $ips
    | $original_doc
    | .nodes.app.ip = $ips[0]
    | .nodes.data.ip = $ips[1]
    | .nodes.analysis.ip = $ips[2]
    | .nodes.elastic_kafka_1.ip = $ips[3]
    | .nodes.elastic_kafka_2.ip = $ips[4]
    | .nodes.elastic_kafka_3.ip = $ips[5]
    | .nodes.master.ip = $ips[6]
  ' < <(printf '%s\0' "${GCP_INSTANCES[@]}") >"nodes.json.$$" \
  && mv "nodes.json.$$" nodes.json
}

ssh -o StrictHostKeyChecking=no -i key.pem user@172.13.1.23 \
  "$(declare -p GCP_INSTANCES; declare -f doRemoteWork); doRemoteWork"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-16
    • 2016-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多