首页 > 解决方案 > debug1:退出状态 0,当尝试 ssh 到远程服务器时

问题描述

我正在尝试 scp 一些文件,然后 ssh 进入远程服务器。从本地主机工作正常。但是,当我通过 Jenkins 中的 shell 脚本尝试相同的操作时,ssh 会话会卡住或关闭。我尝试通过散列 sab 命令单独运行 scp 命令,并且效果很好。我在控制台输出结束时收到 debug1: Exit status 0。

我试图运行的命令是:

sshpass -p 0pen5ecret scp -v -o StrictHostKeyChecking=no 
/home/jenkins/test_folder/${env}_test.properties 
servername@${server}:/home/

接着,

sshpass -p password ssh -tt -o StrictHostKeyChecking=no 
servername@${server} 'cd /home ; rm -f cachefolder ; sh test.sh'

以下是我在控制台输出中收到的错误消息:

debug1: Host '172.21.83.215' is known and matches the RSA host key.
debug1: Found key in /var/lib/jenkins/.ssh/known_hosts:9
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password,keyboard- 
interactive
debug1: Next authentication method: publickey
debug1: Trying private key: /var/lib/jenkins/.ssh/id_rsa
debug1: Trying private key: /var/lib/jenkins/.ssh/id_dsa
debug1: Trying private key: /var/lib/jenkins/.ssh/id_ecdsa
debug1: Trying private key: /var/lib/jenkins/.ssh/id_ed25519
debug1: Next authentication method: keyboard-interactive
debug1: Authentication succeeded (keyboard-interactive).
Authenticated to 172.21.83.244 ([172.21.83.210]:22).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: pledge: network
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
debug1: Sending command: scp -v -t /data01/DS_Release
Sending file modes: C0777 123 TS2_DeploymentScore.properties
Sink: C0777 123 TS2_DeploymentScore.properties
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: channel 0: free: client-session, nchannels 1
debug1: fd 0 clearing O_NONBLOCK
debug1: fd 1 clearing O_NONBLOCK
debug1: fd 2 clearing O_NONBLOCK
Transferred: sent 3320, received 4596 bytes, in 0.1 seconds
Bytes per second: sent 54147.4, received 74958.3
debug1: Exit status 0

如何解决?提前致谢。顺便说一句,这是我在这里的第一个问题,所以很好。

标签: jenkinssshcentos

解决方案


要通过服务器远程 SCP 文件,您可以使用以下两个选项之一:

  1. 如果您使用的是 Jenkins 文件,您可以使用 Jenkins 的 stash 和 unstash 功能:

    • 在管道中创建两个阶段,在源服务器上运行第一个阶段并将文件存储在其中。
    • 在目标服务器上运行第二阶段并在该服务器中取消存储文件。(需要在服务器上安装 Jenkins 代理)
  2. 如果您使用的是自由式作业,您可以使用期望脚本远程复制文件,您可以使用以下语法:

    #!/usr/bin/expect -f
    
    #Usage sshsudologin.expect <host> <ssh user> <ssh password> <foldername>
    
    set timeout 20
    
    spawn scp  -r "/sourcefolder/[lindex $argv 3]" [lindex $argv 1]@[lindex $argv 0]:"/export/home/[lindex $argv 1]/"
    
    expect "yes/no" {
    send "yes\r"
    expect "*?assword" { send "[lindex $argv 2]\r" }
    } "*?assword" { send "[lindex $argv 2]\r" }
    
    
    expect eof
    

推荐阅读