You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jenkins控制台使用Groovy执行远程Shell命令的POC实现咨询

POC: Run Remote Shell Commands via Groovy in Jenkins Console

Prerequisites

  • Install the SSH Pipeline Steps plugin (head to Jenkins > Manage Jenkins > Plugins to search and install it if missing)
  • Your remote server must allow SSH access, and the Jenkins server needs network reachability to its SSH port (default 22)
  • Have valid authentication credentials (password or SSH private key) for the remote server

Full Groovy POC Script

// Configure your remote server details
def remoteServer = [
    name: "MyTestServer",
    host: "192.168.0.50", // Replace with your server's IP/domain
    port: 22,
    username: "ubuntu", // Replace with your remote username
    // Use either password OR privateKey (uncomment the one you need)
    password: "your-secure-password", 
    // privateKey: readFile("/var/lib/jenkins/.ssh/id_rsa") // For SSH key authentication
]

// Helper function to execute commands remotely
def executeRemoteCommand(serverConfig, command) {
    // sshCommand is a method provided by the SSH Pipeline Steps plugin
    sshCommand(
        remote: serverConfig,
        command: command,
        failOnError: true // Trigger exception if command exits with non-zero code
    )
    println "✅ Command completed: ${command}"
}

// Test the setup with common diagnostic commands
try {
    println "=== Checking Remote Server Uptime ==="
    executeRemoteCommand(remoteServer, "uptime")

    println "\n=== Listing Home Directory Contents ==="
    executeRemoteCommand(remoteServer, "ls -la ~")

    println "\n=== Checking Disk Usage ==="
    executeRemoteCommand(remoteServer, "df -h")
} catch (Exception ex) {
    println "❌ Failed to execute command: ${ex.message}"
    throw ex // Preserve error status in Jenkins console for visibility
}

Key Details & Best Practices

  • Authentication: If using SSH keys, ensure the Jenkins system user (usually jenkins) has read permissions to the private key file. Never hardcode credentials in scripts!
  • Secure Credential Storage: For production use, leverage Jenkins' built-in Credentials Manager. Here's how to adjust the script to use stored credentials:
    // Fetch stored credentials (replace 'remote-server-creds' with your credential ID)
    def remoteCreds = credentials('remote-server-creds')
    def remoteServer = [
        name: "MyTestServer",
        host: "192.168.0.50",
        username: remoteCreds.username,
        password: remoteCreds.password
    ]
    
  • Error Handling: The failOnError: true flag ensures that any failed remote command triggers an exception, which we catch and log clearly for debugging.
  • Custom SSH Ports: If your remote server uses a non-standard SSH port, simply update the port value in the remoteServer configuration block.

Troubleshooting Tips

  • If connection fails, verify firewall rules allow outbound traffic from Jenkins to the remote server's SSH port
  • Test SSH connectivity manually from the Jenkins server terminal using the same credentials to rule out network/auth issues
  • Ensure the remote user has sufficient permissions to run the commands you're executing

内容的提问来源于stack exchange,提问作者user1880732

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:46:32