Skip to content

Commit

Permalink
Merge pull request #97 from homelanmder/netstat
Browse files Browse the repository at this point in the history
add(tools): Added ability to view network connection information like netstat
  • Loading branch information
neargle authored May 19, 2024
2 parents addb89f + e94df54 commit a1578d5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/cli/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package cli

import (
"fmt"
"github.com/cdk-team/CDK/pkg/tool/netstat"

"github.com/cdk-team/CDK/pkg/evaluate"
"github.com/cdk-team/CDK/pkg/plugin"
Expand Down Expand Up @@ -114,6 +115,8 @@ func ParseCDKMain() bool {
network.GetLocalAddresses()
case "ps":
ps.RunPs()
case "netstat":
netstat.RunNetstat()
case "probe":
if len(args) != 4 {
log.Println("Invalid input args.")
Expand Down
40 changes: 40 additions & 0 deletions pkg/tool/netstat/netstat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package netstat

import (
"fmt"
"github.com/shirou/gopsutil/v3/net"
"log"
"sort"
)

func RunNetstat() {
log.Printf("[+] run netstat, using RunNestat()")
stats, err := net.Connections("all")
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Printf("ipType\t\tconnection\tlocalAddr\t\t\tstatus\t\t\tremoteAddr\t\t\tpid\n")
sort.Slice(stats, func(i, j int) bool {
return stats[i].Type < stats[j].Type
})
for _, stat := range stats {
switch stat.Family {
case 2:
switch stat.Type {
case 1:
fmt.Printf("ipv4\t\ttcp\t\t%-16s\t\t%-13s\t\t%-16s\t\t%d\n", fmt.Sprintf("%s:%d", stat.Laddr.IP, stat.Laddr.Port), stat.Status, fmt.Sprintf("%s:%d", stat.Raddr.IP, stat.Raddr.Port), stat.Pid)
case 2:
fmt.Printf("ipv4\t\tudp\t\t%-16s\t\t%-13s\t\t%-16s\t\t%d\n", fmt.Sprintf("%s:%d", stat.Laddr.IP, stat.Laddr.Port), stat.Status, fmt.Sprintf("%s:%d", stat.Raddr.IP, stat.Raddr.Port), stat.Pid)
}
case 23:
switch stat.Type {
case 1:
fmt.Printf("ipv6\t\ttcp\t\t%-16s\t\t%-13s\t\t%-16s\t\t%d\n", fmt.Sprintf("%s:%d", stat.Laddr.IP, stat.Laddr.Port), stat.Status, fmt.Sprintf("%s:%d", stat.Raddr.IP, stat.Raddr.Port), stat.Pid)
case 2:
fmt.Printf("ipv6\t\tudp\t\t%-16s\t\t%-13s\t\t%-16s\t\t%d\n", fmt.Sprintf("%s:%d", stat.Laddr.IP, stat.Laddr.Port), stat.Status, fmt.Sprintf("%s:%d", stat.Raddr.IP, stat.Raddr.Port), stat.Pid)
}

}
}
}

0 comments on commit a1578d5

Please sign in to comment.