Windows agent via SSH not able to run SSH commands

The Windows agent launched via SSH and on the agent we are having trouble executing SSH commands in a Jenkins pipeline.

Jenkins version: 2.452.1

Request you to help us to move forward.

Could you share how you are invoking the ssh in the pipeline or if you are using any plugins.

Hi Markbosire,

Thanks for the reply. Here are the details.

Jenkins version: 2.462.3

SSH plugin version: 2.6.1

pipeline {
agent {
label “template-2019”
}
stages {
stage(‘Setup’) {
steps {
dir (‘test’) {
deleteDir()
}
}
}
stage(‘Build’) {
steps {
sleep 3
bat(“set”)
}
}
stage(‘Test’) {
steps {
echo ‘Testing..’
sleep 3
bat ‘’‘set TERM=xterm
C:\Windows\System32\OpenSSH\ssh.exe -vvv -F /Users/jenkins/.ssh/config -i /Users/jenkins/.ssh/id_rsa -t -o StrictHostKeyChecking=no XXXX@XXXXXXXXXX ls -ltra >bla 2>&1’‘’ }
}
stage(‘Deploy’) {
steps {
echo ‘Deploying…’
}
}
}
}

Please find the attached output of ssh & pipeline

(Attachment pipeline.output.txt is missing)

(Attachment ssh.out.txt is missing)

Jenkins.zip (13.3 KB)
Please find the attachment.

The pipeline seems to be running in a mixed Windows/Unix environment. Your bat command is trying to use /Users/jenkins/.ssh/config which appears to be a Unix-style path, but you’re running on a Windows agent.

The agent is running on template-MG24 in the output but your pipeline specifies template-2019. This mismatch might indicate configuration issues.
Windows paths should use backslashes or escaped backslashes. For SSH config and key files, try:

bat ‘’‘set TERM=xterm
C:\Windows\System32\OpenSSH\ssh.exe -vvv -F C:\Users\jenkins\.ssh\config -i C:\Users\jenkins\.ssh\id_rsa -t -o StrictHostKeyChecking=no XXXX@XXXXXXXXXX ls -ltra >bla 2>&1’‘’

Also Consider using the Jenkins SSH Steps plugin for cleaner syntax if available.

Also before executing complex commands, try a simple connection test:

bat “C:\Windows\System32\OpenSSH\ssh.exe -o StrictHostKeyChecking=no XXXX@XXXXXXXXXX echo Connection Successful”

Also check target server access: Ensure the target server allows SSH connections from your Jenkins agent and the user has appropriate permissions.

1 Like

Hi Markbosire,

Since I was on vacation, I was not able to test and reply immediate.

Pipeline modified as below:

pipeline {

agent {

label “window-ssh-test-node”

}

stages {

stage(‘Run SSH Command on Windows’) {

steps {

bat ‘’’

C:\Windows\System32\OpenSSH\ssh.exe -vvv -F \users\jenkins\.ssh\config -I \Users\jenkins\.ssh\id_rsa -o StrictHostKeyChecking=no XXXXXXXXX@XXXXXXXXXXXXXXXXXX echo “Connection Successful” exit

‘’’

}

}

}

}

Jenkins log:

Started by user Venkat

[Pipeline] Start of Pipeline

[Pipeline] node

Running on “window-ssh-test-node” in /Users/jenkins/workspace/workspace/Ec2-Fleet/Ec2-Fleet-SSH

[Pipeline] {

[Pipeline] stage

[Pipeline] { (Run SSH Command on Windows)

[Pipeline] bat

OpenSSH_for_Windows_8.6p1, LibreSSL 2.6.5

workspace\workspace\Ec2-Fleet\Ec2-Fleet-SSH>C:\Windows\System32\OpenSSH\ssh.exe -vvv -F \Users\jenkins.ssh\config -i \Users\jenkins.ssh\id_rsa -o StrictHostKeyChecking=no XXXXXXXXX@XXXXXXXXXXXXXXXXXX echo “Connection Successful”

post triggering the job, it is not completing (finally aborting the job). Not sure it connected to the target server and expecting inputs and hanging there.

Is your agent running on c-drive or another drive?
As you use

bat '''
C:\Windows\System32\OpenSSH\ssh.exe -vvv -F \users\jenkins\.ssh\config -I \Users\jenkins\.ssh\id_rsa -o StrictHostKeyChecking=no XXXXXXXXX@XXXXXXXXXXXXXXXXXX echo “Connection Successful” exit
''' 

You specified the path to the config and key without a drive, now if the agent runs on drive D: then -F \users\jenkins\.ssh\config is not found.

PS: It will really help if you format code and logout, you can use markdown here

Use Case:

We are currently working on an EC2-Fleet Windows node that requires SSH access for two purposes:

  1. Cloning repositories from Bitbucket using SSH.
  2. Connecting to another remote server via SSH.

Below script is working fine to connect to another remote server via ssh.

pipeline {
agent {
label “window-ssh-test-node”
}
stages {
stage(‘Prepare SSH Script and Connect’) {
steps {
withCredentials([sshUserPrivateKey(
credentialsId: ‘ec2_node_test_hosting’,
usernameVariable: ‘SSH_USER’,
keyFileVariable: ‘SSH_KEYFILE’
)]) {
script {
writeFile file: ‘ssh_connection.sh’, text: ‘’‘#!/bin/bash
ssh -i “$1” -o IdentitiesOnly=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null “$2@$3” “hostname && uptime && ls -l”
‘’’
}
bat ‘’’
set SSH_REMOTE_HOST=XXXXXXX.XXXXXXXXXXXXg.com
call “C:\Program Files\Git\bin\bash.exe” -c “chmod +x ./ssh_connection.sh && ./ssh_connection.sh "%SSH_KEYFILE%" "%SSH_USER%" "%SSH_REMOTE_HOST%"”
‘’’
}
}
}
}
}

We have issues to clone from bitbucket repository

pipeline {
agent {
label “window-ssh-test-node”
}

environment {
    GIT_SSH_COMMAND = 'ssh -i C:\\Users\\Administrator\\.ssh\\bitbucket_key -o StrictHostKeyChecking=no'
}

stages {
    stage('Checkout Repo') {
        steps {
            // Copy SSH private key from Jenkins credentials
            withCredentials([sshUserPrivateKey(credentialsId: XXXXXXXXXXXXXXXXXX', keyFileVariable: 'KEYFILE')]) {
                bat """
                    mkdir %USERPROFILE%\\.ssh
                    copy %KEYFILE% %USERPROFILE%\\.ssh\\bitbucket_key
                    git config --global core.sshCommand "ssh -i %USERPROFILE%\\.ssh\\bitbucket_key -o StrictHostKeyChecking=no"
                    git clone ssh://git@XXXXXXXXXXXXXXXX.com:7999/pts/webhook.git
                """
            }
        }
    }
}

}

Can you pls help us to clone from Bitbucket.

HI,

When trying to clone getting “‘SSH-2.0-OpenSSH_9.1’ is not recognized as an internal or external command, operable program or batch file.”

Despite this, Jenkins seems unable to recognize or execute the SSH command properly. I’ve verified environment variables, and OpenSSH is accessible from the command line. The agent setup is failing when trying to establish an SSH connection.

Any help or guidance on resolving this issue would be greatly appreciated.

Use the checkout step for cloning from bitbucket instead of trying to do that in your own manually

We are planning to use Jenkins EC2-Fleet with Windows nodes and would like to perform Bitbucket clone operations via SSH within the pipeline. Please let us know if there are any specific configurations or prerequisites required for this setup.