LoginSignup
0
0

More than 3 years have passed since last update.

[Windows10]pingでIPアドレスを総当たりするバッチを作成する

Posted at

概要

本書では自身PCのネットワーク上に存在するIPアドレスを特定するため、pingコマンドを利用してIPアドレスを総当たりするバッチファイルを作成します。

動作の流れ

  1. IPアドレス(24bit(xxx.xxx.xxx)まで)を入力する。
  2. 正しくIPアドレスが入力されているか確認する。
  3. xxx.xxx.xxx.0 から xxx.xxx.xxx.255 までpingを打つ。
  4. pingでヒットしたIPアドレスを表示する。(ARPテーブルを表示する)

バッチファイルの内容

@echo off

rem ping_brute_force(Windows10)
rem Copyright (c) 2020 yuichi1992_west
rem This software is released under the MIT License.
rem http://opensource.org/licenses/mit-license.php

rem Variable initialization.
set ping_survey=""

rem enter ip address.
echo Enter the ip address up to 24bit. ( Example: 192.168.100 )
echo Important!: If the target bit is 2 digits or less, enter 0 at the beginning. ( Example: 172.016.002 )
set /P ping_survey="After entering, press Enter.  "

rem show ip address.
echo The IP address you entered is %ping_survey%

rem Determine the number of characters by regular expression.
echo %ping_survey% | findstr /r "\<[0-9][0-9][0-9][.][0-9][0-9][0-9][.][0-9][0-9][0-9]\>" >nul 2>&1

rem Output the judgment result of regular expression.
if errorlevel 1 (
    echo.
    echo Important!
    echo Check the input format again and execute the bat file again. 
    echo Especially when the target bit is 2 digits or less, be sure to add 0 at the beginning. 
    echo Example: 172.016.005
    pause
    exit
) else if errorlevel 0 (
    echo.
    echo The ip range is correct. Ping all IP addresses.
    echo.

    rem The IP address is brute-forced with the entered settings.
    for /l %%i in (0,1,255) do ping -w 1 -n 1 %ping_survey%.%%i && arp -a %ping_survey%.%%i

    rem Display ARP table
    echo .
    echo Display ARP table
    arp -a
    echo.

    pause
)

バッチファイルの実行例

成功例

Enter the ip address up to 24bit. ( Example: 192.168.100 )
Important!: If the target bit is 2 digits or less, enter 0 at the beginning. ( Example: 172.016.002 )
After entering, press Enter.  192.168.000
The IP address you entered is 192.168.000

The ip range is correct. Ping all IP addresses.


192.168.0.0 に ping を送信しています 32 バイトのデータ:
要求がタイムアウトしました。

192.168.0.0 の ping 統計:
    パケット数: 送信 = 1、受信 = 0、損失 = 1 (100% の損失)、

192.168.0.1 に ping を送信しています 32 バイトのデータ:
192.168.0.1 からの応答: バイト数 =32 時間 <1ms TTL=64

192.168.0.1 の ping 統計:
    パケット数: 送信 = 1、受信 = 1、損失 = 0 (0% の損失)、
ラウンド トリップの概算時間 (ミリ秒):
    最小 = 0ms、最大 = 0ms、平均 = 0ms

インターフェイス: 192.168.0.124 --- 0x17
  インターネット アドレス 物理アドレス           種類
  192.168.0.1           74-da-88-a0-e8-19     動的

192.168.0.2 に ping を送信しています 32 バイトのデータ:
要求がタイムアウトしました。

192.168.0.2 の ping 統計:
    パケット数: 送信 = 1、受信 = 0、損失 = 1 (100% の損失)、

(中略)

192.168.0.255 に ping を送信しています 32 バイトのデータ:
要求がタイムアウトしました。

192.168.0.255 の ping 統計:
    パケット数: 送信 = 1、受信 = 0、損失 = 1 (100% の損失)、
.
Display ARP table

インターフェイス: 192.168.0.124 --- 0x17
  インターネット アドレス 物理アドレス           種類
  192.168.0.1           xx-xx-xx-xx-xx-xx     動的
  192.168.0.126         xx-xx-xx-xx-xx-xx     動的
  192.168.0.215         xx-xx-xx-xx-xx-xx     動的
  192.168.0.238         xx-xx-xx-xx-xx-xx     動的
  192.168.0.255         xx-xx-xx-xx-xx-xx     静的
  224.0.0.2             xx-xx-xx-xx-xx-xx     静的
  224.0.0.22            xx-xx-xx-xx-xx-xx     静的

続行するには何かキーを押してください . . .

失敗例(入力ミス)

Enter the ip address up to 24bit. ( Example: 192.168.100 )
Important!: If the target bit is 2 digits or less, enter 0 at the beginning. ( Example: 172.016.002 )
After entering, press Enter.  1
The IP address you entered is 1

Important!
Check the input format again and execute the bat file again.
Especially when the target bit is 2 digits or less, be sure to add 0 at the beginning.
Example: 172.016.005
続行するには何かキーを押してください . . .

動作内容

  • IPアドレス(24bitまで)を格納する変数を作成する。
set ping_survey=""
  • 調査するIPアドレス(24bitまで)の入力を促す。
  • 注意:対象のビットが2桁以下の場合、先頭に0を付けること。
  • (例えば172.10.2.0~172.10.2.255の範囲を調査する場合、IPアドレスを入力する際は「172.010.002」と入力する)
set /P ping_survey="After entering, press Enter.  "
  • 正規表現で正しくIPアドレス(xxx.xxx.xxx)が入力されているか確認する。
echo %ping_survey% | findstr /r "\<[0-9][0-9][0-9][.][0-9][0-9][0-9][.][0-9][0-9][0-9]\>" >nul 2>&1
  • 上記の形式で入力されていない場合、警告文を表示し動作を終了する。
echo Important!
echo Check the input format again and execute the bat file again. 
echo Especially when the target bit is 2 digits or less, be sure to add 0 at the beginning. 
echo Example: 172.016.005
pause
exit
  • 正しくIPアドレスが入力されている場合は動作を開始する。
echo The ip range is correct. Ping all IP addresses.
  • 対象IPアドレスの範囲(xxx.xxxx.xxx.0~255)にpingを打つ。
for /l %%i in (0,1,255) do ping -w 1 -n 1 %ping_survey%.%%i && arp -a %ping_survey%.%%i
  • ARPテーブルを表示し、自身PCのネットワーク上に存在するIPアドレスを表示する。
arp -a

最後に

このバッチを利用して、自身PCのネットワーク上に存在するIPアドレスを全て特定することができます。これにより既に使用されているIPアドレスを把握したり、使用されていないIPアドレスを調査するために役立つと思います。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0