gcs

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2022 License: Apache-2.0 Imports: 7 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

检查命令是否存在

func GetUserInfo

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

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

func ShellSystem added in v1.1.0

func ShellSystem(command string) (code int)

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

func Version

func Version()

Types

type ShellConfig added in v1.2.0

type ShellConfig struct {
	// contains filtered or unexported fields
}

定义命令的结构体

func ShellInit

func ShellInit(password string) *ShellConfig

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

func (*ShellConfig) GetStatusOutputBool added in v1.2.0

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

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

func (*ShellConfig) Shell added in v1.2.0

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

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

func (*ShellConfig) ShellGetOutSudo added in v1.2.0

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

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

func (*ShellConfig) ShellSudo added in v1.2.0

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

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

Jump to

Keyboard shortcuts

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