SSH를 통해 argjson과 함께 jq 보내기

SSH를 통해 argjson과 함께 jq 보내기

이 JSON에 대해 SSH를 통해 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'에 있는 것 같은데, 구체적으로 내가 뭘 잘못하고 있는지 알 수 없었습니다.

답변1

모든 인용이 올바르게 수행되었는지 확인하는 가장 쉬운 방법은 을 사용하여 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 [email protected] \
  "$(declare -p GCP_INSTANCES; declare -f doRemoteWork); doRemoteWork"

관련 정보