net-shell
A powerful script execution and variable extraction framework with SSH remote execution and local execution support, pipeline orchestration, and flexible variable extraction via regex.
Features
- Hybrid Execution: Execute scripts both locally and remotely via SSH
- Variable Extraction: Extract variables from command outputs using regex patterns
- Pipeline Orchestration: Chain multiple steps with variable passing between them
- Real-time Output: Stream command outputs in real-time
- Flexible Configuration: YAML-based configuration with support for multiple servers
- Error Handling: Robust error handling and logging
Installation
Add this to your Cargo.toml
:
[]
= "0.2.0"
Or install the binary:
Quick Start
Basic Configuration
Create a config.yaml
file:
steps:
- name: "get_system_info"
servers:
- host: "192.168.1.100"
username: "user"
password: "password"
script: |
echo "System: $(uname -s)"
echo "Hostname: $(hostname)"
echo "Uptime: $(uptime)"
variables:
- name: "system_info"
pattern: "System: (.+)"
- name: "hostname"
pattern: "Hostname: (.+)"
- name: "uptime"
pattern: "Uptime: (.+)"
- name: "process_info"
servers:
- host: "192.168.1.100"
username: "user"
password: "password"
script: |
echo "Processing system: ${system_info}"
echo "On host: ${hostname}"
ps aux | head -5
Local Execution
For local execution, simply omit the servers
field or leave it empty:
steps:
- name: "local_check"
script: |
echo "Running locally on: $(hostname)"
echo "Current user: $(whoami)"
echo "Working directory: $(pwd)"
variables:
- name: "local_hostname"
pattern: "Running locally on: (.+)"
Mixed Local and Remote Execution
You can mix local and remote steps in the same pipeline:
steps:
- name: "local_prep"
script: |
echo "Preparing locally..."
echo "Timestamp: $(date)"
variables:
- name: "timestamp"
pattern: "Timestamp: (.+)"
- name: "remote_execution"
servers:
- host: "192.168.1.100"
username: "user"
password: "password"
script: |
echo "Executing remotely with timestamp: ${timestamp}"
echo "Remote host: $(hostname)"
Usage
Command Line
Run with default configuration (config.yaml
):
Or specify a custom configuration file:
Programmatic Usage
use ;
async
Configuration Reference
Step Configuration
Each step supports the following fields:
name
: Unique identifier for the stepservers
: List of SSH server configurations (optional for local execution)script
: Shell script to executevariables
: List of variable extraction patterns
Server Configuration
servers:
- host: "192.168.1.100"
username: "user"
password: "password"
# Optional: port (default: 22)
port: 22
# Optional: timeout in seconds (default: 30)
timeout: 30
Variable Extraction
Variables are extracted using regex patterns. Only the first capture group is used:
variables:
- name: "extracted_value"
pattern: "Value: (.+)"
Examples
Complex Variable Extraction
steps:
- name: "extract_multiple"
script: |
echo "CPU: $(nproc) cores"
echo "Memory: $(free -h | grep Mem | awk '{print $2}')"
echo "Disk: $(df -h / | tail -1 | awk '{print $4}') available"
variables:
- name: "cpu_cores"
pattern: "CPU: (.+) cores"
- name: "memory_total"
pattern: "Memory: (.+)"
- name: "disk_available"
pattern: "Disk: (.+) available"
Pipeline with Variable Passing
steps:
- name: "step1"
script: |
echo "Step 1 completed"
echo "Result: success"
variables:
- name: "step1_result"
pattern: "Result: (.+)"
- name: "step2"
script: |
echo "Step 1 result was: ${step1_result}"
if [ "${step1_result}" = "success" ]; then
echo "Step 2: processing..."
echo "Status: completed"
else
echo "Step 2: skipped"
echo "Status: skipped"
fi
variables:
- name: "step2_status"
pattern: "Status: (.+)"
Real-time Output Events
The framework provides comprehensive real-time output events through callback functions. Each event includes:
- Pipeline name: The name of the executing pipeline
- Server name: The server where the event occurred (or "system" for system events)
- Step information: Complete step details including name, script, and configuration
- Event type: The type of output event
- Content: The actual output content
- Timestamp: When the event occurred
- Variables: Current variable state at the time of the event
Event Types
The framework supports the following event types:
Stdout
: Standard output from script executionStderr
: Standard error output from script executionLog
: System log messages and status updatesStepStarted
: Triggered when a step begins execution (🚀)StepCompleted
: Triggered when a step finishes execution (✅)
Event Callback Example
let output_callback = new;
Error Handling
The framework provides comprehensive error handling:
- SSH connection failures
- Script execution errors
- Variable extraction failures
- Configuration validation errors
All errors are logged with appropriate context and stack traces.
Implementation Example
Here's the complete implementation of the main program (src/main.rs
) that demonstrates how to use the net-shell framework:
// 模块声明
// 重新导出主要类型,方便外部使用
pub use RemoteExecutor;
pub use *;
use ;
use tracing_subscriber;
use env;
// 主函数用于演示实时输出功能
async
Key Features Demonstrated:
- Command Line Arguments: Supports specifying custom configuration file paths
- Real-time Output: Implements comprehensive real-time output handling with different output types
- Variable Tracking: Shows current variable state during execution
- Detailed Logging: Provides step-by-step execution details
- Result Summary: Generates comprehensive execution reports
- Statistics: Calculates success rates for pipelines and steps
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)
at your option.