【发布时间】:2019-03-23 15:35:28
【问题描述】:
我想使用 shell 脚本将格式化(即缩进)JSON 配置文件写入远程计算机。
我的代码:
json_config = {
"api-listen": true,
"api-network": true,
"multi-version": "1",
"pools": [
{
"pass": "123",
"url": "antpool.com:3333"
},
{
"pass": "123",
"url": "antpool.com:443"
},
{
"pass": "123",
"antpool.com:25"
}
]
}
# format the new configuration
json_config_formatted = json.dumps(json.dumps(json_config), indent=4)
# write the new config
connection.sendShell('echo "{}" | cat > "/config/bmminer.conf"'.format(json_config_formatted))
但是,所有内容都写在一行上。如何保留字符串的格式?
【问题讨论】:
-
connection的实例是什么?有没有办法直接为cat设置标准输入? -
如果您使用的是
subprocess.Popen,例如,您可能会使用p = Popen(["ssh", remote_host, "cat > /config/bmminer.conf"], stdin=PIPE),后跟json.dump(json_config, p.stdin)。cat从ssh继承其标准输入。