首页 文章

Jenkins SSH shell在执行远程命令之前关闭

提问于
浏览
4

我有一个Jenkins作业,在“执行shell”下有以下命令:

ssh jenkins@172.31.12.58
pwd

我希望Jenkins服务器通过SSH连接到远程服务器,然后在远程服务器上运行命令 .

相反,Jenkins连接到远程服务器,立即断开连接,然后在本地运行 pwd 命令,如输出中所示:

Started by user Johanan Lieberman
Building in workspace /var/lib/jenkins/jobs/Test Github build/workspace
[workspace] $ /bin/sh -xe /tmp/hudson266272646442487328.sh
+ ssh jenkins@172.31.12.58
Pseudo-terminal will not be allocated because stdin is not a terminal.
+ pwd
/var/lib/jenkins/jobs/Test Github build/workspace
Finished: SUCCESS

Edit :知道为什么 ssh 命令之后的后续命令不在SSH shell中运行,而是在本地运行?

2 回答

  • 6

    如果您没有以交互方式运行,SSH不会创建交互式会话(因此您看到的“伪终端”错误消息),因此它与在交互式终端中执行一系列命令并不完全相同 .

    要通过SSH会话运行特定命令,请使用:

    ssh jenkins@YOUR_IP 'uname -a'
    

    必须正确引用remote命令作为ssh命令的单个参数 . 或者使用bash here-doc syntax作为简单的多行脚本:

    ssh jenkins@YOUR_IP <<EOF
    pwd
    uname -a
    EOF
    
  • 1

    我想你可以使用Publish Over SSH plugin用SSH在slave上执行命令:

    enter image description here

    如果源文件字段是必需的,则可以传输虚拟文件 .

    更新:另一种解决方案是使用SSH plugin . 也许这是一个比其他插件更好的解决方案:)

相关问题