Connect Jenkins for SSH

Good afternoon.
I am testing in a freestyle job, connecting to a Linux machine which I manage, I have problems with the connection.
I have tried everything in my script, could you please tell me what is the correct way to connect against Linux and Windows from Jenkins?

The main idea, which I am first testing in development, is to copy .war files from a previous build of another pipeline to the Linux machine, previously stopping Tomcat, deleting the old files, and starting Tomcat again.

Thanks !!!

script

#!/bin/bash

# Variables
BUILD_NUMBER="8"  # Reemplazar con el nĂşmero de versiĂłn de la Pipeline 1
ZIP_DIR="/opt/20240111/"
REMOTE_SERVER="xxxxxxx"
REMOTE_USER="xxxxxxxx"
REMOTE_PASS="xxxxxxxx"
REMOTE_TOMCAT_DIR="/opt/tomcat9"
WEBAPPS_DIR="$REMOTE_TOMCAT_DIR/webapps"
APP_NAME="xxxxxx"

# Copy ZIP files to remote server
scp "$ZIP_DIR/WAR1-$BUILD_NUMBER*.zip" "$ZIP_DIR/WAR2-$BUILD_NUMBER*.zip" "$REMOTE_USER@$REMOTE_SERVER:/opt/tomcat9/webapps"

# Wait for the copy to finish before continuing
wait

# Connect to remote server and run commands
sshpass -p "$REMOTE_PASS" ssh -o StrictHostKeyChecking=no -t -t "$REMOTE_USER@$REMOTE_SERVER" << EOF

# Stop Tomcat
$REMOTE_TOMCAT_DIR/bin/shutdown.sh

# Delete old WARs and associated directories
find "$WEBAPPS_DIR" -maxdepth 1 -type d -name "$APP_NAME*" ! -name "ROOT" ! -name "host-manager" ! -name "manager" -exec rm -rf {} \;

# Unzip new ZIP files
unzip -q "$WEBAPPS_DIR/WAR1-$BUILD_NUMBER*.zip" -d "$WEBAPPS_DIR/"
unzip -q "$WEBAPPS_DIR/WAR2-$BUILD_NUMBER*.zip" -d "$WEBAPPS_DIR/"

# Start Tomcat
$REMOTE_TOMCAT_DIR/bin/startup.sh

EOF

I see you are trying to copy out to $REMOTE_USER:$REMOTE_SERVER; but the password isn’t passed thru, which would suggest to me that you are going to get the Perm Denied. Reference to .ssh/id_rsa would suggest that the box is trying to run the script unattended.

Can you confirm if the .zip files are copied to the destination? If you were to run the commands at Command Line, the user should be prompted for the password, but that doesn’t happen in the Jenkins Scripts. You can look up how to set up unattended access: .ssh/id_rsa as your research point.

1 Like

Thanks for your answer.
No, it does not copy anything or do any action on the tomcat, authentication fails.
I have been having this problem for a long time, I don’t know what is the correct way to log in from Jenkins against a Linux…
Thanks for the help.

scp is basically copying files over ssh. So you will need to provide either an ssh key or username/password to the command. Either way you should provide this via credentials in Jenkins and not store these things directly in the script
Probably using the ssh-agent-plugin is the best approach here

1 Like

Thank you very much Markus.
I’ll try it and let you know… yes, I think that’s where the error is.
Greetings.

Hi Markus.
Indeed, I have added the ssh-agent plugin and add the credentials from there.
I have also modified my script and removed the credentials part since it now does it from the plugin.
I am encountering the following error.

Script modified.

#!/bin/bash

# Variables
BUILD_NUMBER="8"  # Reemplazar con el nĂşmero de versiĂłn de la Pipeline 1
ZIP_DIR="/opt/20240111/"
REMOTE_SERVER="192.168.68.61"
REMOTE_TOMCAT_DIR="/opt/tomcat9"
WEBAPPS_DIR="$REMOTE_TOMCAT_DIR/webapps"
APP_NAME="xxxxx"

# Stop Tomcat
$REMOTE_TOMCAT_DIR/bin/shutdown.sh

# Delete old WARs and associated directories
find "$WEBAPPS_DIR" -maxdepth 1 -type d -name "$APP_NAME*" ! -name "ROOT" ! -name "host-manager" ! -name "manager" -exec rm -rf {} \;

# Copy ZIP files to remote server
scp "$ZIP_DIR/WAR1-$BUILD_NUMBER*.zip" "$ZIP_DIR/WAR2-$BUILD_NUMBER*.zip" "$REMOTE_USER@$REMOTE_SERVER:/opt/tomcat9/webapps"

# Unzip new ZIP files
unzip -q "$WEBAPPS_DIR/WAR1-$BUILD_NUMBER*.zip" -d "$WEBAPPS_DIR/"
unzip -q "$WEBAPPS_DIR/WAR2-$BUILD_NUMBER*.zip" -d "$WEBAPPS_DIR/"

# Start Tomcat
$REMOTE_TOMCAT_DIR/bin/startup.sh

Thanks !!!

the ssh-agent-plugin will not open ssh connections for you. It will just allow you to inject passwords and ssh keys to an ssh based command.
Afaik $REMOTE_TOMCAT_DIR/bin/shutdown.sh should run on the remote machine so this can’t be before the scp command and needs to stay in the sshpass command as it was before.

1 Like

Thanks Markus.
with the script as at the beginning plus the credentials in ssh-agent plugin it gives me this error…
(The credentials are correct, I also attach a screenshot of the log of the machine I am trying to connect to, I don’t understand anything…)

imagen

Seems the credentials are not correct

1 Like

Once again thanks for the response Markus.
If they are correct, I log in directly to the target machine via SSH with those credentials… hence my confusion with this issue.
Thank you.