update host file

master^2
兔子 9 months ago
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 { func (h *Host) SetHostIPs(host string, ips ...string) error {
info := h.ListByHost(host) if len(ips) == 0 {
if len(info) == 0 { return fmt.Errorf("no ip address")
return h.AddHosts(host, ips...)
} else if len(info) == 1 {
info[0].ip = ips[0]
info[0].comment = ""
return nil
} }
err := h.RemoveHosts(host) err := h.RemoveHosts(host)
if err != nil { if err != nil {
return err 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 { 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") err = h.SaveAs("./test_hosts_02.txt")
if err != nil { if err != nil {
t.Error(err) t.Error(err)

@ -8,6 +8,7 @@
#special IPv6 addre #special IPv6 addre
127.0.0.1 localhost 127.0.0.1 localhost
127.0.0.1 b612 127.0.0.1 b612
8.8.8.8 ssh.b612.me
#special IPv6 addresses #special IPv6 addresses
::1 localhost ipv6-localhost ipv6-loopback ::1 localhost ipv6-localhost ipv6-loopback
fe00::0 ipv6-localnet fe00::0 ipv6-localnet

Loading…
Cancel
Save