-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimtool.go
125 lines (117 loc) · 2.28 KB
/
simtool.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
package trygo
import (
"bufio"
"flag"
"fmt"
"net"
"os"
"os/exec"
"sort"
"strconv"
"strings"
)
// DNSLookup func
func DNSLookup() {
dnssec := flag.Bool("dnssec", false, "Request DNSSEC records")
port := flag.String("port", "53", "Set the query port")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [OPTIONS] [name ...]\n", os.Args[0])
flag.PrintDefaults()
}
flag.Parse()
if *dnssec {
}
if *port == "53" {
}
}
// Psgrp func
func Psgrp() {
ps := exec.Command("ps", "-e", "-opid,ppid,comm")
output, _ := ps.Output()
child := make(map[int][]int)
for i, s := range strings.Split(string(output), "\n") {
if i == 0 || len(s) == 0 {
continue
}
f := strings.Fields(s)
fp, _ := strconv.Atoi(f[0])
fpp, _ := strconv.Atoi(f[1])
child[fpp] = append(child[fpp], fp)
}
schild := make([]int, len(child))
i := 0
for k := range child {
schild[i] = k
i++
}
sort.Ints(schild)
for _, ppid := range schild {
fmt.Printf("Pid %d has %d child", ppid, len(child[ppid]))
if len(child[ppid]) == 1 {
fmt.Printf(": %v\n", child[ppid])
continue
}
fmt.Printf("ren: %v\n", child[ppid])
}
}
// Wc func
func Wc() {
var chars, words, lines int
r := bufio.NewReader(os.Stdin)
for {
switch s, ok := r.ReadString('\n'); true {
case ok != nil:
fmt.Printf("%d %d %d\n", chars, words, lines)
return
default:
chars += len(s)
words += len(strings.Fields(s))
lines++
}
}
}
// Uniq func
func Uniq() {
list := []string{"a", "b", "a", "a", "c", "d", "e", "f"}
first := list[0]
fmt.Printf("%s ", first)
for _, v := range list[1:] {
if first != v {
fmt.Printf("%s ", v)
first = v
}
}
}
// EchoServer func
// Usage
// $ ./test # server side
// $ nc 127.0.0.1 8053 # client side
// abc
// abc
// true loop by default!
func EchoServer() {
l, err := net.Listen("tcp", "127.0.0.1:8053")
if err != nil {
fmt.Printf("Failure to listen: %s\n", err.Error())
return
}
for {
if c, err := l.Accept(); err == nil {
go Echo(c)
}
}
}
// Echo func
func Echo(c net.Conn) {
defer c.Close()
line, err := bufio.NewReader(c).ReadString('\n')
if err != nil {
fmt.Printf("Failure to read: %s\n", err.Error())
return
}
_, err = c.Write([]byte(line))
if err != nil {
fmt.Printf("Failure to write: %s\n", err.Error())
return
}
}