The following PHP code scans IP range 192.168.204.200 to 192.168.204.254 port 443 in 5 seconds. It's tested on Windows with PHP 5.3, with php_socket extension enabled.
<?php
$port = "443";
$timeout = 5; //timeout in seconds
$write=array();
for ($i=200;$i<255;$i++){
$host="192.168.204.$i";
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Unable to create socket\n");
socket_set_nonblock($socket) or die("Unable to set nonblock on socket\n");
$connected=@socket_connect($socket, $host, $port);
if (!$connected) {
$error = socket_last_error($socket);
if ($error != 10035 && $error != SOCKET_EINPROGRESS && $error != SOCKET_EALREADY) {
socket_close($socket);
}else{
$write[]=$socket;
}
}
}
$count=0;
$write0=$write;
$mynil=NULL;
$timeout_us=0;
$endtime=microtime(true)+$timeout;
$address="";
while(true){
$ret=socket_select($mynil,$write,$mynil,$timeout,$timeout_us);
if ($ret==0){
die("Done. Total $count found.\n");
}
if ($ret>0){
foreach ($write as $sock){
socket_getpeername($sock,$address);
echo "$address\n";
$count++;
socket_close($sock);
}
$write=array_diff($write0,$write);
$write0=$write;
$newtimeout=$endtime-microtime(true);
$timeout=floor($newtimeout);
$timeout_us=$newtimeout-$timeout;
}
}
No comments:
Post a Comment