File: tcp.go

package info (click to toggle)
golang-github-aelsabbahy-gonetstat 0.0~git20160428.0.edf89f7-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 76 kB
  • sloc: makefile: 4
file content (33 lines) | stat: -rw-r--r-- 797 bytes parent folder | download | duplicates (3)
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
package main

import (
	"fmt"

	"github.com/aelsabbahy/GOnetstat"
)

/* Get TCP information and show like netstat.
   Information like 'user' and 'name' of some processes will not show if you
   don't have root permissions */

func main() {
	d := GOnetstat.Tcp(false)

	// format header
	fmt.Printf("Proto %16s %20s %14s %24s\n", "Local Adress", "Foregin Adress",
		"State", "Pid/Program")

	for _, p := range d {

		// Check STATE to show only Listening connections
		if p.State == "LISTEN" {
			// format data like netstat output
			ip_port := fmt.Sprintf("%v:%v", p.Ip, p.Port)
			fip_port := fmt.Sprintf("%v:%v", p.ForeignIp, p.ForeignPort)
			pid_program := fmt.Sprintf("%v/%v", p.Pid, p.Name)

			fmt.Printf("tcp %16v %20v %16v %20v\n", ip_port, fip_port,
				p.State, pid_program)
		}
	}
}