-
Notifications
You must be signed in to change notification settings - Fork 548
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #97 from homelanmder/netstat
add(tools): Added ability to view network connection information like netstat
- Loading branch information
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} | ||
} | ||
} |