forked from gotoolkits/libnetgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibnet.go
More file actions
83 lines (71 loc) · 2 KB
/
Copy pathlibnet.go
File metadata and controls
83 lines (71 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"flag"
"fmt"
"github.com/gotoolkits/libnetgo/api"
"github.com/gotoolkits/libnetgo/common"
"github.com/gotoolkits/libnetgo/connect"
"github.com/gotoolkits/libnetgo/netstat"
log "github.com/sirupsen/logrus"
"os"
"time"
)
var (
h bool
svr bool
console bool
host string
interval int
connType string
)
func init() {
flag.BoolVar(&h, "h", false, "libnetgo help")
flag.BoolVar(&svr, "s", true, "api server")
flag.BoolVar(&console, "c", false, "console mode")
flag.StringVar(&host, "ip", "", "ip address for pcap.")
flag.IntVar(&interval, "r", 3, "To get datas interval,default 3 second.")
flag.StringVar(&connType, "t", "all", "all/remote/local")
flag.Usage = usage
}
func main() {
flag.Parse()
if h {
flag.Usage()
os.Exit(0)
}
if svr {
go api.ServerRun()
}
//api.ServerRun()
if len(host) > 1 {
if ok, _ := common.VerifyIP(host); ok {
api.HostIP = host
}
} else {
log.Warningln("Unspecified IP parameter'-ip', unable to open packet capture function")
}
for {
if console {
formatNetstat(connect.GetConns(connType))
} else {
connect.GetConns(connType)
}
time.Sleep(time.Duration(interval) * time.Second)
}
}
func formatNetstat(ns map[string]netstat.Process) {
fmt.Println("=======================================================================================================================")
fmt.Println("| PID | USER | IPLINK |IN|OUT(k)|SENT|RECEIVED(kB/s)| PROGRAM |")
fmt.Println("=======================================================================================================================")
for itemId, info := range ns {
fmt.Printf(" %-6s %-8s %-40s %-6d%-6d%-6d%-6d %-s\n",
info.Pid, info.User, itemId, info.In/1024, info.Out/1024, info.OutRate/1024, info.InRate/1024, info.Exe)
}
}
func usage() {
fmt.Fprintf(os.Stderr, `libnetgo version: libnetgo/1.0
Usage: libnetgo [-hs] [-ip ipAddr] [-r interval] [-t all/remote/local]
Options:
`)
flag.PrintDefaults()
}