PHP
ping

特定のセグメントにいるIP全体に ping を1回ずつ送る

コマンド 開始 終了
php area_ping.php 0.0.0.0 255.255.255.255
php area_ping.php 1 1.0.0.0 1.255.255.255
php area_ping.php 1 2 1.2.0.0 1.2.255.255
php area_ping.php 1 2 3 1.2.3.0 1.2.3.255
php area_ping.php 1 2 4 1.2.3.4 1.2.3.4
area_ping.php
<?php

function setval(&$area, $start, $end=null) {
  if ($end==null) $end = $start;
  $area[0] = $start;
  $area[1] = $end;
}

$area1 = [ 0, 255 ];
$area2 = [ 0, 255 ];
$area3 = [ 0, 255 ];
$area4 = [ 0, 255 ];

if (isset($argv[1])) {
  setval($area1, $argv[1]);
}
if (isset($argv[2])) {
  setval($area2, $argv[2]);
}
if (isset($argv[3])) {
  setval($area3, $argv[3]);
}
if (isset($argv[4])) {
  setval($area4, $argv[4]);
}

foreach (range($area1[0], $area1[1], 1) as $a1) {
  foreach (range($area2[0], $area2[1], 1) as $a2) {
    foreach (range($area3[0], $area3[1], 1) as $a3) {
      foreach (range($area4[0], $area4[1], 1) as $a4) {
        $ip = sprintf("%d.%d.%d.%d", $a1, $a2, $a3, $a4);
        $cmd = sprintf("ping -c1 -W1 %-16s", $ip);
        echo $cmd , ":";

        $output = [];
        $retval = [];

        exec($cmd, $output, $retval);
        echo $output[3] . PHP_EOL;
      }
    }
  }
}

実行例 (ping の3行目 lost = 失敗)

$ php area_ping.php 13 228 211
ping -c1 -W1 13.228.211.0    :1 packets transmitted, 0 received, 100% packet loss, time 0ms
ping -c1 -W1 13.228.211.1    :1 packets transmitted, 0 received, 100% packet loss, time 0ms
ping -c1 -W1 13.228.211.2    :1 packets transmitted, 0 received, 100% packet loss, time 0ms
ping -c1 -W1 13.228.211.3    :1 packets transmitted, 0 received, 100% packet loss, time 0ms
ping -c1 -W1 13.228.211.4    :1 packets transmitted, 0 received, 100% packet loss, time 0ms
ping -c1 -W1 13.228.211.5    :1 packets transmitted, 0 received, 100% packet loss, time 0ms
ping -c1 -W1 13.228.211.6    :1 packets transmitted, 0 received, 100% packet loss, time 0ms
ping -c1 -W1 13.228.211.7    :1 packets transmitted, 0 received, 100% packet loss, time 0ms
ping -c1 -W1 13.228.211.8    :1 packets transmitted, 0 received, 100% packet loss, time 0ms
ping -c1 -W1 13.228.211.9    :1 packets transmitted, 0 received, 100% packet loss, time 0ms
ping -c1 -W1 13.228.211.10   :1 packets transmitted, 0 received, 100% packet loss, time 0ms
ping -c1 -W1 13.228.211.11   :1 packets transmitted, 0 received, 100% packet loss, time 0ms
ping -c1 -W1 13.228.211.12   :1 packets transmitted, 0 received, 100% packet loss, time 0ms
ping -c1 -W1 13.228.211.13   :1 packets transmitted, 0 received, 100% packet loss, time 0ms
ping -c1 -W1 13.228.211.14   :1 packets transmitted, 0 received, 100% packet loss, time 0ms
ping -c1 -W1 13.228.211.15   :--- 13.228.211.15 ping statistics ---
ping -c1 -W1 13.228.211.16   :1 packets transmitted, 0 received, 100% packet loss, time 0ms
ping -c1 -W1 13.228.211.17   :1 packets transmitted, 0 received, 100% packet loss, time 0ms
ping -c1 -W1 13.228.211.18   :--- 13.228.211.18 ping statistics ---
: (以下続く)