O que estou fazendo errado neste script bash?

0

Gostaria de saber o que há de errado com o meu código? o que estou perdendo? Por que a instrução IF não está funcionando corretamente? Por que mostra apenas uma saída? Por que a instrução elif não está funcionando? Estou tentando hospedar 3 servidores, www.ee, www.eu e www.com e, em seguida, hospedá-los novamente para determinar se eles têm endereços IPv6 ou servidor de e-mail.

EDIT: Deveria funcionar assim:

1) Crie um script chamado nimed.sh

2) Na entrada, o host executa .ee; .eu; anfitriões .com.

3) Se o host não existir, imprima "Host not found"

4) O endereço do host exibirá primeiro o endereço do servidor de e-mail correspondente encontrado. %código%. Deve remover pontos no final da frase do host. Tipo, mostra assim:? .Com. (removendo o ponto =? .com)

5) Se o servidor de e-mail não foi encontrado, imprima (Use commands sort, awk, tail or head)

6) Se o servidor de e-mail foi encontrado, pergunte ao endereço IP do servidor de e-mail com o comando host.

7) Se o servidor de e-mail tiver o endereço IPv6, imprima "mail server not found" , senão, em seguida, imprima "found IPv6"

Isso é o que eu preciso fazer e estou preso na parte 7. É um exercício escolar para aprender bash fazendo exercícios. É opcional, não obrigatório.

#!/bin/bash

m1="has address"
m2="has IPv6 address"
m3="mail is handled by 0 ."
m4="found IPv6"
m5="IPv6 not found"
m6="mail server not found"
###########################################################################################################################
host "$(host www.ee | sort | grep "mail is handled" | head -1 | awk '{print $7}')" >> www.all.txt
#first line shows this to www.all.txt
#aspmx.l.google.com has address 108.177.14.26
#aspmx.l.google.com has IPv6 address 2a00:1450:4010:c03::1b
host "$(host www.eu | sort | grep "mail is handled" | head -1 | awk '{print $7}')" >> www.all.txt
#second line shows this to www.all.txt
#mail.www.eu has address 46.105.44.68
host "$(host www.com | sort | grep "mail is handled" | head -1 | awk '{print $7}')" >> www.all.txt
#third line shows this to www.all.txt
#ASPMX.L.GOOGLE.com has address 108.177.14.26
#ASPMX.L.GOOGLE.com has IPv6 address 2a00:1450:4010:c03::1b
file="www.all.txt" #and this file has this in total:
#aspmx.l.google.com has address 108.177.14.26
#aspmx.l.google.com has IPv6 address 2a00:1450:4010:c03::1b
#mail.www.eu has address 46.105.44.68
#ASPMX.L.GOOGLE.com has address 108.177.14.26
#ASPMX.L.GOOGLE.com has IPv6 address 2a00:1450:4010:c03::1b
while read line #now this is where it gets messy. I don't know what to do-
#with line variable
do
    if grep -q "$m2" $file #if string 'has IPv6 address' is in www.all.txt
    then
        awk 'NR==1 {print $1}' #go to line 1 and print the first text
# aspmx.l.google.com
        echo "${m4}" #print 'Found IPv6'
    elif grep -q "$m1" $file; then #else if string 'has address' is in
# www.all.txt then
        awk 'NR==2 {print $1}' #go to line 2 and print the first text
        echo "${m5}" #print 'IPv6 not found'
    elif grep -q "$m3" $file; then #if string 'mail is handled by 0 .'
#is in www.all.txt then
        echo "${m6}" #print 'mail server does not exist'
    else #if none of the above was correct then
        echo "${m6}" #mail server does not exist and
        echo "${m5}" #IPv6 not found
    fi #end the if-elif-else statement
done < $file #end the while loop

Arquivo criado:

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$

TESTE 1: Sucesso

Correct program output
--- Input ---

 www.ee


--- Program output ---

aspmx.l.google.com
found IPv6


--- Expected output (text)---

aspmx.l.google.com
found IPv6

TESTE 2: falha

Incorrect program output
--- Input ---

 www.eu


--- Program output ---

aspmx.l.google.com
Found IPv6


--- Expected output (text)---

mail.www.eu
Didn't find IPv6
    
por Kristen Ungur 11.10.2018 / 21:32

1 resposta

0

Por um tempo, eu consegui trabalhar. Eu cometi um grave erro e não li o texto corretamente. Bem, foi um exercício fácil quando descobri que precisava usar read : D.

Esta é a resposta para minha pergunta:

#!/bin/bash

m1="has address"
m2="has IPv6 address"
m3="mail is handled by 0 ."
m4="on IPv6"
m5="ei ole IPv6"
m6="mailiserverit pole"
m7="NXDOMAIN"
f="www.all.txt"
read -p "Sisesta host: " hostname
host $hostname | sort > $f
if grep -q "$m3" $f; then
        echo "$m6"
        echo "$m5"
elif grep -q "$m2" $f; then
        awk 'NR==3 {print $7}' $f
        echo "$m4"
elif host "$(host $hostname | sed 's/\.$//' | tail -1 | awk '{print $7}')" > www.all.txt && grep -q "mx-eu.mail.am0.yahoodns.net" $f; then
        awk 'NR==1 {print $1}' $f
        echo "$m5"
elif grep -q "$m7" $f; then
        echo "hosti pole"
else
        host "$(host $hostname | sed 's/\.$//' | sort | awk 'NR==2 {print $7}')" > $f
                if grep -q "$m2" $f; then
                        awk 'NR==1 {print $1}' $f
                        echo "$m4"
                elif grep -q "$m1" $f; then
                        awk 'NR==1 {print $1}' $f
                        echo "$m5"
fi
        fi
    
por 17.10.2018 / 17:44

Tags