Faça ping em um host, verifique se está ativo ou não e envie o resultado para o arquivo por meio de um shell script

2

Para limpar meu arquivo hosts personalizado e remover domínios mortos no Windows, faço ping nos hosts de domains.txt e, se obtiver uma resposta, adiciono esse host a result.txt :

@echo off

>result.txt (
    for /f %%i in (domain.txt) do ping -n 1 %%i >nul && echo 127.0.0.1 %%i
)

Alguém pode me ajudar a implementar a mesma funcionalidade através de um script de shell do Linux?

    
por SMed79 19.04.2015 / 02:35

3 respostas

1
O Funceble link é a melhor ferramenta que eu recomendo usar hoje .

$ ./funceble -f domain.txt

Domain                                      Status      Expiration Date   Source     Analyse Date        
------------------------------------------- ----------- ----------------- ---------- --------------------
google.com                                  ACTIVE      14-sep-2020       WHOIS      Tue Sep 25, 06:30:43 
stackexchange.com                           ACTIVE      12-jun-2018       WHOIS      Tue Sep 25, 06:30:44 
zhnixomknxkm.com                            INACTIVE    Unknown           NSLOOKUP   Tue Sep 25, 06:30:45 
kvfjjyrlsphr.info                           INACTIVE    Unknown           NSLOOKUP   Tue Sep 25, 06:30:46 
adblockplus.org                             ACTIVE      09-jun-2018       WHOIS      Tue Sep 25, 06:30:47

Status      Percentage   Numbers
----------- ------------ -------------
ACTIVE      60%          3
INACTIVE    40%          2
INVALID     0%           0
    
por 25.09.2017 / 07:40
1

Se você estiver usando bash , você pode tentar este pequeno script:

#!/bin/bash
while IFS= read -r host; do
    [[ $(ping -c4 "$host") ]] 2>/dev/null && echo "$host" >> /path/to/result.txt
done < /path/to/domains.txt

Aqui, assumi que os hosts em domains.txt são separados por nova linha.

EDITAR:

Aqui está o script modificado com ping sendo detalhado (sumarizado) e assumindo que o diretório /path/to/directory/ contém os arquivos domains-01.txt , domains-02.txt etc.

#!/bin/bash
for file in /path/to/directory/domains-*.txt; do
    while IFS= read -r host; do
        ping -qc4 "$host" && echo "$host" >> /where/to/save/result.txt
    done < "$file"
done

Se você quiser o comportamento de pausa no currículo, consulte SIGTSTP / SIGSTOP e SIGCONT sinais que podem ser emitidos com kill e comandos semelhantes, por exemplo se o PID do processo for 2773, então:

Para suspender: kill -TSTP 2773 , para retomar: kill -CONT 2773 .

    
por 19.04.2015 / 02:45
0

Eu tento evitar o ping, já que qualquer coisa pode interceptar e responder ao eco - mesmo que não seja o alvo pretendido. Aqui está o que eu uso - espero que ajude:

