在 groovy 脚本中从 ant - sshexec 获得格式良好的输出

发布于 2024-11-30 13:28:33 字数 564 浏览 1 评论 0原文

我的问题是,ant 任务的输出在开头总是有一些 [ssh-exec] 信息文本。我可以抑制/禁用它吗?

到目前为止我的代码:

def ant = new AntBuilder()

// .... variable definition ...

ant.sshexec(host: host,
            port: port,
            trust: true,
            username: username,
            password: password,
            command: 'ls')

>>> output:

  [sshexec] Connecting to foomachine.local:22
  [sshexec] cmd : ls
  [sshexec] oldarchive.gz
  [sshexec] newarchive.gz
  [sshexec] empty-db.sh
  [sshexec] testfile.py

我只想获得我执行的 cmd 的原始输出...

一些想法?!

my problem is, that the output from the ant task alwas has some [ssh-exec] infotext at the beginning. can i suppress / disable that?

my code so far:

def ant = new AntBuilder()

// .... variable definition ...

ant.sshexec(host: host,
            port: port,
            trust: true,
            username: username,
            password: password,
            command: 'ls')

>>> output:

  [sshexec] Connecting to foomachine.local:22
  [sshexec] cmd : ls
  [sshexec] oldarchive.gz
  [sshexec] newarchive.gz
  [sshexec] empty-db.sh
  [sshexec] testfile.py

i just want to have the raw output from the cmd i execute...

some ideas?!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

墨小墨 2024-12-07 13:28:33

您可以将原始输出保存在 Ant 属性中:

def ant = new AntBuilder()
ant.sshexec(host: host,
            port: port,
            trust: true,
            username: username,
            password: password,
            command: 'ls',
            outputproperty: 'result')

def result = ant.project.properties.'result'

You can save the raw output inside an Ant property:

def ant = new AntBuilder()
ant.sshexec(host: host,
            port: port,
            trust: true,
            username: username,
            password: password,
            command: 'ls',
            outputproperty: 'result')

def result = ant.project.properties.'result'
抠脚大汉 2024-12-07 13:28:33

问题是outputproperty无法正常工作(它没有设置ant变量)。

我经常使用 antcontrib 中的 trycatch 来测试是否发生错误,而不是读取返回值。

例子 :

<trycatch>
<try>
      <sshexec host="@{host}" failonerror="true" username="${username}" password="${password}" timeout="${ssh.timeout}" command="@{command}" usepty="@{usepty}" trust="true" />
</try>
<catch>
      <echo>Service already stopped!</echo>
</catch>
</trycatch>

the problem is that outputproperty is not working properly (it does not set the ant variable).

I often use trycatch from antcontrib to test if error occurs instead of reading return value.

Example :

<trycatch>
<try>
      <sshexec host="@{host}" failonerror="true" username="${username}" password="${password}" timeout="${ssh.timeout}" command="@{command}" usepty="@{usepty}" trust="true" />
</try>
<catch>
      <echo>Service already stopped!</echo>
</catch>
</trycatch>
绾颜 2024-12-07 13:28:33

我在 gradle 中遇到了同样的问题,从那里我不得不改变访问该属性的方式:
根据 官方 gradle 文档 3.3

println ant.antProp
println ant.properties.antProp
println ant.properties['antProp']

是正确的方法去。

def ant = new AntBuilder()
ant.sshexec(host: host,
            port: port,
            trust: true,
            username: username,
            password: password,
            command: 'ls',
            outputproperty: 'result')

def result = ant.properties.'result'

希望这可以帮助处于相同情况的人。干杯

I tripped over the same issue in gradle and from there I had to change the way to access the property:
According to the official gradle doc 3.3

println ant.antProp
println ant.properties.antProp
println ant.properties['antProp']

is the correct way to go.

def ant = new AntBuilder()
ant.sshexec(host: host,
            port: port,
            trust: true,
            username: username,
            password: password,
            command: 'ls',
            outputproperty: 'result')

def result = ant.properties.'result'

Hope this helps people in the same situation. Cheers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文