glbm

package module
v1.3.3 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2022 License: MPL-2.0 Imports: 13 Imported by: 0

README

glbm

介绍

glbm的全称是(Go Linux Basic module), 即使用Golang编写的Linux基础模块

Gitee 项目地址

特色

  • 使用全中文注释,即使小白也能轻松上手
  • 完全开源、永久免费

功能清单

  • 网络: 支持httpicmp状态检测,文件下载功能
  • 命令: 支持执行系统命令,支持sudo接口
  • 包管理: 支持调用apt安装、卸载包(单文件或者列表)
  • 系统: 支持sudo权限验证、UOS系统开发者状态检测
  • 文件管理: 增加读取全部,读取指定行,追加写入,覆盖写入功能

安装

关闭校验

设置环境变量

export GOSUMDB="off"
开始安装

首先进入你的项目根目录,然后执行下面的命令

go get gitee.com/liumou_site/glbm@latest

效果如下

liumou@liumou-PC:~/LinuxData/git/golang/install$ go get gitee.com/liumou_site/glbm@latest
go: downloading gitee.com/liumou_site/glbm v0.0.0-20221107150844-5eae14c86fd0
go: added gitee.com/liumou_site/glbm v0.0.0-20221107150844-5eae14c86fd0
go: added github.com/go-ping/ping v1.1.0
go: added github.com/google/uuid v1.2.0
go: added github.com/wonderivan/logger v1.0.0
go: added golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4
go: added golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
go: added golang.org/x/sys v0.0.0-20210315160823-c6e025ad8005

使用

Demo
package main

import (
	"fmt"
	"strings"

	"gitee.com/liumou_site/logger"
	"gitee.com/liumou_site/glbm"
)

// 测试命令执行
func Cmd() {
	sudo := SudoCommand("1")
	cc := Shell("ls /home", "列出文件")
	if cc {
		fmt.Println("命令1测试成功")
	}
	status, out := ShellGetOut("ls /home", "列出文件2")
	if status {
		fmt.Println("列出成功")
		fmt.Println(out)
	}
	sudo_c := sudo.ShellSudo("ls /home", "Sudo测试")
	if sudo_c {
		fmt.Println("sudo命令执行成功")
	} else {
		fmt.Println("Sudo命令执行失败")
	}
}

// 权限功能测试
func Ju() {
	g := CheckSudo("1")
	if g {
		fmt.Println("密码检验正确")
	} else {
		fmt.Println("密码错误或无权限")
	}
	d := Developer()
	if d {
		fmt.Println("已开启开发者")
	} else {
		fmt.Println("未开启开发者")
	}
}

// 用户信息测试
func GetUser() {
	get, username, uid, u_home := GetUserInfo(false)
	if get {
		fmt.Println(username)
		fmt.Println(uid)
		fmt.Println(u_home)
	}
}

// Apt 测试

func Apt() {
	apt := AptInitConfig("1")
	i := apt.install("vsftpd")
	if i {
		fmt.Println("ok")
	} else {
		fmt.Println("Failed to install")
	}
	r := apt.uninstall("vsftpd", "ftp")
	if r {
		fmt.Println("Uninstall is succeeded")
	} else {
		fmt.Println("Uninstall is Failed")
	}
	file := "go.deb vsftpd.deb"
	file_list := strings.Split(file, " ")
	s := apt.LocalInsallList(file_list, "测试")
	if s {
		logger.Info("ok")
	}
	f := apt.AptLocalInstallFile("/home/liumou/LinuxData/git/golang/modular/glbm/dpkg.go", "测试")
	if f {
		logger.Info("ok")
	}
}

func Dpkg() {
	dpkg := DpkgInstalled("docker.io")
	if dpkg {
		fmt.Println("ok")
	} else {
		fmt.Println("not installed")
	}
}

func OsVersion() {
	Version()
}
func OsInfo() {
	os_type, os_arch, ov := GetOsInfo()
	fmt.Println(os_type)
	fmt.Println(os_arch)
	fmt.Println(ov)
}

效果

[Running] go run "/home/liumou/LinuxData/git/golang/install/main.go"
Demo Code
baidu.com:443
连接成功
百度访问成功

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckSudo

func CheckSudo(password string) bool

判断是否拥有sudo权限

func CurlToSave

func CurlToSave(url, filename string, cover bool) bool

使用Curk下载文件到指定路径(cover是否覆盖已有文件)

func DeleteFile

func DeleteFile(file string) (bool, error)

删除文件

func Developer

func Developer() bool

检查是否开启开发者模式

func DpkgCheckInstalledKey added in v1.3.3

func DpkgCheckInstalledKey(pac1, pac2 string) (bool, string)

使用两个关键词查询本地是否已安装某个软件包

func DpkgCheckPackageVersion

