-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvping.v
83 lines (77 loc) · 1.77 KB
/
vping.v
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
module vping
import os
// ping () -> runs the ping process and returns an <Answer> Object (Struct)
pub fn ping(pingconf Conf) Answer
{
result := prepare(pingconf).join(" ")
executed := os.execute(result)
//println("CODE -> " + executed.exit_code.str()) // [0 -> Funzt, 1 -> Packet Loss, 2 -> Domain or name not found]
return answer(executed, pingconf, result)
}
// answer () -> helper function to generate the <Answer> Object
pub fn answer(executed os.Result, pingconf Conf, cmd string) Answer
{
return Answer{
conf: pingconf
command: cmd
status: executed.exit_code
raw: executed.output
parsed: parse(executed.output)
}
}
// prepare () -> Generate the command as an array | example: ["ping", "host.ip", "-c", "1"]
fn prepare(pingconf Conf) []string
{
mut bmap := map[string]string{}
$if windows {
bmap = windows_map.clone()
}
$if linux {
bmap = linux_map.clone()
}
mut result := ["ping", pingconf.ip]
if pingconf.count > 0
{
result << bmap["count"]
result << pingconf.count.str()
}
if pingconf.timeout > 0
{
result << bmap["timeout"]
result << pingconf.timeout.str()
}
if pingconf.size > 0
{
result << bmap["size"]
result << pingconf.size.str()
}
if pingconf.force in [4,6]
{
//in this case the argument itself is the argument, which means we hat to implement a replace
result << bmap["force"].replace("$", pingconf.force.str())
}
if pingconf.ttl > 0
{
result << bmap["ttl"]
result << pingconf.ttl.str()
}
//linux only:
$if linux {
if pingconf.iface != ""
{
result << bmap["iface"]
result << pingconf.iface
}
if pingconf.interval > 0
{
result << bmap["interval"]
result << pingconf.interval.str()
}
}
//custom string | !! Security relevance !!:
if pingconf.custom != ""
{
result << pingconf.custom
}
return result
}