<?php
//掃描端口腳本 -- PHP CLI 版本
if(!extension_loaded('sockets')){
die('sockets has not open');
}
echo 'Please enter the host name or IP address'."\r\n";
$host = trim(fgets(STDIN));
$host = str_replace('http://','',$host);
$host = str_replace('https://','',$host);
//如果輸入的不是IP地址,嘗試轉換成IP地址
if(!filter_var($host,FILTER_VALIDATE_IP)){
$host = gethostbyname($host);
}
echo 'Please enter the ports by scanner'."\r\n";
$ports = trim(fgets(STDIN));
$ports = explode(',',str_replace(",",",",$ports));
echo 'Please enter the timeout ,Default 30 second';
$timeout = trim(fgets(STDIN));
if(!$timeout){
$timeout = 30;
}
foreach ($ports as $port) {
//創建一個套接字,成功則端口打開。
$conn = @fsockopen($host,$port,$error_no,$error_str,$timeout);
if($conn){
fclose($conn);
echo $host.':'.$port.' is open'."\r\n";
}else{
echo $host.':'.$port.' is close'."\r\n";
}
}
?>