Jenkins SSH 从 Windows 服务器到 Linux 服务器

Jenkins SSH From Windows Server To Linux Server

如何建立从 Jenkins Build Server(Windows) 到 Linux 机器的连接并在 Linux 机器上执行 shell 脚本。请让我知道如何实现。

1) 使用 SSH 无密码登录:一种方法是先安装 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 .

粘贴上面的代码段 link 以供参考。

stage('SCP JAR file') {
    steps {
        bat '"c:\Program Files\git\usr\bin\ssh.exe" -i "c:\Users\tom\.ssh\azure\id_rsa" tom@xy.xyz.xy.xz ls -ltr'
    }
}

2) 使用密码身份验证:使用 SSH 是首选做法,但是,出于任何原因,如果这不可行,您可以使用 密码 身份验证进行连接。请按照以下步骤操作:

a) 在 Linux 实例上的 /etc/ssh/sshd_config 文件中将 PasswordAuthentication no 更改为 PasswordAuthentication yes。重新启动 sshd 服务。为您要连接的用户设置密码。使用 passwd 命令。下面提到的所有步骤 link.

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

b) 您可以使用 PuTTY 通过密码进行连接。参考这个link:https://unix.stackexchange.com/questions/116672/running-linux-script-on-remote-linux-system-using-putty

以上 link 解释了如何使用文件(包含命令)选项,它还指定了如何使用 PuTTY Remote command 运行 单个命令SSH 部分中的框。在图中。如下所示,如果我使用用户 ubuntu 连接,它将在 /home/ubuntu 下创建一个目录 abc,之后 PuTTY 将立即退出。

使用命令文件选项:putty.exe -ssh user@192.168.1.1 -pw password -m C:\local\file\containing_command


3) 使用 plink:您可以从 here

下载可执行文件

如果您希望 运行 多个命令,请创建一个包含所有命令的文件。例如,

cmds.txt的内容:

hostname
touch file
ls -ltr

命令:

c:\test>plink -ssh centos@xx.xyz.xxy.xyz -pw centos -m cmds.txt

输出:

注意:如果您注意到第一个命令 运行s 而第二个没有,请尝试更改命令文件的格式:

first_cmd ; second_cmd

first_cmd
second_cmd

此外,不要忘记在最后一个命令后按 Enter。您的文件应如下所示: