gcs

package module
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2022 License: Apache-2.0 Imports: 10 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 Version

func Version()

func WriteTxtCover added in v1.3.1

func WriteTxtCover(text, filename string, wrap bool) (res bool, err error)

WriteTxtCover 写入文本到文件中,当文件已存在则覆盖,当文件不存在则创建,默认权限: 0666

Types

type ShellConfig added in v1.2.0

type ShellConfig struct {
	SudoPath  string // sudo命令绝对路径
	ShellFile string // 生成的Shell脚本
	// contains filtered or unexported fields
}

ShellConfig 定义命令的结构体

func ShellInit

func ShellInit(password string, realtime bool) *ShellConfig

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

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) ShellSystem added in v1.3.1

func (shells *ShellConfig) ShellSystem(command string) (code int)

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

func (*ShellConfig) SudoBase added in v1.3.1

func (shells *ShellConfig) SudoBase() (int, string, error)

SudoBase 通过Sudo执行脚本

func (*ShellConfig) System added in v1.3.1

func (shells *ShellConfig) System() (code int, out string, err error)

System 执行命令并支持实时获取输出,和Python的os.system函数一样,返回退出代码 command 需要执行的命令 realtime 是否开启实时打印

func (*ShellConfig) WriShellFile added in v1.3.1

func (shells *ShellConfig) WriShellFile(txt string) bool

WriShellFile 写入执行语句到文件

Jump to

Keyboard shortcuts

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