我想知道我的代码出了什么问题?我想念什么?
编辑:经过2天的实验。我无处可去。我在下面尝试了这些答案,很遗憾,它们没有用,但是由于这些注释,我缩短了代码并清理了它。我想知道这次如何使其正确吗?这项练习非常辛苦,我似乎无法掌握它的含义:(。我不知道自己在做错什么。
#!/bin/bash
m1="has IPv6 address"
m2="has address"
m3="mail is handled by 0 ."
m4="found IPv6"
m5="Didn't find IPv6"
m6="Didn't find mail server"
###########################################################################################################################
host `host www.ee | sort | grep "mail is handled" | head -1 | awk '{print $7}'` >> www.all.txt
host `host www.eu | sort | grep "mail is handled" | head -1 | awk '{print $7}'` >> www.all.txt
host `host www.com | sort | grep "mail is handled" | head -1 | awk '{print $7}'` >> www.all.txt
file="www.all.txt"
while read line
do
if grep -q "$m1" $file
then
awk 'NR==1 {print $1}'
echo "${m4}"
elif grep -q "$m2" $file
then
awk 'NR==1 {print $1}'
echo "${m5}"
else
echo "${m6}"
echo "${m5}"
fi
done < $file
它创建的文件:
kristen@kristen-virtual-machine:~/Desktop$ ./nimed.sh
aspmx.l.google.com
found IPv6
kristen@kristen-virtual-machine:~/Desktop$ ls
koopia.sh nimed.sh TEST.sh www.all.txt
kristen@kristen-virtual-machine:~/Desktop$ cat www.all.txt
aspmx.l.google.com has address 64.233.161.26
aspmx.l.google.com has IPv6 address 2a00:1450:4010:c0e::1b
mail.www.eu has address 46.105.44.68
ASPMX.L.GOOGLE.com has address 64.233.161.26
ASPMX.L.GOOGLE.com has IPv6 address 2a00:1450:4010:c0e::1b
kristen@kristen-virtual-machine:~/Desktop$
测试1:成功
Correct program output
--- Input ---
www.ee
--- Program output ---
aspmx.l.google.com
found IPv6
--- Expected output (text)---
aspmx.l.google.com
found IPv6
测试2:失败
Incorrect program output
--- Input ---
www.eu
--- Program output ---
aspmx.l.google.com
Found IPv6
--- Expected output (text)---
mail.www.eu
Didn't find IPv6
答案 0 :(得分:0)
您是否要查找给定主机是否具有IPv6邮件服务器?您做错了的一件事是从第一个主机命令获取第一行输出。如果要查找邮件服务器,则必须找到其中包含“邮件已处理”的行。
$ host www.ee
www.ee has address 194.106.111.42 <--- need to ignore this?
www.ee mail is handled by 30 aspmx3.googlemail.com.
www.ee mail is handled by 30 aspmx2.googlemail.com.
www.ee mail is handled by 20 alt2.aspmx.l.google.com.
www.ee mail is handled by 10 aspmx.l.google.com.
www.ee mail is handled by 20 alt1.aspmx.l.google.com.
www.ee mail is handled by 30 aspmx4.googlemail.com.
www.ee mail is handled by 30 aspmx5.googlemail.com.
像这样吗?
$ host `host www.ee | grep "mail is handled" | head -1 | awk '{print $7}'`
aspmx3.googlemail.com has address 173.194.219.27
aspmx3.googlemail.com has IPv6 address 2607:f8b0:4002:c03::1a
$ host `host www.eu | grep "mail is handled" | head -1 | awk '{print $7}'`
mail.www.eu has address 46.105.44.68
$ host `host www.com | grep "mail is handled" | head -1 | awk '{print $7}'`
ALT1.ASPMX.L.GOOGLE.com has address 74.125.129.26
ALT1.ASPMX.L.GOOGLE.com has IPv6 address 2607:f8b0:4001:c15::1b
答案 1 :(得分:0)
虽然我认为@IanMcGowan的答案很明确,但似乎您想:
host
设置URL并获取dns服务器host
,以查看它们是否具有与其关联的IPv6地址这可以在一行中完成:
host="www.ee"; host $host | awk '$7{print $7}' | xargs -I {} host {} | awk -v host=$host '/IPv6/{printf "%s\n%s\n", host,"Found IPv6"; exit 0}'
话虽这么说...我不知道你的if/elif
是干什么的。实际上,www.ee确实具有与其关联的IPv6地址(如您从上面的输出中看到的),因此您的elif
永远不会触发。目前尚不清楚您在脚本的此阶段要做什么。