【问题标题】:Unable to Preserve line breaks and whitespaces while sending Yaml content through Java Client SSH Remote call通过 Java 客户端 SSH 远程调用发送 Yaml 内容时无法保留换行符和空格
【发布时间】:2021-05-25 12:47:12
【问题描述】:

我正在使用 Java - SSH 客户端库并尝试将以下内容发送到远程服务器。

ssh 192.168.8.129 'export KUBECONFIG=/root/.kube/custom/kubeconfig &&
kubectl create -f- << EOF
apiVersion: v1
kind: Secret
metadata:
  name: mysecret
type: Opaque
data:
  password: $(echo -n 's33msi4' | base64 -w0)
  username: $(echo -n 'jane' | base64 -w0)
EOF'

在终端上运行此命令可以正常工作。没有问题。但是,当我在 Java 程序中发送相同的有效负载时,我会丢失 yml 的格式,其中会丢失空格和换行符。

下面是java程序中使用的编码字符串,

String content = "apiVersion: v1\nkind: Secret\nmetadata:\n name: mysecret\ntype: Opaque\ndata:\n password: $(echo -n 's33msi4' | base64 -w0)\n username: $(echo -n 'jane' | base64 -w0)";

但是,从程序运行此命令后,格式完全丢失。 以下是错误。

STD ERR :: bash: warning: here-document at line 0 delimited by end-of-file (wanted `EOF')
error: Unexpected args: [apiVersion: v1 kind: Secret metadata: name: mysecret type: Opaque data: password: czMzbXNpNA== username: amFuZQ== EOF]

我的格式化输入有什么问题?

【问题讨论】:

  • 你误用了双引号。例如:“s33msi4”应该是“s33msi4”等
  • 编辑了原帖。尝试使用单引号。
  • 你需要显示发出命令的Java代码,heredoc似乎有问题。无论如何,我宁愿通过标准输入从客户端发送内容,而不是服务器端的heredoc,这样更不容易出错。

标签: java linux kubernetes ssh yaml


【解决方案1】:

你必须cat你的命令,例如:

$ ssh $server cat << EOF
...command goes here...
EOF

注意:它也可以使用引号。

【讨论】:

  • 从终端,不管有没有单引号,它都能正常工作
  • 谢谢。我编辑了我的答案,以便对社区有用。
猜你喜欢
  • 2018-05-05
  • 2018-06-15
  • 1970-01-01
  • 1970-01-01
  • 2017-02-08
  • 2016-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多