Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: netstat install #26

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions service/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func AgentRegister() (err error) {
var strErr bytes.Buffer
var out bytes.Buffer
fmt.Println(req.Pid)
netstatOperate()
if OS == "windows" {
cmd = exec.Command("netstat", "-ano")
cmd.Stderr = &strErr
Expand Down Expand Up @@ -166,6 +167,40 @@ func AgentRegister() (err error) {
return nil
}

// 判断如果有netstat则继续等待,否则就是安装,安装失败则panic,提醒用户。
func netstatOperate() {
var look = func(add func()) {
cmd := exec.Command("netstat", "-e")
err := cmd.Run()
if err != nil {
add()
}
}
var add = func() {
install := make(map[string][]string)
install["apt-get"] = []string{"install", "net-tools"}
install["yum"] = []string{"-y", "install", "net-tools"}

var err error

var execCommand = func(tool string, params []string) {
cmd := exec.Command(tool, params...) // 拼接命令
err = cmd.Start()
}

for key, value := range install {
execCommand(key, value)
if err == nil {
return
}
}
if err != nil {
panic("请自行安装netstat工具")
}
}
look(add)
}

func getCurrentPath() (string, error) {
file, err := exec.LookPath(os.Args[0])
if err != nil {
Expand Down