gcs

package module
v1.2.9 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2022 License: Apache-2.0 Imports: 12 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 main

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

// 命令执行是否成功
func Shell() {
	// 执行成功
	cc := ShellInit("1")
	c := strings.Split("ls /home/liumou@ls /home/lll@lw /home/", "@")
	for _, s := range c {
		res := cc.Shell(s)
		if res {
			logger.Info("执行成功")
		} else {
			logger.Error("执行失败")
		}
	}
}
// 查找命令是否存在
func exists() {
	ls_ := CheckCmd("dir")
	if ls_ {
		logger.Info("存在")
	} else {
		logger.Error("不存在命令")
	}
}

// 返回命令反馈字符串和布尔值
func ShellOutBool() {
	// 执行成功
	cc := ShellInit("1")
	c := strings.Split("ls /home/liumou@ls /home/lll@lw /home/@ls -la", "@")
	for _, s := range c {
		res, out := cc.GetStatusOutputBool(s)
		if res {
			logger.Info("执行成功")
		} else {
			logger.Error("执行失败")
		}
		fmt.Println(out)
	}
}

// 实时刷新命令信息
func ShellRe() {
	// 执行成功
	c := strings.Split("ls /home/liumou@lss@apt list", "@")
	for _, s := range c {
		code := ShellSystem(s)
		if code != 0 {
			logger.Error("执行失败")
		} else {
			logger.Info("执行成功")
		}
		logger.Debug("退出代码: ", code)
	}
}

func main() {
	Shell()
	exists()
	ShellOutBool()
	ShellRe()
}

三方模块

  • gitee.com/liumou_site/logger v1.1.1 // indirect
  • github.com/spf13/cast v1.5.0 // indirec

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckCmd

func CheckCmd(cmd string) bool

CheckCmd 检查命令是否存在

func GetUserInfo

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

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

func ScriptSystem added in v1.2.6

func ScriptSystem(cmd string) (string, error)

ScriptSystem 通过Script模块实现基础功能

func ShellSystem added in v1.1.0

func ShellSystem(command string) (code int)

ShellSystem 执行命令并实时获取输出,和Python的os.system函数一样,返回退出代码

func Version

func Version()

Types

type ShellConfig added in v1.2.0

type ShellConfig struct {
	SudoPath string // sudo命令绝对路径
	// contains filtered or unexported fields
}

ShellConfig 定义命令的结构体

func ShellInit

func ShellInit(password string, realtime bool) *ShellConfig

ShellInit 命令实例构造函数, 当不需要执行sudo命令的时候直接传入任意字符串给password即可 realtime 是否开启实时打印数据

func (*ShellConfig) Execute added in v1.2.6

func (shells *ShellConfig) Execute(cmd *exec.Cmd) (string, error)

Execute 执行命令

func (*ShellConfig) GetStatusOutputBool added in v1.2.0

func (shells *ShellConfig) GetStatusOutputBool(cmd string) (bool, string)

GetStatusOutputBool 执行命令并获取输出数据, 返回执行结果布尔值 - (成功/失败)及 命令输出 (字符串)

func (*ShellConfig) Shell added in v1.2.0

func (shells *ShellConfig) Shell(command string) bool

Shell 执行命令并获取执行结果, 返回执行结果(成功/失败)

func (*ShellConfig) ShellGetOutSudo added in v1.2.0

func (shells *ShellConfig) ShellGetOutSudo(cmd string) (string, error)

ShellGetOutSudo 通过Sudo权限执行命令并获取输出数据, 返回执行结果(成功/失败)及命令输出(字符串)

func (*ShellConfig) ShellSudo added in v1.2.0

func (shells *ShellConfig) ShellSudo(command string) bool

ShellSudo 执行命令并获取执行结果, 返回执行结果(成功/失败)

func (*ShellConfig) SudoTimeOut added in v1.2.7

func (shells *ShellConfig) SudoTimeOut(cmd string, timeout int) (string, error)

SudoTimeOut 支持设置超时的命令执行函数

func (*ShellConfig) SystemTimeOut added in v1.2.7

func (shells *ShellConfig) SystemTimeOut(cmd string, timeout int) (string, error)

SystemTimeOut 支持设置超时的命令执行函数

Jump to

Keyboard shortcuts

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