forked from yinqiwen/gscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hosts.go
executable file
·49 lines (43 loc) · 847 Bytes
/
hosts.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
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
type HostIP struct {
Host string
IP string
// Verifying bool
}
type HostIPTable map[string]HostIP
func parseHostsFile(file string) (HostIPTable, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
hosts := make(HostIPTable)
scanner := bufio.NewScanner(f)
lineno := 1
for scanner.Scan() {
line := scanner.Text()
line = strings.TrimSpace(line)
//comment start with '#'
if strings.HasPrefix(line, "#") || len(line) == 0 {
continue
}
ss := strings.Fields(line)
if len(ss) != 2 {
return nil, fmt.Errorf("Invalid line:%d in hosts file:%s", lineno, file)
}
pair := HostIP{
Host: ss[1],
IP: ss[0],
// Verifying: false,
}
hosts[ss[1]] = pair
lineno = lineno + 1
}
return hosts, nil
}