-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
110 lines (98 loc) · 2.23 KB
/
main.go
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
/*
Author Gaurav Sablok
Universitat Potsdam
Date 2024-10-2
implementing the tcp, icp, uid connection check for the systemd to see the
system.d ctrl start service for the login and the host port check for
containers.
*/
import (
"log"
"os"
"os/exec"
"time"
"github.com/spf13/cobra"
)
func main() {
if err := rootCmd.Execute(); err != nil {
log.Fatal(err)
os.Exit(1)
}
}
var (
tcp string
udp string
tcp4 string
tcp6 string
// port string
)
var rootCmd = &cobra.Command{
Use: "options",
Long: "Provide the tcp, udp, 4tcp, 6tcp or the port for the check for the running containers ip address",
Run: flagFunc,
}
func init() {
rootCmd.Flags().StringVarP(&tcp, "tcp", "t", "provide the tcp", "tcp as a flag")
rootCmd.Flags().StringVarP(&udp, "udp", "u", "provide the udp", "udp as a flag")
rootCmd.Flags().StringVarP(&tcp6, "tcp6", "s", "provide the tcp", "tcp6 as a flag")
rootCmd.Flags().StringVarP(&tcp4, "tcp4", "f", "provide the tcp4", "tcp4 as a flag")
}
func flagFunc(cmd *cobra.Command, args []string) {
if tcp == "tcp" {
out, err := exec.Command("lsof", "-i", "tcp").Output()
if err != nil {
log.Fatal(err)
}
file, err := os.Create("tcpfile.txt")
if err != nil {
log.Fatal(err)
}
defer file.Close()
t := time.Now()
tstring := t.String()
file.WriteString(string(out) + tstring)
}
if udp == "udp" {
out, err := exec.Command("lsof", "-i", "udp").Output()
if err != nil {
log.Fatal(err)
}
file, err := os.Create("udpfile.txt")
if err != nil {
log.Fatal(err)
}
defer file.Close()
t := time.Now()
tstring := t.String()
file.WriteString(string(out) + tstring)
}
if tcp4 == "tcp4" {
out, err := exec.Command("lsof", "-i", "4tcp").Output()
if err != nil {
log.Fatal(err)
}
file, err := os.Create("tcp4file.txt")
if err != nil {
log.Fatal(err)
}
defer file.Close()
t := time.Now()
tstring := t.String()
file.WriteString(string(out) + tstring)
}
if tcp6 == "tcp6" {
out, err := exec.Command("lsof", "-i", "6tcp").Output()
if err != nil {
log.Fatal(err)
}
t := time.Now()
tstring := t.String()
file, err := os.Create("tcp6file.txt" + tstring)
if err != nil {
log.Fatal(err)
}
defer file.Close()
file.WriteString(string(out))
}
}