这是我查找服务器 IP 并检查它是否从服务器解析的简单脚本:
read domain_name
echo domain is: $domain_name
def_ip="ip route get 8.8.8.8 | head -1 | cut -d ' ' -f 7"
if ( dig +short @8.8.8.8 $domain_name == $def_ip )
then
echo $domain_name resolves from $def_ip
else
echo $domain_name does not resolve from $def_ip, please check
fi
这是运行后的错误:
google.com
domain is: google.com
setup_libs()
setup_system()
create_search_list()
ndots is 1.
get_server_list()
make_server(8.8.8.8)
make_server(127.0.0.53)
dig_query_setup
parse_args()
making new lookup
make_empty_lookup()
digrc (open)
main parsing +short
main parsing @8.8.8.8
make_server(8.8.8.8)
main parsing google.com
clone_lookup()
make_empty_lookup()
clone_server_list()
make_server(8.8.8.8)
looking up google.com
main parsing ==
clone_lookup()
make_empty_lookup()
clone_server_list()
make_server(8.8.8.8)
looking up ==
main parsing ip
clone_lookup()
make_empty_lookup()
clone_server_list()
make_server(8.8.8.8)
looking up ip
main parsing route
clone_lookup()
make_empty_lookup()
clone_server_list()
make_server(8.8.8.8)
looking up route
main parsing get
clone_lookup()
make_empty_lookup()
clone_server_list()
make_server(8.8.8.8)
looking up get
main parsing 8.8.8.8
clone_lookup()
make_empty_lookup()
clone_server_list()
make_server(8.8.8.8)
looking up |
main parsing head
clone_lookup()
make_empty_lookup()
clone_server_list()
make_server(8.8.8.8)
looking up head
main parsing -1
Invalid option: -1
Usage: dig [@global-server] [domain] [q-type] [q-class] {q-opt}
{global-d-opt} host [@local-server] {local-d-opt}
[ host [@local-server] {local-d-opt} [...]]
Use "dig -h" (or "dig -h | more") for complete list of options
echo google.com does not resolve from ip route get 8.8.8.8 | head -1 | cut -d ' ' -f 7, please check
如果我更改echo $domain_name does not resolve from $def_ip
为eval $domain_name ...
,则错误是google.com
找不到该命令。
我搜索并根据结果,我认为我应该同时使用两者echo
,eval
但没有找到合适的方法来做到这一点。
那正确吗?如果没有,最好的做法是什么?
要捕获命令的输出,您需要 Command Substitution
$( ... )
。要比较两个值,您不能使用
( ... )
,它只是在后台运行命令。使用[ ... ]
或[[ ... ]]
:你注意到我们再次使用了命令替换吗?
使用
[[ ... ]]
, 比较的左侧不需要双引号。右侧可能仍需要它们以防止将其解释为模式。尝试类似的东西