#=====  FUNCTION  =============================================================
#          NAME:  validateServerAvailability
#   DESCRIPTION:  Validates that a given host exists in DNS and is alive
#    PARAMETERS:  Target host, port number (optional)
#       RETURNS:  0 if success, 1 otherwise
#==============================================================================
function validateServerAvailability
{
    trap '[ ! -z "${ENABLE_VERBOSE}" ] && [ "${ENABLE_VERBOSE}" = "${_TRUE}" ] || set +x; [ ! -z "${ENABLE_TRACE}" ] && [ "${ENABLE_TRACE}" = "${_TRUE}" ] && set +v; set -o noclobber' INT TERM EXIT;

    [ ! -z "${ENABLE_VERBOSE}" ] && [ "${ENABLE_VERBOSE}" = "${_TRUE}" ] && set -x || set +x;
    [ ! -z "${ENABLE_TRACE}" ] && [ "${ENABLE_TRACE}" = "${_TRUE}" ] && set -v || set +v;

    set +o noclobber;
    typeset METHOD_NAME="${0}#${FUNCNAME[0]}";
    typeset -i RETURN_CODE=0;

    [ ! -z "${ENABLE_PERFORMANCE}" ] && [ "${ENABLE_PERFORMANCE}" = "${_TRUE}" ] && writeLogEntry "PERFORMANCE" "${METHOD_NAME}" "${0}" "${LINENO}" "${METHOD_NAME} START: $(date +"${TIMESTAMP_OPTS}")";
    [ ! -z "${ENABLE_PERFORMANCE}" ] && [ "${ENABLE_PERFORMANCE}" = "${_TRUE}" ] && typeset -i START_EPOCH=$(date +"%s");

    [ ! -z "${ENABLE_DEBUG}" ] && [ "${ENABLE_DEBUG}" = "${_TRUE}" ] && writeLogEntry "DEBUG" "${METHOD_NAME}" "${0}" "${LINENO}" "${METHOD_NAME} -> enter";
    [ ! -z "${ENABLE_DEBUG}" ] && [ "${ENABLE_DEBUG}" = "${_TRUE}" ] && writeLogEntry "DEBUG" "${METHOD_NAME}" "${0}" "${LINENO}" "Provided arguments: ${*}";

    if [ ${#} -eq 0 ]
    then
        typeset RETURN_CODE=3;

        writeLogEntry "STDERR" "${METHOD_NAME}" "${0}" "${LINENO}" "${METHOD_NAME} - Validate a provided server's availability";
        writeLogEntry "STDERR" "${METHOD_NAME}" "${0}" "${LINENO}" "Usage: ${METHOD_NAME} [ hostname ] [ port ]
                 -> The target host to validate
                 -> The port number to attempt connection on";

        [ ! -z "${ENABLE_DEBUG}" ] && [ "${ENABLE_DEBUG}" = "${_TRUE}" ] && writeLogEntry "DEBUG" "${METHOD_NAME}" "${0}" "${LINENO}" "RETURN_CODE -> ${RETURN_CODE}";
        [ ! -z "${ENABLE_DEBUG}" ] && [ "${ENABLE_DEBUG}" = "${_TRUE}" ] && writeLogEntry "DEBUG" "${METHOD_NAME}" "${0}" "${LINENO}" "${METHOD_NAME} -> exit";

        [ ! -z "${ENABLE_VERBOSE}" ] && [ "${ENABLE_VERBOSE}" = "${_TRUE}" ] && set +x;
        [ ! -z "${ENABLE_TRACE}" ] && [ "${ENABLE_TRACE}" = "${_TRUE}" ] && set +v;

        [ ! -z "${ENABLE_PERFORMANCE}" ] && [ "${ENABLE_PERFORMANCE}" = "${_TRUE}" ] && typeset -i END_EPOCH=$(date +"%s");
        [ ! -z "${ENABLE_PERFORMANCE}" ] && [ "${ENABLE_PERFORMANCE}" = "${_TRUE}" ] && typeset -i RUNTIME=$(( START_EPOCH - END_EPOCH ));
        [ ! -z "${ENABLE_PERFORMANCE}" ] && [ "${ENABLE_PERFORMANCE}" = "${_TRUE}" ] && writeLogEntry "PERFORMANCE" "${METHOD_NAME}" "${0}" "${LINENO}" "${METHOD_NAME} TOTAL RUNTIME: $(( RUNTIME / 60 )) MINUTES, TOTAL ELAPSED: $(( RUNTIME % 60 )) SECONDS";
        [ ! -z "${ENABLE_PERFORMANCE}" ] && [ "${ENABLE_PERFORMANCE}" = "${_TRUE}" ] && writeLogEntry "PERFORMANCE" "${METHOD_NAME}" "${0}" "${LINENO}" "${METHOD_NAME} END: $(date +"${TIMESTAMP_OPTS}")";

        [ ! -z "${ERROR_COUNT}" ] && typeset -i ERROR_COUNT=0;
        [ ! -z "${SEARCH_ENTRY}" ] && unset -v SEARCH_ENTRY;
        [ ! -z "${VALIDATE_HOSTNAME}" ] && unset -v VALIDATE_HOSTNAME;
        [ ! -z "${VALIDATE_PORT}" ] && unset -v VALIDATE_PORT;
        [ ! -z "${METHOD_NAME}" ] && unset -v METHOD_NAME;

        return ${RETURN_CODE};
    fi

    typeset VALIDATE_HOSTNAME="${1}";
    [ ${#} -eq 2 ] && typeset -i VALIDATE_PORT=${2};

    [ ! -z "${ENABLE_DEBUG}" ] && [ "${ENABLE_DEBUG}" = "${_TRUE}" ] && writeLogEntry "DEBUG" "${METHOD_NAME}" "${0}" "${LINENO}" "VALIDATE_HOSTNAME -> ${VALIDATE_HOSTNAME}";
    [ ! -z "${ENABLE_DEBUG}" ] && [ "${ENABLE_DEBUG}" = "${_TRUE}" ] && writeLogEntry "DEBUG" "${METHOD_NAME}" "${0}" "${LINENO}" "VALIDATE_PORT -> ${VALIDATE_PORT}";

    [ ! -z "${ENABLE_DEBUG}" ] && [ "${ENABLE_DEBUG}" = "${_TRUE}" ] && writeLogEntry "DEBUG" "${METHOD_NAME}" "${0}" "${LINENO}" "RUN COMMAND -> /usr/bin/env host \"${VALIDATE_HOSTNAME}\" > /dev/null 2>&1;";

    case $(/usr/bin/env host "${VALIDATE_HOSTNAME}" > /dev/null 2>&1; echo ${?};) in
        0)
            case ${#} in
                1)
                    case "$(uname)" in
                        [Ss][Oo][Ll][Aa][Rr][Ii][Ss])
                            [ ! -z "${ENABLE_DEBUG}" ] && [ "${ENABLE_DEBUG}" = "${_TRUE}" ] && writeLogEntry "DEBUG" "${METHOD_NAME}" "${0}" "${LINENO}" "RUN COMMAND -> /usr/bin/env ping \"${VALIDATE_HOSTNAME}\" 1 > /dev/null 2>&1";

                            /usr/bin/env ping "${VALIDATE_HOSTNAME}" 1 > /dev/null 2>&1;
                            ;;
                        *)
                            [ ! -z "${ENABLE_DEBUG}" ] && [ "${ENABLE_DEBUG}" = "${_TRUE}" ] && writeLogEntry "DEBUG" "${METHOD_NAME}" "${0}" "${LINENO}" "RUN COMMAND -> /usr/bin/env ping -c 1 -q -W ${THREAD_TIMEOUT} \"${VALIDATE_HOSTNAME}\" > /dev/null 2>&1";

                            /usr/bin/env ping -c 1 -q -W ${THREAD_TIMEOUT} "${VALIDATE_HOSTNAME}" > /dev/null 2>&1;
                            ;;
                    esac
                    ;;
                2)
                    if [ ! -z "$(type nc 2>/dev/null)" ]
                    then
                        [ ! -z "${ENABLE_DEBUG}" ] && [ "${ENABLE_DEBUG}" = "${_TRUE}" ] && writeLogEntry "DEBUG" "${METHOD_NAME}" "${0}" "${LINENO}" "RUN COMMAND -> /usr/bin/env nc -w 10 ${VALIDATE_HOSTNAME} ${VALIDATE_PORT} < /dev/null";

                        /usr/bin/env nc -w 10 ${VALIDATE_HOSTNAME} ${VALIDATE_PORT} < /dev/null > /dev/null 2>&1;
                    else
                        [ ! -z "${ENABLE_DEBUG}" ] && [ "${ENABLE_DEBUG}" = "${_TRUE}" ] && writeLogEntry "DEBUG" "${METHOD_NAME}" "${0}" "${LINENO}" "RUN COMMAND -> printf \"quit\n\" | /usr/bin/env telnet \"${VALIDATE_HOSTNAME}\" ${VALIDATE_PORT} >/dev/null 2>&1";

                        printf "quit\n" | /usr/bin/env telnet "${VALIDATE_HOSTNAME}" ${VALIDATE_PORT} >/dev/null 2>&1;
                    fi
                    ;;
            esac
            ;;
        *)
            ## lets do a few things here. dig/host don't use /etc/resolv.conf
            ## so we have to kind of drag it in here.
            for SEARCH_ENTRY in $(/usr/bin/env grep search /etc/resolv.conf)
            do
                [ ! -z "${ENABLE_DEBUG}" ] && [ "${ENABLE_DEBUG}" = "${_TRUE}" ] && writeLogEntry "DEBUG" "${METHOD_NAME}" "${0}" "${LINENO}" "SEARCH_ENTRY -> ${SEARCH_ENTRY}";

                [ "${SEARCH_ENTRY}" == "search" ] && continue;

                case $(/usr/bin/env host "${VALIDATE_HOSTNAME}.${SEARCH_ENTRY}" > /dev/null 2>&1; echo ${?};) in
                    0)
                        ## make sure to fix the error counter
                        [ ${ERROR_COUNT} -ne 0 ] && unset -v ERROR_COUNT;
                        typeset -i ERROR_COUNT=0;

                        case ${#} in
                            1)
                                case "$(uname)" in
                                    [Ss][Oo][Ll][Aa][Rr][Ii][Ss])
                                        [ ! -z "${ENABLE_DEBUG}" ] && [ "${ENABLE_DEBUG}" = "${_TRUE}" ] && writeLogEntry "DEBUG" "${METHOD_NAME}" "${0}" "${LINENO}" "RUN COMMAND -> /usr/bin/env ping \"${VALIDATE_HOSTNAME}\" 1 > /dev/null 2>&1";

                                        /usr/bin/env ping "${VALIDATE_HOSTNAME}" 1 > /dev/null 2>&1;

                                        break;
                                        ;;
                                    *)
                                        [ ! -z "${ENABLE_DEBUG}" ] && [ "${ENABLE_DEBUG}" = "${_TRUE}" ] && writeLogEntry "DEBUG" "${METHOD_NAME}" "${0}" "${LINENO}" "RUN COMMAND -> /usr/bin/env ping -c 1 -q -W ${THREAD_TIMEOUT} \"${VALIDATE_HOSTNAME}\" > /dev/null 2>&1";

                                        /usr/bin/env ping -c 1 -q -W ${THREAD_TIMEOUT} "${VALIDATE_HOSTNAME}" > /dev/null 2>&1;

                                        break;
                                        ;;
                                esac
                                ;;
                            2)
                                [ ! -z "${ENABLE_DEBUG}" ] && [ "${ENABLE_DEBUG}" = "${_TRUE}" ] && writeLogEntry "DEBUG" "${METHOD_NAME}" "${0}" "${LINENO}" "RUN COMMAND -> printf \"quit\n\" | /usr/bin/env telnet \"${VALIDATE_HOSTNAME}\" ${VALIDATE_PORT} >/dev/null 2>&1";

                                printf "quit\n" | /usr/bin/env telnet "${VALIDATE_HOSTNAME}" ${VALIDATE_PORT} >/dev/null 2>&1;

                                break;
                                ;;
                        esac
                        ;;
                    *)
                        (( ERROR_COUNT += 1 ));

                        continue;
                esac

                if [ ${ERROR_COUNT} -ne 0 ]
                then
                    typeset RETURN_CODE=1;

                    writeLogEntry "ERROR" "${METHOD_NAME}" "${0}" "${LINENO}" "${VALIDATE_HOSTNAME} does not appear to exist in DNS. Unable to determine availability.";
                    writeLogEntry "STDERR" "${METHOD_NAME}" "${0}" "${LINENO}" "${VALIDATE_HOSTNAME} does not appear to exist in DNS. Unable to determine availability.";

                    [ ! -z "${ENABLE_DEBUG}" ] && [ "${ENABLE_DEBUG}" = "${_TRUE}" ] && writeLogEntry "DEBUG" "${METHOD_NAME}" "${0}" "${LINENO}" "RETURN_CODE -> ${RETURN_CODE}";
                    [ ! -z "${ENABLE_DEBUG}" ] && [ "${ENABLE_DEBUG}" = "${_TRUE}" ] && writeLogEntry "DEBUG" "${METHOD_NAME}" "${0}" "${LINENO}" "${METHOD_NAME} -> exit";

                    [ ! -z "${ENABLE_VERBOSE}" ] && [ "${ENABLE_VERBOSE}" = "${_TRUE}" ] && set +x;
                    [ ! -z "${ENABLE_TRACE}" ] && [ "${ENABLE_TRACE}" = "${_TRUE}" ] && set +v;

                    [ ! -z "${ENABLE_PERFORMANCE}" ] && [ "${ENABLE_PERFORMANCE}" = "${_TRUE}" ] && typeset -i END_EPOCH=$(date +"%s");
                    [ ! -z "${ENABLE_PERFORMANCE}" ] && [ "${ENABLE_PERFORMANCE}" = "${_TRUE}" ] && typeset -i RUNTIME=$(( START_EPOCH - END_EPOCH ));
                    [ ! -z "${ENABLE_PERFORMANCE}" ] && [ "${ENABLE_PERFORMANCE}" = "${_TRUE}" ] && writeLogEntry "PERFORMANCE" "${METHOD_NAME}" "${0}" "${LINENO}" "${METHOD_NAME} TOTAL RUNTIME: $(( RUNTIME / 60 )) MINUTES, TOTAL ELAPSED: $(( RUNTIME % 60 )) SECONDS";
                    [ ! -z "${ENABLE_PERFORMANCE}" ] && [ "${ENABLE_PERFORMANCE}" = "${_TRUE}" ] && writeLogEntry "PERFORMANCE" "${METHOD_NAME}" "${0}" "${LINENO}" "${METHOD_NAME} END: $(date +"${TIMESTAMP_OPTS}")";

                    [ ! -z "${ERROR_COUNT}" ] && typeset -i ERROR_COUNT=0;
                    [ ! -z "${SEARCH_ENTRY}" ] && unset -v SEARCH_ENTRY;
                    [ ! -z "${VALIDATE_HOSTNAME}" ] && unset -v VALIDATE_HOSTNAME;
                    [ ! -z "${VALIDATE_PORT}" ] && unset -v VALIDATE_PORT;
                    [ ! -z "${METHOD_NAME}" ] && unset -v METHOD_NAME;

                    return ${RETURN_CODE};
                fi
            done
            ;;
    esac

    RETURN_CODE=${?};

    [ ! -z "${ENABLE_DEBUG}" ] && [ "${ENABLE_DEBUG}" = "${_TRUE}" ] && writeLogEntry "DEBUG" "${METHOD_NAME}" "${0}" "${LINENO}" "RETURN_CODE -> ${RETURN_CODE}";
    [ ! -z "${ENABLE_DEBUG}" ] && [ "${ENABLE_DEBUG}" = "${_TRUE}" ] && writeLogEntry "DEBUG" "${METHOD_NAME}" "${0}" "${LINENO}" "${METHOD_NAME} -> exit";

    [ ! -z "${ENABLE_VERBOSE}" ] && [ "${ENABLE_VERBOSE}" = "${_TRUE}" ] && set +x;
    [ ! -z "${ENABLE_TRACE}" ] && [ "${ENABLE_TRACE}" = "${_TRUE}" ] && set +v;

    [ ! -z "${ENABLE_PERFORMANCE}" ] && [ "${ENABLE_PERFORMANCE}" = "${_TRUE}" ] && typeset -i END_EPOCH=$(date +"%s");
    [ ! -z "${ENABLE_PERFORMANCE}" ] && [ "${ENABLE_PERFORMANCE}" = "${_TRUE}" ] && typeset -i RUNTIME=$(( START_EPOCH - END_EPOCH ));
    [ ! -z "${ENABLE_PERFORMANCE}" ] && [ "${ENABLE_PERFORMANCE}" = "${_TRUE}" ] && writeLogEntry "PERFORMANCE" "${METHOD_NAME}" "${0}" "${LINENO}" "${METHOD_NAME} TOTAL RUNTIME: $(( RUNTIME / 60 )) MINUTES, TOTAL ELAPSED: $(( RUNTIME % 60 )) SECONDS";
    [ ! -z "${ENABLE_PERFORMANCE}" ] && [ "${ENABLE_PERFORMANCE}" = "${_TRUE}" ] && writeLogEntry "PERFORMANCE" "${METHOD_NAME}" "${0}" "${LINENO}" "${METHOD_NAME} END: $(date +"${TIMESTAMP_OPTS}")";

    [ ! -z "${ERROR_COUNT}" ] && typeset -i ERROR_COUNT=0;
    [ ! -z "${SEARCH_ENTRY}" ] && unset -v SEARCH_ENTRY;
    [ ! -z "${VALIDATE_HOSTNAME}" ] && unset -v VALIDATE_HOSTNAME;
    [ ! -z "${VALIDATE_PORT}" ] && unset -v VALIDATE_PORT;
    [ ! -z "${METHOD_NAME}" ] && unset -v METHOD_NAME;

    return ${RETURN_CODE};
}

link

    
por 19.04.2015 / 05:09