返されたIPアドレスnslookup

返されたIPアドレスnslookup

私はネットワークセキュリティを勉強している新入生です。侵入テスト段階では、kali linuxを使用します。私も初めてbashスクリプトに触れました。

注文する:

nslookup -type=mx cathay.com.sg

出力例

Server:     192.168.48.2
Address:    192.168.48.2#53

Non-authoritative answer:
cathay.com.sg   mail exchanger = 0 cathay-com-sg.mail.protection.outlook.com.

Authoritative answers can be found from:

私はそれを見せたいだけです:

192.168.48.2
192.168.48.2#53

しかし、私の現在のスクリプトは

#!/bin/bash
#nslookup but display only IP
nslookup -type=mx cathay.com.sg
|awk -F":" '{print $2}'

これにより、次のエラーが発生します。

./nslookupscr.sh: line 4: syntax error near unexpected token `|'
./nslookupscr.sh: line 4: `|awk -F":" '{print $2}''

代わりにdigを使うという話を聞きましたが、まだこれについて議論したことはありません。私たちはフォーマットのためにawkとgrepだけを見たので、何が問題なのかわかりません。

ベストアンサー1

返されたIPアドレスnslookup

あなたのリクエストを読んでみると表示したいようです。MXドメインのIPアドレス。

ただし、nslookup最初の行には名前とIPアドレスのみが報告されます。あなたの設定されたDNS:

nslookup -type=mx cathay.com.sg | cat -n
     1      Server:         192.168.1.1
     2      Address:        192.168.1.1#53
     3
     4      Non-authoritative answer:
     5      cathay.com.sg   mail exchanger = 0 cathay-com-sg.mail.protection.outlook.com.
     6
     7      Authoritative answers can be found from:
     8      cathay.com.sg   nameserver = ns2.dreamhost.com.
     9      cathay.com.sg   nameserver = ns3.dreamhost.com.
    10      cathay.com.sg   nameserver = ns1.dreamhost.com.
    11      ns1.dreamhost.com       internet address = 64.90.62.230
    12      ns2.dreamhost.com       internet address = 208.97.182.10
    13      ns3.dreamhost.com       internet address = 66.33.205.230

実際の回答は4行目から始まり、MX(優先順位= 0)にname:があることを示す1行だけが含まれていますcathay-com-sg.mail.protection.outlook.com

そこで(少なくとも)2番目の質問をする必要があります。

nslookup cathay-com-sg.mail.protection.outlook.com |cat -n
     1      Server:         192.168.1.1
     2      Address:        192.168.1.1#53
     3
     4      Non-authoritative answer:
     5      Name:   cathay-com-sg.mail.protection.outlook.com
     6      Address: 104.47.124.36
     7      Name:   cathay-com-sg.mail.protection.outlook.com
     8      Address: 104.47.126.36

繰り返しますが、答えは4行の後ろから始まり、2つAddress:

USG dig, ダウン、単に次のことができます。

mapfile -t mxnames < <(dig +noall +answer mx cathay.com.sg)
mxnames=("${mxnames[@]##*[ $'\t']}")
mapfile -t mxips < <(dig +noall +answer ${mxnames[@]})
mxips=("${mxips[@]##*[ $'\t']}")

それから

declare -p mxips mxnames

次の内容が返されることがあります。

declare -a mxips=([0]="67.205.10.249" [1]="77.32.207.225" [2]="104.47.125.36" [3]="104.47.126.36")
declare -a mxnames=([0]="cathay.com.sg." [1]="cathay-com-sg.mail.protection.outlook.com.")

使用できる

for ip in ${mxips[@]};do
    ping -{c,W}1 $ip &>/dev/null && echo $ip ok || echo $ip -- &
        sleep .02
  done |
    cat
77.32.207.225 ok
67.205.10.249 ok
104.47.125.36 --
104.47.126.36 --

いくつかの説明:

  • 構文はdig +noall +answer <QUERY>1行の答えのみを返します。たとえば、次のようになります。

     dig +noall +answer mx cathay.com.sg
     cathay.com.sg.    12345   IN   MX   0 cathay-com-sg.mail.protection.outlook.com.
    
     dig +noall +answer $mxname
     cathay-com-sg.mail.protection.outlook.com. 10 IN A 104.47.125.36
     cathay-com-sg.mail.protection.outlook.com. 10 IN A 104.47.126.36
    

man dig詳細については確認してください。

  • 文法${var##* }と呼ばれますパラメータ拡張、左から最後まで文字列を削除します。スペース

     mxname=${mxname##* }
    

    dig答えの左側の部分を削除してcathay-com-sg.mail.protection.outlook.com.保存します$mxname

     mxips+=(${line##* })
    

    前の回答の最後の部分を追加します。$mxips 大量に

ぜひ見てman bash検索してみてくださいパラメータ拡張
man -P'less +"/^ *Parameter Expansion"' bash

またはを使用しますが、nslookup2つのクエリを一度だけ実行してください。

nslookup走り続けることができるのでインタラクティブパターンを使用して複数のクエリに応答できるように1を使用する機能があります。クロスnslookupドメイン内のすべてのIPアドレスを一覧表示するのに必要なだけ多くのクエリを要求してください。

mxIps ()  { 
    local target=$1 line foo;
    local -a mxnames=() mxips=();
    mkfifo fifo-nsl;                 # Fifo for binding nslookup output
    exec 7> >(stdbuf -i0 -o0 nslookup >fifo-nsl);
    exec 6< fifo-nsl;
    rm fifo-nsl;                     # As FD 6 and 7 are open, we could remove them
    printf "set type=mx\n%s\n" $target 1>&7;  # 1st query
    read -u 6 line;                           # wait for answer and drop 1st line
    while read -t .002 -u 6 line; do
        [ "$line" ] && [ -z "${line%%$target*mail exchanger*}" ] &&
            mxnames+=(${line##* });           # Add to mxnames array
    done;
    printf 'set type=A\n' 1>&7;               # Set -type=A
    for mxname in ${mxnames[@]%.};
    do
        echo $mxname 1>&7;                    # Next query
        read -u 6 line;                       # Wait for answer and
        read -u 6 line;                       # drops two first lines
        while read -t .002 -u 6 line; do
            [ "$line" ] && [ -z "${line%%Address:*}" ] &&
                mxips+=(${line##* });         # Add to mxips array
        done;
    done;
    echo exit 1>&7;                           # End nslookup sub process
    exec 6>&-;                                # Close FD 6, then 7
    exec 7>&-;
    declare -p mxips                          # Dump result
}

それから

mxIps cathay.com.sg
declare -a mxips=([0]="104.47.125.36" [1]="104.47.126.36")

おすすめ記事