系统检测应用(1)
测试环境:
- ubuntu16.04
- golang: go1.8
内容
GetEnvFunc() :获取环境变量函数
GetEnvSvr() :当前只获取cpu核数代码
package mainimport ( "bytes" "detect_sys/ming" "fmt" "os" "os/exec" /* "strconv" */ "strings")// OsEnv 系统环境变量获取type OsEnv struct { EnvUser string EnvHome string Pid int}func main() { fmt.Println("start") fmt.Println(GetEnvFunc()) GetEnvSvr() ming.StrFunc() fmt.Println("end")}//GetEnvFunc 获取环境变量函数func GetEnvFunc() (res OsEnv) { res.EnvUser = os.Getenv("USER") res.EnvHome = os.Getenv("HOME") res.Pid = os.Getpid() return res}// GetEnvSvr 1.获取cpu核数func GetEnvSvr() int { cmd := exec.Command("/bin/bash", "-c", "lscpu |grep 'CPU(s)' |grep -v 'li'|grep -v '-'|awk '{print $1}'") var out bytes.Buffer cmd.Stdout = &out err := cmd.Run() if err != nil { fmt.Println("error") return -1 } // 去除空格 str := strings.Replace(out.String(), " ", "", -1) // 去除换行符 str = strings.Replace(str, "\n", "", -1) fmt.Println("CPU核数:" + str) return 1}