【发布时间】: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,但根本问题是相同的。