This is a creation in Article, where the information may have evolved or changed.
Package Mainimport ("FMT" "Github.com/axgle/mahonia" "github.com/shirou/gopsutil/net" nnet "NET" "OS" "Os/exec" " Strings "" Time ") var debug stringfunc main () {if Len (OS. Args)! = 3 && len (OS. Args)! = 4 {fmt. PRINTLN ("Args error ...") Return}r_name: = Strings. ToUpper (OS. ARGS[1]) I_name: = Strings. ToUpper (OS. ARGS[2]) If Len (OS. Args) = = 4 {debug = OS. Args[3]}var Addr Stringlis, _: = Nnet. Interfaces () for _, V: = range lis {if strings. ToUpper (v.name) = = R_name {addr = v.hardwareaddr.string ()}if debug = = "true" {fmt. Println (v)}}if len (addr) = = 0 {addr = Cmd_get_mac (r_name) If Len (addr) < 2 {Return}}var num []uint64 = make ([]uint64, 2) For I: = 0; I < 2; i++ {netio, err: = Net.netiocounters (TRUE) if err! = Nil {fmt. PRINTLN (Err) Return}for _, V: = Range NetIO {if debug = = "true" {fmt. Printf ("HardWare Addr:%s\n%s\n", Addr, Nnet. HARDWAREADDR (V.addr[:8]). String ()) fmt. Println (v)}if strings. Contains (nnet. HARDWAREADDR (V.addr[:8]). String (), addr) {switch I_name {case ' in ': num[i] = v.bytesrecvcase "Out": num[i] = v.bytessent}}}if i = = 1 {break}time. Sleep (5e9)}if debug = = "true" {fmt. PRINTLN (num)}fmt. Print ((num[1]-num[0])/5.00)}func Cmd_get_mac (name string) string {cmd: = exec. Command ("Getmac", "/fo", "CSV", "/V", "/nh") buf, err: = cmd. Output () if err! = Nil {if debug = = "true" {fmt. PRINTLN (Err)}return ""}s: = Mahonia. Newdecoder ("GB18030"). Convertstring (String (BUF)) for _, V: = Range strings. Split (S, "\ n") {if strings. Contains (V, name) {hwaddr, err: = Nnet. Parsemac (Split (v)) if err! = Nil && debug = = "true" {fmt. PRINTLN (Err) Continue}return hwaddr. String ()}}return ""}func split (s string) string {lis: = strings. Split (S, ",") If Len (LIS) < 3 {return ""}return strings. Trim (lis[2], ' "')}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.