-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathtimeparsefilter.go
45 lines (38 loc) · 918 Bytes
/
timeparsefilter.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
package main
import (
"bufio"
"fmt"
"net"
"os"
"strconv"
"strings"
"time"
)
func main() {
fmt.Printf("|IP address|Download time|DNS name|\n")
fmt.Printf("|----------|-------------|--------|\n")
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
sp := strings.IndexByte(line, ' ')
if sp > -1 {
// Convert "epoch" time to meaninglful timestamp
sec, err := strconv.ParseInt(string(line[sp+1:]), 10, 64)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
continue
}
t := time.Unix(sec, 0)
// Get host name for IP address
names, nerr := net.LookupAddr(line[:sp])
name := "N/A"
if nerr == nil {
name = names[0]
name = name[0:len(name)-1]
}
fmt.Printf("|%s|%s|%s|\n", line[:sp], string(t.Format("2006-01-02T03:04:05.000-0700")), name)
} else {
fmt.Fprintf(os.Stderr, "Did not find a space in: %q\n", line)
}
}
}