-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
352 lines (286 loc) · 6.78 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package main
import (
"bufio"
"fmt"
"github.com/fatih/color"
g "github.com/soniah/gosnmp"
"log"
"os"
"strconv"
"strings"
"time"
)
func printValue(pdu g.SnmpPDU) error {
fmt.Printf("%s = ", pdu.Name)
switch pdu.Type {
case g.OctetString:
b := pdu.Value.([]byte)
fmt.Printf("STRING: %s\n", string(b))
default:
fmt.Printf("TYPE %d: %d\n", pdu.Type, g.ToBigInt(pdu.Value))
}
return nil
}
func retPorts() {
oid := ".1.3.6.1.4.1.43.10.26.1.1.1.5"
err := g.Default.Walk(oid, printValue)
if err != nil {
fmt.Printf("Walk Error: %v\n", err)
os.Exit(1)
}
}
func setPortCheck() {
var seg, port int
var aux []string
if _, err := os.Stat("ports.cfg"); err == nil {
fmt.Println("FILE DETECTED")
//File exists
file, _ := os.Open("ports.cfg")
fscanner := bufio.NewScanner(file)
for fscanner.Scan() {
if fscanner.Text()[0] != ';' {
aux = strings.Split(fscanner.Text(), ":")
seg, _ = strconv.Atoi(aux[1])
port, _ = strconv.Atoi(aux[0])
setPort(seg, port)
}
}
} else {
// Obtenemos primero el valor asociado al segmento 2:
fmt.Println("IGNORE FILE")
fmt.Print("Enter port: ")
fmt.Scanf("%d", &port)
fmt.Print("Enter segment: ")
fmt.Scanf("%d", &seg)
setPort(seg, port)
}
}
func setPort(seg int, port int) {
oid := ".1.3.6.1.4.1.43.10.26.1.1.1.5.1."
oid1 := oid + "100" + strconv.Itoa(seg)
oid2 := oid + strconv.Itoa(port)
result, err := g.Default.Get([]string{oid1})
if err != nil {
fmt.Printf("Get() Error: %v\n", err)
os.Exit(1)
}
var segInt interface{}
var seg2Int interface{}
for _, variable := range result.Variables {
if variable.Type != g.OctetString {
//fmt.Printf("%d\n", g.ToBigInt(variable.Value))
segInt = (variable.Value)
}
}
// Lo establecemos para el puerto port:
pdu := g.SnmpPDU{
Name: oid2,
Type: g.Integer,
Value: segInt,
}
_, err = g.Default.Set([]g.SnmpPDU{pdu})
result, err = g.Default.Get([]string{oid2})
for _, variable := range result.Variables {
if variable.Type != g.OctetString {
//fmt.Printf("%d\n", g.ToBigInt(variable.Value))
seg2Int = (variable.Value)
}
}
if seg2Int == segInt {
c := color.New(color.FgGreen)
c.Println("The port " + strconv.Itoa(port) + " has been changed")
} else {
c := color.New(color.FgRed)
c.Println("Error in port " + strconv.Itoa(port))
}
}
func getIP() {
oidConf := ".1.3.6.1.4.1.43.10.27.1.1.1.15.1"
oidIP := ".1.3.6.1.4.1.43.10.28.1.1.2."
oidMask := ".1.3.6.1.4.1.43.10.28.1.1.3."
oidGate := ".1.3.6.1.4.1.43.10.28.1.1.4."
var resInt interface{}
//Comprobar los valores de direccionamiento IP:
result, err := g.Default.Get([]string{oidConf})
if err != nil {
fmt.Printf("Get() Error: %v\n", err)
os.Exit(1)
}
for _, variable := range result.Variables {
if variable.Type != g.OctetString {
//fmt.Printf("%d\n", g.ToBigInt(variable.Value))
resInt = (variable.Value)
}
}
// Para comprobar el valor de la dirección IP del hub:
oidIP += fmt.Sprintf("%v", resInt)
result, err = g.Default.Get([]string{oidIP})
if err != nil {
fmt.Printf("Get() Error: %v\n", err)
os.Exit(1)
}
for _, variable := range result.Variables {
fmt.Print("IP Address: ")
c := color.New(color.FgYellow)
c.Println(variable.Value)
}
// Para comprobar el valor de la máscara de la dirección IP del hub:
oidMask += fmt.Sprintf("%v", resInt)
result, err = g.Default.Get([]string{oidMask})
if err != nil {
fmt.Printf("Get() Error: %v\n", err)
os.Exit(1)
}
for _, variable := range result.Variables {
fmt.Print("Subnet Mask: ")
c := color.New(color.FgCyan)
c.Println(variable.Value)
}
// Para comprobar el valor de la dirección IP del router por defecto:
oidGate += fmt.Sprintf("%v", resInt)
result, err = g.Default.Get([]string{oidGate})
if err != nil {
fmt.Printf("Get() Error: %v\n", err)
os.Exit(1)
}
for _, variable := range result.Variables {
fmt.Print("Default Router: ")
c := color.New(color.FgBlue)
c.Println(variable.Value)
}
}
func setIP(ip string) {
oid := ".1.3.6.1.4.1.43.10.28.1.1.4."
oidConf := ".1.3.6.1.4.1.43.10.27.1.1.1.15.1"
//Comprobar los valores de direccionamiento IP:
var resInt interface{}
result, err := g.Default.Get([]string{oidConf})
if err != nil {
fmt.Printf("Get() Error: %v\n", err)
os.Exit(1)
}
for _, variable := range result.Variables {
if variable.Type != g.OctetString {
resInt = (variable.Value)
}
}
oid += fmt.Sprintf("%v", resInt)
// Lo establecemos para el puerto port:
pdu := g.SnmpPDU{
Name: oid,
Type: g.IPAddress,
Value: ip,
}
g.Default.Set([]g.SnmpPDU{pdu})
}
func is_ipv4(host string) bool {
parts := strings.Split(host, ".")
if len(parts) < 4 {
return false
}
for _, x := range parts {
if i, err := strconv.Atoi(x); err == nil {
if i < 0 || i > 255 {
return false
}
} else {
return false
}
}
return true
}
func showMenu() int {
color.Magenta("-------MENU-------")
fmt.Println("1. Retrive segments")
fmt.Println("2. Modify port")
fmt.Println("3. Retrieve IP layer details")
fmt.Println("4. Change gateway")
fmt.Println("5. Exit")
fmt.Println("")
fmt.Print("Choose an option: ")
var i int
fmt.Scanf("%d", &i)
return i
}
func main() {
ip := ""
com := ""
args := os.Args[1:]
if len(args) >= 2 {
ip = args[0]
com = args[1]
}
color.Red("Welcome to Cirquit")
color.Yellow("Version 0.3")
color.Green("Licensed under GNU Public License v3")
fmt.Println("")
reader := bufio.NewReader(os.Stdin)
// Enter target IP
if ip == "" {
fmt.Print("Enter target IP: ")
ipN, _ := reader.ReadString('\n')
ip = strings.TrimSuffix(ipN, "\n")
}
isValidIp := is_ipv4(ip)
if !isValidIp {
color.Red("The IP introduced is TRASH")
os.Exit(3)
}
if com == "" {
// Enter community
fmt.Print("Enter Community: ")
comN, _ := reader.ReadString('\n')
com = strings.TrimSuffix(comN, "\n")
}
g.Default.Target = ip
g.Default.Community = com
g.Default.Version = g.Version1
g.Default.Timeout = time.Duration(10 * time.Second)
err := g.Default.Connect()
if err != nil {
log.Fatalf("Connect() err: %v", err)
}
defer g.Default.Conn.Close()
var option int
exit := false
for !exit {
if len(args) >= 3 {
option, _ = strconv.Atoi(args[2])
exit = true
} else {
option = showMenu()
}
switch option {
case 1:
retPorts()
case 2:
setPortCheck()
case 3:
getIP()
case 4:
// Para comprobar el valor de la dirección IP del hub:
var ip string
if len(args) == 4 {
ip = args[3]
} else {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter gateway IP: ")
ipN, _ := reader.ReadString('\n')
ip = strings.TrimSuffix(ipN, "\n")
}
isValidIp := is_ipv4(ip)
if !isValidIp {
color.Red("The IP introduced is TRASH")
os.Exit(3)
}
setIP(ip)
case 5:
exit = true
default:
color.Red("Not a valid option\n\n")
if exit == true {
os.Exit(2)
}
}
}
}