ホストを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/SIGSTOPおよびSIGCONT信号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

意図した対象でなくても何でもエコーを傍受して応答できるので、pingを避けようとします。これが私が使用するものです:

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

関連情報