func DpkgCheckPackageVersion(package_ string, version string) (status_ bool, ver_ string)

使用dpkg检查本地安装版本与标准版本是否一致

func DpkgConfig

func DpkgConfig(password string) *dpkg

DpkgConfig初始化(retime: 是否开启实时刷新)

func DpkgGetPackageStatus

func DpkgGetPackageStatus(pac_package string) (res map[string]string, status bool)

使用 Dpkg查询包状态, 通过res返回字典,通过status返回查询状态,字典key(status/name/version)

func DpkgInstalled

func DpkgInstalled(pac_package string) bool

使用Dpkg查询是否已安装(ii)

func Exists

func Exists(path string) (bool, error)

判断路径是否存在

func GetOsInfo

func GetOsInfo() (os_type, os_arch string, os_version int)

获取系统信息

func GetUserInfo

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

获取当前系统环境信息(display: 是否显示信息)

func IsDir

func IsDir(path string) (bool, error)

判断是否属于文件夹

func IsFile

func IsFile(path string) (bool, error)

判断是否属于文件

func ListToString

func ListToString(ls list.List, seq string) string

列表转字符串(seq: 拼接符,可以用: 空格 / _ @ 之类,具体请以实际需求设置)

func Mkdir

func Mkdir(dir string, mode fs.FileMode) (bool, error)

递归创建文件夹(dir 文件夹路径 mode 创建模式,例如: 0777)

func ReadLine

func ReadLine(line int, filename string) (string, error)

读取指定行

func ReadOnlyAll

func ReadOnlyAll(filename string) (string, error)

以只读方式读取文件所有内容

func RemoveIndexList

func RemoveIndexList(l []string, index int) []string

移除指定索引的列表元素

func RemoveNullList

func RemoveNullList(l []string) []string

移除空的列表元素

func RemoveValueList

func RemoveValueList(l []string, v string) []string

从列表中移除指定元素(str),不区分大小写

func SliceAppend

func SliceAppend(ls []string, v string) []string

切片添加元素

func SliceToString

func SliceToString(ls []string, sep string) string

字符串列表转字符串(seq: 拼接符,可以用: 空格 / _ @ 之类,具体请以实际需求设置)

func Version

func Version()

func WgetToSave

func WgetToSave(url, filename string) bool

使用wget下载文件到指定路径

func WriteTxtAppend

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

追加方式写入文本(wrap: 是否在末尾追加换行符)

func WriteTxtCover

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

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

Types

type AptPassword

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

定义一个命令行密码结构体

func AptInitConfig

func AptInitConfig(password string, debug bool) *AptPassword

Apt 管理初始化

func (*AptPassword) AptInstall

func (a *AptPassword) AptInstall(packages string) bool

使用apt在线安装包

func (*AptPassword) AptInstallList

func (a *AptPassword) AptInstallList(pac_list list.List) (ok, failed list.List)

通过apt安装包(列表传入)

func (*AptPassword) AptInstallSlice

func (a *AptPassword) AptInstallSlice(pac_list []string) (ok, failed list.List)

通过apt安装包(切片传入) - 推荐

func (*AptPassword) AptLocalInstallFile

func (a *AptPassword) AptLocalInstallFile(filename, name string) bool

使用apt安装本地文件(单个),安装之前将会检测文件是否存在

func (*AptPassword) AptLocalInstallStr

func (a *AptPassword) AptLocalInstallStr(install_str, name string) bool

使用apt安装本地文件(字符串),直接安装,不会检测文件是否存在

func (*AptPassword) AptUninstall

func (a *AptPassword) AptUninstall(pac_package string, name string) bool

通过apt卸载单个包

func (*AptPassword) AptUninstallList

func (a *AptPassword) AptUninstallList(pac_list list.List) (ok, failed list.List)

通过apt卸载包(列表传入)

func (*AptPassword) AptUninstallSclice

func (a *AptPassword) AptUninstallSclice(pac_list []string) (ok, failed list.List)

通过apt卸载包(列表传入)

func (*AptPassword) AptUpdateIndex

func (a *AptPassword) AptUpdateIndex() bool

更新索引

func (*AptPassword) AptUpgrade

func (a *AptPassword) AptUpgrade() bool

更新系统

func (*AptPassword) LocalInsallList

func (a *AptPassword) LocalInsallList(file_list []string, name string) bool

使用apt安装本地文件(列表传入)

type NetDevice

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

网卡管理

func NetDeviceInit

func NetDeviceInit(ty, status string) (n *NetDevice, res bool)

网络管理初始化 返回实例及设备获取结果(bool)

func (*NetDevice) GetConnectionList

func (n *NetDevice) GetConnectionList() bool

获取指定设备及类型的连接列表

Jump to

Keyboard shortcuts

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