@@ -20,13 +20,15 @@ import (
20
20
"context"
21
21
"flag"
22
22
"fmt"
23
-
24
23
"github.com/chaosblade-io/chaosblade-spec-go/channel"
25
24
"github.com/chaosblade-io/chaosblade-spec-go/spec"
25
+ "strings"
26
26
27
27
"github.com/chaosblade-io/chaosblade-exec-os/exec/bin"
28
28
)
29
29
30
+ const sep = ","
31
+
30
32
var dnsDomain , dnsIp string
31
33
var changeDnsStart , changeDnsStop bool
32
34
@@ -57,13 +59,18 @@ var cl = channel.NewLocalChannel()
57
59
// startChangeDns by the domain and ip
58
60
func startChangeDns (domain , ip string ) {
59
61
ctx := context .Background ()
60
- dnsPair := createDnsPair (domain , ip )
61
- response := cl .Run (ctx , "grep" , fmt .Sprintf (`-q "%s" %s` , dnsPair , hosts ))
62
- if response .Success {
63
- bin .PrintErrAndExit (fmt .Sprintf ("%s has been exist" , dnsPair ))
64
- return
62
+ domains := strings .Split (domain , sep )
63
+ var dnsPairs string
64
+ for _ , v := range domains {
65
+ dnsPair := createDnsPair (v , ip )
66
+ response := cl .Run (ctx , "grep" , fmt .Sprintf (`-q "%s" %s` , dnsPair , hosts ))
67
+ if response .Success {
68
+ bin .PrintErrAndExit (fmt .Sprintf ("%s has been exist" , dnsPair ))
69
+ return
70
+ }
71
+ dnsPairs = dnsPairs + "\n " + dnsPair
65
72
}
66
- response = cl .Run (ctx , "echo" , fmt .Sprintf (`"%s" >> %s` , dnsPair , hosts ))
73
+ response : = cl .Run (ctx , "echo" , fmt .Sprintf (`"%s" >> %s` , dnsPairs , hosts ))
67
74
if ! response .Success {
68
75
bin .PrintErrAndExit (response .Err )
69
76
return
@@ -74,22 +81,24 @@ func startChangeDns(domain, ip string) {
74
81
// recoverDns
75
82
func recoverDns (domain , ip string ) {
76
83
ctx := context .Background ()
77
- dnsPair := createDnsPair (domain , ip )
78
- response := cl .Run (ctx , "grep" , fmt .Sprintf (`-q "%s" %s` , dnsPair , hosts ))
79
- if ! response .Success {
80
- bin .PrintOutputAndExit ("nothing to do" )
81
- return
82
- }
84
+ domains := strings .Split (domain , sep )
85
+ for _ , v := range domains {
86
+ dnsPair := createDnsPair (v , ip )
87
+ response := cl .Run (ctx , "grep" , fmt .Sprintf (`-q "%s" %s` , dnsPair , hosts ))
88
+ if ! response .Success {
89
+ bin .PrintOutputAndExit ("nothing to do" )
90
+ return
91
+ }
83
92
84
- if ! cl .IsCommandAvailable ("cat" ) {
85
- bin .PrintErrAndExit (spec .CommandCatNotFound .Msg )
86
- }
87
-
88
- response = cl . Run ( ctx , "cat" , fmt . Sprintf ( `%s | grep -v "%s" > %s && cat %s > %s` ,
89
- hosts , dnsPair , tmpHosts , tmpHosts , hosts ))
90
- if ! response .Success {
91
- bin . PrintErrAndExit ( response . Err )
92
- return
93
+ if ! cl .IsCommandAvailable ("cat" ) {
94
+ bin .PrintErrAndExit (spec .CommandCatNotFound .Msg )
95
+ }
96
+ response = cl . Run ( ctx , "cat" , fmt . Sprintf ( `%s | grep -v "%s" > %s && cat %s > %s` ,
97
+ hosts , dnsPair , tmpHosts , tmpHosts , hosts ))
98
+ if ! response . Success {
99
+ bin . PrintErrAndExit ( response .Err )
100
+ return
101
+ }
93
102
}
94
103
cl .Run (ctx , "rm" , fmt .Sprintf (`-rf %s` , tmpHosts ))
95
104
}
0 commit comments