update host file
This commit is contained in:
parent
a1fdeb62fc
commit
c1b5b0c2c5
@ -580,19 +580,20 @@ func (h *Host) SetIPHosts(ip string, hosts ...string) error {
|
||||
}
|
||||
|
||||
func (h *Host) SetHostIPs(host string, ips ...string) error {
|
||||
info := h.ListByHost(host)
|
||||
if len(info) == 0 {
|
||||
return h.AddHosts(host, ips...)
|
||||
} else if len(info) == 1 {
|
||||
info[0].ip = ips[0]
|
||||
info[0].comment = ""
|
||||
return nil
|
||||
if len(ips) == 0 {
|
||||
return fmt.Errorf("no ip address")
|
||||
}
|
||||
err := h.RemoveHosts(host)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return h.AddHosts(ips[0], host)
|
||||
for _, ip := range ips {
|
||||
err := h.AddHosts(ip, host)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *Host) addHosts(comment string, ip string, hosts ...string) error {
|
||||
|
@ -116,6 +116,23 @@ func Test_Hosts(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
err = h.SetHostIPs("ssh.b612.me", "9.9.9.9")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
data = h.ListIPsByHost("ssh.b612.me")
|
||||
if len(data) == 0 {
|
||||
t.Error("Expected 1 got ", data)
|
||||
} else {
|
||||
t.Log(data)
|
||||
}
|
||||
|
||||
err = h.SetIPHosts("10.10.10.10", "ssh.b612.me", "ssr.b612.me")
|
||||
if len(data) == 0 {
|
||||
t.Error("Expected 1 got ", data)
|
||||
}
|
||||
|
||||
err = h.SaveAs("./test_hosts_02.txt")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
|
@ -8,6 +8,7 @@
|
||||
#special IPv6 addre
|
||||
127.0.0.1 localhost
|
||||
127.0.0.1 b612
|
||||
8.8.8.8 ssh.b612.me
|
||||
#special IPv6 addresses
|
||||
::1 localhost ipv6-localhost ipv6-loopback
|
||||
fe00::0 ipv6-localnet
|
||||
|
Loading…
x
Reference in New Issue
Block a user