SSH Connection Succeeds, But Script Execution Fails to Recognize SSH Key on VM #894
-
I’m using Tart with Jenkins to establish an SSH connection from a Jenkins agent to a VM running on that agent. The SSH connection itself is successful, but when I try to execute a script on the VM, it seems that the script does not recognize or use the SSH key that was passed. Steps to Reproduce:
Here’s the relevant portion of my Jenkins pipeline script: withCredentials([sshUserPrivateKey(credentialsId: 'xyz123', keyFileVariable: 'PK')]) {
sh """
printf "%s" "$PK" > /tmp/temp_ssh_key
chmod 600 /tmp/temp_ssh_key
sshpass -p $VM_PW ssh -i /tmp/temp_ssh_key -o 'StrictHostKeyChecking=no' $VM_USER@$(tart ip $IMAGE_NAME) 'bash -s' < '${env.WORKSPACE}/myScript.sh'
rm -f /tmp/temp_ssh_key
"""
} Expected Behavior:The script executed on the VM should be able to use the SSH key to perform operations like cloning a Git repository or accessing other SSH-protected resources. Actual Behavior:The script on the VM seems to fail when it requires SSH access, indicating that it doesn’t recognize or cannot use the SSH key passed from the Jenkins agent. Additional Information:
Is there something specific about how Tart handles SSH keys or sessions that might cause this issue? Could it be related to the environment in which the script is executed on the VM? Any guidance or insights would be greatly appreciated! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
This is unlikely, because the Tart is pretty net-neutral unless Regarding the pipeline script you've posted, I think it needs to either (1) copy of the private key into the VM or (2) forward the agent connection to the VM to achieve what you want. The latter can be accomplished by first starting a new instance of the SSH agent, adding a private key to it, and then forwarding the agent connection when connecting to the VM using the withCredentials([sshUserPrivateKey(credentialsId: 'xyz123', keyFileVariable: 'PK')]) {
sh """
eval \$(ssh-agent -s)
ssh-add ${PK}
sshpass -p $VM_PW ssh -A -o 'StrictHostKeyChecking=no' $VM_USER@$(tart ip $IMAGE_NAME) 'bash -s' < '${env.WORKSPACE}/myScript.sh'
ssh-agent -k
"""
} Note that you can use the
|
Beta Was this translation helpful? Give feedback.
This is unlikely, because the Tart is pretty net-neutral unless
--net-softnet
is used, and we don't do anything special to the VM environments, you can check thebase.pkr.hcl
to see that it's mostly about installing Homebrew and assorted software.Regarding the pipeline script you've posted, I think it needs to either (1) copy of the private key into the VM or (2) forward the agent connection to the VM to achieve what you want.
The latter can be accomplished by first starting a new instance of the SSH agent…