重构 auto-check:状态数据下沉各包,workflow 精简为纯调度
- discovery: ping 探测存活 + ARP 解析 MAC,包级 Devices map,串行扫描 - stress: 脚本生成模式(BuildScript/ParseResults),ToolSet 工具检测,包级 Results map - report: 包级 Printed map,SaveAndPrintDeviceReport 保存+打印 - sshclient: 包级 RunCommand - workflow: 仅持有 config,Start() 固定循环,直接调用各包方法 - config: 移除 ScanConfig.Concurrency(串行扫描无需并发数)
This commit is contained in:
@@ -2,14 +2,15 @@ package sshclient
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"golang.org/x/crypto/ssh"
|
||||
)
|
||||
|
||||
// Client SSH 客户端封装
|
||||
type Client struct {
|
||||
// Config SSH 连接配置(不含连接状态)
|
||||
type Config struct {
|
||||
User string
|
||||
Password string
|
||||
KeyFile string
|
||||
@@ -17,9 +18,9 @@ type Client struct {
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
// NewClient 创建 SSH 客户端
|
||||
func NewClient(user, password, keyFile string, port int, timeout time.Duration) *Client {
|
||||
return &Client{
|
||||
// NewConfig 创建配置
|
||||
func NewConfig(user, password, keyFile string, port int, timeout time.Duration) *Config {
|
||||
return &Config{
|
||||
User: user,
|
||||
Password: password,
|
||||
KeyFile: keyFile,
|
||||
@@ -28,23 +29,23 @@ func NewClient(user, password, keyFile string, port int, timeout time.Duration)
|
||||
}
|
||||
}
|
||||
|
||||
// Connect 尝试 SSH 连接,返回 SSH client
|
||||
func (c *Client) Connect(ip string) (*ssh.Client, error) {
|
||||
config, err := c.buildConfig()
|
||||
// Connect 建立 SSH 连接,由调用方负责 Close
|
||||
func Connect(ip string, cfg *Config) (*ssh.Client, error) {
|
||||
sshCfg, err := cfg.buildSSHConfig()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("SSH 配置构建失败: %w", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
addr := fmt.Sprintf("%s:%d", ip, c.Port)
|
||||
client, err := ssh.Dial("tcp", addr, config)
|
||||
addr := net.JoinHostPort(ip, fmt.Sprintf("%d", cfg.Port))
|
||||
client, err := ssh.Dial("tcp", addr, sshCfg)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("SSH 连接 %s 失败: %w", addr, err)
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
|
||||
// RunCommand 在远程主机执行命令,返回输出
|
||||
func (c *Client) RunCommand(client *ssh.Client, command string) (string, error) {
|
||||
// RunCommand 在已有 SSH 连接上执行命令,返回输出(实现 stress.Executor 接口)
|
||||
func RunCommand(client *ssh.Client, command string) (string, error) {
|
||||
session, err := client.NewSession()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("创建会话失败: %w", err)
|
||||
@@ -53,21 +54,19 @@ func (c *Client) RunCommand(client *ssh.Client, command string) (string, error)
|
||||
|
||||
out, err := session.CombinedOutput(command)
|
||||
if err != nil {
|
||||
return string(out), fmt.Errorf("命令执行失败: %w, 输出: %s", err, string(out))
|
||||
return string(out), fmt.Errorf("命令执行失败: %w", err)
|
||||
}
|
||||
return string(out), nil
|
||||
}
|
||||
|
||||
// buildConfig 构建 SSH 认证配置
|
||||
func (c *Client) buildConfig() (*ssh.ClientConfig, error) {
|
||||
// buildSSHConfig 构建认证配置
|
||||
func (c *Config) buildSSHConfig() (*ssh.ClientConfig, error) {
|
||||
var authMethods []ssh.AuthMethod
|
||||
|
||||
// 密码认证
|
||||
if c.Password != "" {
|
||||
authMethods = append(authMethods, ssh.Password(c.Password))
|
||||
}
|
||||
|
||||
// 密钥认证
|
||||
if c.KeyFile != "" {
|
||||
key, err := os.ReadFile(c.KeyFile)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user