dig获取并批量路由IP地址到指定网关
💡
原文中文,约700字,阅读约需2分钟。
📝
内容提要
本文介绍了如何使用 dig 命令获取域名的 IP 地址,并通过 ip route add 命令添加路由。以 frytea.com 为例,首先执行 `dig frytea.com +short` 获取 IP 地址,然后添加路由。提供了一个自动化脚本以批量处理多个 IP 地址,执行时需具备 root 权限,并确保目标路由可达。
🎯
关键要点
- 安装 dig 命令:Debian/Ubuntu 使用 apt-get install knot-dnsutils,CentOS/RHEL 使用 dnf install bind-utils。
- 使用 dig 命令获取域名 IP 地址:执行 dig frytea.com +short。
- 添加路由规则:假设获取到的 IP 是 1.2.3.4,运行 ip route add 1.2.3.4/32 via 192.168.25.18。
- 提供自动化脚本:使用 for 循环和 grep 过滤出 IPv4 地址并添加路由。
- 执行添加路由命令需要 root 权限,确保目标路由可达。
➡️