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

能否在Katalon中执行终端命令?Mac环境下SSH服务器操作测试需求

Can Katalon Execute Terminal Commands on macOS?

Absolutely! Since Katalon Studio is built on Groovy (which runs on Java), you can leverage Java/Groovy's native process handling utilities to run terminal commands on macOS. Below’s a practical, step-by-step guide to implement your specific workflow:

Step 1: Create a New Test Case

  • Open Katalon Studio, go to your project, right-click Test Cases > New > Test Case.
  • Name it something descriptive like RunServerSetupCommands and switch to the script view.

Step 2: Write the Groovy Script

Here’s a script that handles your command sequence. Note that commands like sudo require special handling for privileges—see the notes below for security best practices:

import java.io.BufferedReader
import java.io.InputStreamReader

// Helper function to run a terminal command and capture output/errors
def runTerminalCommand(String command) {
    // Start a bash process to run the command
    def process = new ProcessBuilder("/bin/bash", "-c", command).start()
    def output = new StringBuilder()
    def errorOutput = new StringBuilder()

    // Read standard output
    BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(process.inputStream))
    String line
    while ((line = stdoutReader.readLine()) != null) {
        output.append(line).append("\n")
    }

    // Read error output (critical for debugging failed commands)
    BufferedReader stderrReader = new BufferedReader(new InputStreamReader(process.errorStream))
    while ((line = stderrReader.readLine()) != null) {
        errorOutput.append(line).append("\n")
    }

    // Wait for the command to finish and get exit code
    process.waitFor()
    def exitCode = process.exitValue()

    // Print results to Katalon's console for verification
    println "=== Command Output ==="
    println output.toString()
    if (errorOutput.length() > 0) {
        println "=== Command Errors ==="
        println errorOutput.toString()
    }
    println "Exit Code: ${exitCode}"

    return [output: output.toString(), error: errorOutput.toString(), exitCode: exitCode]
}

// Execute your command sequence
// 1. Navigate to the PEM directory and run SSH (note: cd must be chained with SSH since each process is a new shell)
def sshCommand = "cd /documents/pem/ && ssh -i key.pem your-username@your-server-ip"
runTerminalCommand(sshCommand)

// 2. Run sudo su and install packages (chain commands to run in the same SSH session)
// Option 1: Pass commands directly via SSH (recommended for single-session execution)
def installCommand = "ssh -i /documents/pem/key.pem your-username@your-server-ip 'echo \"your-sudo-password\" | sudo -S yum install -y php7 mysql'"
runTerminalCommand(installCommand)

// Option 2: If you need to run multiple commands in sequence on the server, use a here-document
// def multiCommand = "ssh -i /documents/pem/key.pem your-username@your-server-ip << 'EOF'
// sudo su -
// yum install -y php7
// yum install -y mysql
// EOF"
// runTerminalCommand(multiCommand)

Critical Notes for Success:

  • Security Alert: Hardcoding passwords in scripts is a major security risk. For production, set up passwordless SSH access with sudo privileges granted for specific install commands, or use Katalon’s built-in credential management to retrieve passwords at runtime.
  • Shell Context: Each ProcessBuilder call starts a new shell, so commands like cd won’t persist across calls—always chain directory changes with the command that needs that context (e.g., cd /docs && ssh ...).
  • PEM File Permissions: Ensure your key.pem has strict permissions (chmod 600 /documents/pem/key.pem)—SSH will reject keys with overly open permissions.
  • Exit Codes: Check the exit code returned by each command (0 = success, non-zero = failure) to add assertions in your test case (e.g., assert result.exitCode == 0 : "Command failed with error: ${result.error}").

Alternative: Katalon Built-in Keywords

Katalon has limited built-in support for process execution via Runtime.getRuntime().exec(), but ProcessBuilder is far more flexible for handling input/output and error streams, so it’s the better choice for this use case.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 06:34:03