호스트를 ping하고 존재하는지 확인한 다음 쉘 스크립트를 통해 결과를 파일로 보냅니다.

호스트를 ping하고 존재하는지 확인한 다음 쉘 스크립트를 통해 결과를 파일로 보냅니다.

Windows에서 사용자 정의 파일을 정리 hosts하고 데드존을 제거하기 위해 호스트에 ping을 보내고 domains.txt응답을 받으면 호스트를 다음에 추가합니다 result.txt.

@echo off

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

누구든지 Linux 쉘 스크립트를 통해 동일한 기능을 달성하도록 도와줄 수 있습니까?

답변1

을 사용하는 경우 bash다음과 같은 짧은 스크립트를 사용해 볼 수 있습니다.

#!/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

여기서는 호스트가 개행 문자로 구분되어 있다고 가정합니다 domains.txt.

편집하다:

다음은 수정된 스크립트이며, ping자세한 내용(요약)이며 디렉터리에 /path/to/directory/파일 domains-01.txt등이 domains-02.txt포함되어 있다고 가정합니다.

#!/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

일시 중지-재개 동작을 원하면 다음을 확인하세요.SIGTSTP/ SIGSTOPSIGCONT신호kill예를 들어 프로세스의 PID가 2773인 경우 유사한 명령을 실행할 수 있습니다 .

일시중지: kill -TSTP 2773, 재개: kill -CONT 2773.

답변2

폰 세이블 https://github.com/funilrys/funcble~이다최고의 도구오늘은 꼭 사용하시길 추천드립니다.

$ ./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

답변3

의도한 대상이 아니더라도 무엇이든 에코를 가로채서 응답할 수 있기 때문에 핑을 피하려고 합니다. 이것이 내가 사용하는 것입니다:

#=====  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};
}

https://github.com/cws-khuntly/dotfiles/blob/master/functions.d/F09-network

관련 정보