场景:我正在开发一个jenkins步骤,它需要将一个文件传输到机器上(安装一个jboss模块)。我正试图通过ssh交互来实现它。我需要通过ssh连接,切换到授权用户以访问jboss文件夹/文件,然后使用rsync在jboss模块文件夹中传输jar文件。我不能使用同一个用户来使用ssh和jboss。
问题:我可以通过ssh成功地连接,但是当我发送第一个命令(切换用户)时,它会断开连接,然后就不再工作了。表面上是在执行su命令之前断开连接。下一个命令是检查模块文件夹是否存在(如果不存在,则创建它)。
命令序列在函数中执行:
def installModule(HOST, USER, PASSWORD) {
sh set -x && sshpass -p [PASSWORD] ssh -v -tt -o StrictHostKeyChecking=no [USER]@[HOST] echo [PASSWORD] | sudo -S su - jboss && cd [MODULE_FOLDER] && if [[ ! -e [MODULE_VERSION] ]]; then mkdir [MODULE_VERSION]; fi
}控制台输出:
debug1: Authentication succeeded (keyboard-interactive).
Authenticated to [MACHINE_NAME_HERE] ([IP_HERE]:22).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: pledge: network
debug1: client_input_global_request: rtype hostkeys-00@openssh.com want_reply 0
debug1: tty_make_modes: no fd or tio
debug1: Sending environment.
debug1: Sending env LANG = en_GB.UTF-8
debug1: Sending command: echo [PASSWORD_HERE]
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
Connection to [MACHINE_NAME_HERE] closed.
Transferred: sent 2180, received 3356 bytes, in 0.3 seconds
Bytes per second: sent 7006.2, received 10785.6
debug1: Exit status 0
Sorry, try again.
[sudo] password for jenkins: Sorry, try again.
[sudo] password for jenkins:
sudo: no password was provided
sudo: 2 incorrect password attempts如有任何帮助,将不胜感激。)
发布于 2022-10-21 17:05:05
我不得不做类似的事情,在我的例子中,我选择在我的环境中有一个shell脚本文件,其中包含我需要在远程机器上执行的所有命令。
我就是这样做的:
withCredentials([
usernamePassword(credentialsId: "$VM_CREDENTIALS", usernameVariable: 'USER_VM', passwordVariable: 'PWD_VM')
]) {
script {
sh 'sshpass -p $PWD_VM ssh -o StrictHostKeyChecking=no $USER_VM@$IP_VM "bash -s ' + "$VARIABLE_A $VARIABLE_B" + '" < path/to/shell/script.sh'
}
}我使用$VARIABLE_A和$VARIABLE_B向脚本传递一些参数。$path/to/shell_script.sh表示放置在您的Jenkins环境中要在远程计算机上执行的脚本的路径。
我还必须在shell脚本中切换用户,我是这样做的:
# Switch to root user
echo $PWD | sudo -S sleep 1 && sudo -E su记住,不要在$PWD变量硬编码的地方定义,您需要采取安全措施。
https://stackoverflow.com/questions/74156830
复制相似问题