在线时间:8:00-16:00
132-9538-2358
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
package mainimport( "fmt" "os/exec" "os" "strings")func main(){ fmt.Println("shell") var str,ip,data []byte var err error var cmd *exec.Cmd // cmd = exec.Command("whoami") str,err = cmd.Output() if err!=nil{ fmt.Println(err) os.Exit(1) } fmt.Println(string(str)) fmt.Println("=======") //filter line breaks fmt.Println(strings.Trim(string(str),"\n")) //get IP ,Mac platform,can not implement fmt.Println("=======") cmd = exec.Command("/bin/sh", "-c", `/sbin/ifconfig en0 | grep -E 'inet ' | awk '{print $2}'`) ip,err = cmd.Output() if err!=nil{ fmt.Println(err) os.Exit(1) } fmt.Println(string(ip)) fmt.Println(strings.Trim(string(ip),"\n")) //implement command fmt.Println("====================") cmd = exec.Command("/bin/sh","-c","echo wo shi shui wo zai na") data,err = cmd.Output() if err!=nil{ fmt.Println(err) os.Exit(1) } fmt.Println(string(data)) fmt.Println(strings.Trim(string(data),"\n"))}
评论
请发表评论