satya
satya

Reputation: 131

Jenkins SSH From Windows Server To Linux Server

How to Establish connection From Jenkins Build Server(Windows) to Linux Machines and execute the shell scripts on the Linux machine. Please let me know how it can be achieved.

Upvotes: 0

Views: 2363

Answers (1)

Technext
Technext

Reputation: 8107

1) Using SSH passwordless login: One way to achieve that is to first install Git Bash on your Windows machine. This will also install several helper utilities including scp and ssh. Once you have those in place, you can then simply follow the instructions explained here.

Pasting snippet from above link for reference.

stage('SCP JAR file') {
    steps {
        bat '"c:\\Program Files\\git\\usr\\bin\\ssh.exe" -i "c:\\Users\\tom\\.ssh\\azure\\id_rsa" [email protected] ls -ltr'
    }
}

2) Using password authentication: Using SSH is the preferred practice however, for any reason, if that's not feasible, you can connect using password authentication. Follow these steps:

a) Change PasswordAuthentication no to PasswordAuthentication yes in /etc/ssh/sshd_config file on your Linux instance. Restart sshd service. Set password for the user with which you want to connect. Use passwd command for it. All steps mentioned in below link.

https://aws.amazon.com/premiumsupport/knowledge-center/ec2-password-login/

b) You may use PuTTY to connect using password. Refer this link: https://unix.stackexchange.com/questions/116672/running-linux-script-on-remote-linux-system-using-putty

Above link explains how you can use a file (containing commands) option and it also specifies how you can run a single command using the PuTTY Remote command box which is present in SSH section. In the fig. shown below, if i connect using a user ubuntu, it will create a directory abc under /home/ubuntu and after that PuTTY will exit immediately.

enter image description here

Using command file option: putty.exe -ssh [email protected] -pw password -m C:\local\file\containing_command


3) Using plink: You can download the executable from here

In case you wish to run multiple commands, create a file containing all your commands. For example,

Content of cmds.txt:

hostname
touch file
ls -ltr

Command:

c:\test>plink -ssh [email protected] -pw centos -m cmds.txt

Output:

enter image description here

Note: In case you're noticing that the first command runs and the second doesn't, try changing the format of your command file from:

first_cmd ; second_cmd

to

first_cmd
second_cmd

Also, don't forget to press Enter after the last command. Your file should look something like this:

enter image description here

Upvotes: 2

Related Questions