鄙人不才,又写了个鸡肋的轮子,既然写了,还是介绍下吧

工作原理:获取所有Docker容器的IP地址,添加到 hosts 文件

我丰富的经验告诉我 Go 的 Docker 库很成熟了,So

1
import "github.com/docker/docker/client"

先连接 Docker 服务端

1
Cli, err = client.NewClientWithOpts(client.FromEnv)

列出所有的 Docker 容器

1
list, err := Cli.ContainerList(context.Background(), types.ContainerListOptions{})

待会拿到了IP和名字怎么处理呢?[]string? map[string]string ? []struct{}?

For 循环 获取容器名称和IP

1
2
3
4
5
6
7
8
9
for i := 0; i < len(list); i++ {
name := list[i].Names[len(list[i].Names)-1], "/", "", -1)
for _, v := range list[i].NetworkSettings.Networks {
if v.IPAddress != "" {
hostNow[name] = v.IPAddress
}
break // 只记录第一个IP
}
}

接着就是复杂的条件语句,怎么处理 历史记录和当前记录,以及各种性能调优,先放一图,见笑!

截屏2022-09-27 04.14.22

介绍一下实际用途:

截屏2022-09-27 04.27.44

截屏2022-09-27 04.28.19

截屏2022-09-27 04.33.33

截屏2022-09-27 04.39.17

截屏2022-09-27 04.25.32

Github: https://github.com/XRSec/AUDCH/

最近赞少的可怜,点个赞吧😂

XRSec has the right to modify and interpret this article. If you want to reprint or disseminate this article, you must ensure the integrity of this article, including all contents such as copyright notice. Without the permission of the author, the content of this article shall not be modified or increased or decreased arbitrarily, and it shall not be used for commercial purposes in any way