gcs

package module
v1.8.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 19, 2024 License: Apache-2.0 Imports: 15 Imported by: 3

README

gcs

介绍

使用Golang编写的系统命令执行模块,实现类似Python3os.Systemsubprocess.getstatusoutputsubprocess.getoutput

安装教程

执行下面的命令

go get -u gitee.com/liumou_site/gcs

效果如下

PS D:\data\git\Go\glbm> go  get -u gitee.com/liumou_site/gcs   
go: downloading gitee.com/liumou_site/gcs v1.1.0
go: downloading golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
go: added gitee.com/liumou_site/gcs v1.1.0
PS D:\data\git\Go\glbm> 

使用说明

package demo

import (
	"fmt"
	"gitee.com/liumou_site/gcs"
	"gitee.com/liumou_site/logger"
)

func GcsShell(cmd string) {
	shell := gcs.NewShell()
	shell.RunSudo(cmd)
	if shell.Err != nil {
		logger.Error("执行失败")
	} else {
		logger.Info("执行成功")
		fmt.Println(shell.Strings)
	}
}

func GcsSudo(cmd, password string)  {
	s := gcs.NewSudo(password)
	s.Realtime = true // 开启实时打印
	s.RunSudo(cmd)
	s.Line(1).Column(5, " ")
	fmt.Println(s.Strings)
}

常用参数说明

参数名称 作用
Realtime bool 实时打印命令执行反馈信息
Strings String 存储命令执行反馈文本信息

更多参数请查阅api.go文件

问题反馈

点击链接加入QQ群聊【坐公交也用券

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckCmd

func CheckCmd(cmd string) bool

CheckCmd 检查命令是否存在

@Description:
@param cmd 需要检查的命令
@return bool 是否存在

func GetUserInfo

func GetUserInfo(display bool) (ok bool, username string, userid int, UserHome string)

GetUserInfo 获取用户名,用户uid,用户家目录

@Description:
@param display 是否显示详细信息
@return ok 获取结果
@return username 用户名
@return userid 用户ID
@return UserHome 用户主目录

func Version

func Version() string

Version

@Description: 记录模块版本
@return string

Types

type ApiHandle added in v1.5.7

type ApiHandle struct {
	Strings string   // 处理结果
	Err     error    // 错误
	Slice   []string // 切片
}

type ApiShell added in v1.4.7

type ApiShell struct {
	PrintErr  bool // 打印详细错误信息(默认: true)
	PrintInfo bool // 是否打印详细信息(默认: false)
	Debug     bool // 是否打印调试信息(默认: false)

	Realtime bool   // 是否开启实时打印信息
	Result   bool   // 是否执行成功
	Text     string // 需要执行的命令
	OsType   string // 系统类型

	Strings  string // 输出结果
	ExitCode int    // 命令执行退出代码
	Err      error  // 错误

	Slice      []string // 数据切片
	Script     string   // 脚本文件
	ScriptMode bool     // 脚本文件

	Terminal     string // 使用的终端命令
	TerminalArg  string // 使用的终端参数
	TerminalBool bool   // 是否可以使用终端
	BlackHole    bool   // 是否使用黑洞模式(忽略错误信息)
	Ignore       bool   // 是否忽略标准输出
	// contains filtered or unexported fields
}

ApiShell @Description: 定义命令的结构体

func NewShell added in v1.4.6

func NewShell() *ApiShell

NewShell 命令实例构造函数 Realtime 是否开启实时打印数据

@Description:
@return *ApiShell

func (*ApiShell) AwkCol added in v1.7.5

func (api *ApiShell) AwkCol(col string)

AwkCol 打印命令执行反馈信息

func (*ApiShell) Column added in v1.5.5

func (api *ApiShell) Column(col int, sep string) *ApiShell

Column 截取指定列

func (*ApiShell) Echo added in v1.5.5

func (api *ApiShell) Echo()

Echo 打印命令执行反馈信息

func (*ApiShell) Grep added in v1.5.7

func (api *ApiShell) Grep(match string) *ApiShell

Grep 通过关键词筛选所有匹配行

func (*ApiShell) Line added in v1.5.5

func (api *ApiShell) Line(n int) *ApiShell

Line 截取指定行

func (*ApiShell) ProcessIsRunning added in v1.8.0

func (api *ApiShell) ProcessIsRunning(command string) bool

ProcessIsRunning 通过进程命令检测进程是否存在,例如传入: dpkg

func (*ApiShell) RunScript added in v1.5.8

func (api *ApiShell) RunScript(command ...string)

RunScript 通过生成脚本的方式执行Shell,支持Shell管道

@Description:
@receiver api
@param command 需要执行的命令,例如 'apt', 'update',命令与选项可以分开传入

func (*ApiShell) RunShell added in v1.4.7

func (api *ApiShell) RunShell(command ...string)

RunShell 执行命令

@Description:
@receiver api
@param command 需要执行的命令,例如 'apt', 'update',命令与选项可以分开传入

func (*ApiShell) RunShellList added in v1.7.7

func (api *ApiShell) RunShellList(cs []string)

RunShellList 批量执行命令

@Description: 通过传入完整的命令切片遍历执行,当遇到执行错误立即返回
@receiver api
@param cs 需要执行的命令切片,每个切片元素必须是完整命令

func (*ApiShell) RunTerminal added in v1.7.8

func (api *ApiShell) RunTerminal(command ...string)

RunTerminal 通过调用图形化终端程序的方式执行Shell命令,使用脚本封装的方式,支持Shell管道

@Description:
@receiver api
@param command 需要执行的命令,例如 'apt', 'update',命令与选项可以分开传入

func (*ApiShell) RunTimeout added in v1.7.4

func (api *ApiShell) RunTimeout(n time.Duration, command ...string) *ApiShell

RunTimeout 执行系统命令并设置超时时间

@Description:
@receiver api
@param n 超时时间(秒)
@param command 需要执行的命令,例如 'apt', 'update',命令与选项可以分开传入
@return *ApiShell

type ApiSudo added in v1.4.7

type ApiSudo struct {
	SudoPath string // sudo命令绝对路径
	Password string // sudo权限使用的密码

	ApiShell // 继承Shell实例
	// contains filtered or unexported fields
}

ApiSudo 定义命令的结构体

func NewSudo added in v1.4.6

func NewSudo(password string) *ApiSudo

NewSudo 命令实例构造函数, 当不需要执行sudo命令的时候直接传入任意字符串给password即可

@Description:
@param password 主机密码
@return *ApiSudo

func (*ApiSudo) Grep added in v1.5.8

func (api *ApiSudo) Grep(match string) *ApiSudo

Grep

@Description: 从结果中筛选关键词
@receiver api
@param match
@return *ApiSudo

func (*ApiSudo) RunScriptSudo added in v1.7.0

func (api *ApiSudo) RunScriptSudo(command ...string)

RunScriptSudo 通过生成脚本的方式执行Shell,支持Shell管道

@Description:
@receiver api
@param command

func (*ApiSudo) RunSudo added in v1.7.0

func (api *ApiSudo) RunSudo(command ...string)

RunSudo 通过sudo执行命令

@Description:
@receiver api
@param command

func (*ApiSudo) RunSudoList added in v1.7.7

func (api *ApiSudo) RunSudoList(cs []string)

RunSudoList 通过sudo执行命令

@Description:
@receiver api
@param cs

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL