当前位置:   article > 正文

Golang实现一个批量自动化执行树莓派指令的软件(6)简易批量指令处理

Golang实现一个批量自动化执行树莓派指令的软件(6)简易批量指令处理

简介

基于上篇 Golang实现一个批量自动化执行树莓派指令的软件(5)模块整合, 这里我们实现简单的从配置文件设置指令集, 然后程序自动运行指令集的操作。

环境描述

运行环境: Windows, 基于Golang, 暂时没有使用什么不可跨平台接口, 理论上支持Linux/MacOS
目标终端:树莓派DebianOS(主要做用它测试)

实现

package controller

import (
	"encoding/json"
	"fmt"
	"os"
	"ssh_remote_access/app"
	"ssh_remote_access/models"
	"ssh_remote_access/sshutil"
	"ssh_remote_access/utils"
	"sync"
	"time"
)

type JobController struct {
	settings models.Settings
	sshutil  *sshutil.SSHUtil
}

func NewJobController(decryptionKey string, cfgPath string) (*JobController, error) {
	var (
		controller   = &JobController{}
		err          error
		encrypt      *utils.Encryption
		realPassword string
	)

	/* 加载文件 */
	if err = controller.loadConfig(cfgPath); nil != err {
		return nil, err
	}

	/* 组合配置信息 */
	encrypt = utils.NewEncryption(decryptionKey)
	if realPassword, err = encrypt.Decrypt(controller.settings.User.Password); nil != err {
		return nil, err
	}

	controller.sshutil, err = sshutil.NewSSHUtil(sshutil.SSHConfig{
		IP:       controller.settings.Network.IP,
		Port:     controller.settings.Network.Port,
		Timeout:  controller.settings.Network.Timeout,
		User:     controller.settings.User.Name,
		Password: realPassword,
	})

	return controller, err
}

func RunJobList() error {
	var (
		controller *JobController
		err        error
	)

	/* 初始化任务列表 */
	controller, err = NewJobController(app.ASE_KEY, app.CONFIG_PATH)
	if nil != err {
		fmt.Println("run job list fail, ", err.Error())
		return err
	}
	defer controller.Destroy()

	/* 执行任务 */
	err = controller.RunJobs(func(currFinishedJob models.SSHJob, jobNum, fnishedNum uint) error {
		fmt.Printf("job_type: %d, total job: %d, finished job: %d.\n", currFinishedJob.Type, jobNum, fnishedNum)
		return nil
	})
	return err
}

func (j *JobController) Destroy() error {
	return j.sshutil.Destroy()
}

func (j *JobController) loadConfig(filepath string) error {
	var bs, err = os.ReadFile(filepath)
	if nil != err {
		return err
	}
	err = json.Unmarshal(bs, &j.settings)
	return err
}

func (j *JobController) RunJobs(processCallback func(currFinishedJob models.SSHJob, jobNum, fnishedNum uint) error) error {
	var (
		waiter sync.WaitGroup
		err    error
	)
	for i, job := range j.settings.JobList {
		waiter.Add(1)
		time.Sleep(time.Duration(job.BeforeSleep) * time.Millisecond)
		switch models.SSH_TYPE(job.Type) {
		case models.SSH_COMMAND:
			_, err = j.sshutil.Commander().Command(job.Command)
			if nil != err {
				return err
			}
		case models.SSH_UPLOAD:
			err = j.sshutil.SFTFClient().UploadWithCallback(job.From, job.To, func(from, to string, num, uploaded uint) {
				fmt.Println("upload from ", from, " to ", to, ", total_num ", num, " upload_num ", uploaded)
			}, func(err error) {
				fmt.Println("upload finished")
			}, false)
			if nil != err {
				return err
			}
		case models.SSH_DOWNLOAD:
			err = j.sshutil.SFTFClient().DownloadWithCallback(job.From, job.To, func(from, to string, num, downloaded uint) {
				fmt.Println("download from ", from, " to ", to, ", total_num ", num, " download_num ", downloaded)
			}, func(err error) {
				fmt.Println("download finished")
			}, false)
			if nil != err {
				return err
			}
		}
		time.Sleep(time.Duration(job.AfterSleep) * time.Millisecond)
		if err = processCallback(job, uint(len(j.settings.JobList)), uint(i+1)); nil != err {

		}
	}

	return err
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126

测试用例

代码源

https://gitee.com/grayhsu/ssh_remote_access

视频

Golang实现远程ssh访问-简单批量化指令示例

其他

参考

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/489884
推荐阅读
相关标签
  

闽ICP备14008679号