1. 程式人生 > >PHP掃描內網存活主機

PHP掃描內網存活主機

直接上程式碼

<?PHP
header('Content-Type: text/html; charset=utf-8');  
error_reporting(0);  

function ping($host, $timeout = 1) {
	$package = "\x08\x00\x7d\x4b\x00\x00\x00\x00PingHost";
	$socket  = socket_create(AF_INET, SOCK_RAW, 1);
	socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $timeout, 'usec' => 0));
	socket_connect($socket, $host, null);

	$ts = microtime(true);
	socket_send($socket, $package, strLen($package), 0);
	if (socket_read($socket, 255))
		$result = microtime(true) - $ts;
	else    
		$result = false;
	socket_close($socket);
	return $result;
}
function get_ip_pre($ip){  
	$num = explode('.',$ip);  
	return $num[0].".".$num[1].".".$num[2].".";  
} 	
$startip = $argv[1];
$endip = $argv[2];
$ippre = get_ip_pre($startip);
$startnum = array_pop(explode(".",$startip));
$endnum = array_pop(explode(".",$endip));
	
for($i=$startnum; $i<=$endnum; $i++){
	$ip = $ippre."$i";
	$res = ping($ip,2);
	if($res)
		echo "$ip is onlink\n";
	else 
		echo "$ip sense shutdown\n";
	$ip = $ippre;
	ob_flush();  
	flush();
}
?>