note 120148 added to function.proc-open
| From: | Anonymous | Date: | Fri, 11 Nov 2016 13:27:50 +0000 |
| Subject: | note 120148 added to function.proc-open | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-207072@lists.php.net to get a copy of this message | ||
Mass ping for windows, with non stream block (async).
Out:
Cant resolve host/request timeout: nonexistendomain3293491.com
Host: duckduckgo.com : 650ms
Host: google.com : 1350ms
Host: yahoo.com : 1290ms
Host: baidu.com : 79ms
Cant resolve host/request timeout: php.net
<?php
$massPing[] = new Ping('php.net');
$massPing[] = new Ping('duckduckgo.com');
$massPing[] = new Ping('google.com');
$massPing[] = new Ping('yahoo.com');
$massPing[] = new Ping('baidu.com');
$massPing[] = new Ping('nonexistendomain3293491.com');
while(1) {
if(empty($massPing))
break;
foreach($massPing as $key => $ping) {
if(($ret = $ping->getIntVal()) && is_int($ping->getIntVal()))
{
echo 'Host: ', $ping->host, ' : ' ,$ping->getIntVal(),
'ms'. PHP_EOL;
unset($massPing[$key]);
} else if($ping->getIntVal() === false) {
echo 'Cant resolve host/request timeout: ', $ping->host. PHP_EOL;
unset($massPing[$key]);
}
}
sleep(1);
}
class Ping
{
private $rawAnswer = null;
private $process;
private $pingIntVal = null;
public $host = '';
function __construct($host)
{
$this->host = $host;
$this->process = new RunProcess('chcp 437 | ping '.$host.' -l 1200');
}
public function getIntVal()
{
if($this->pingIntVal !== null)
return $this->pingIntVal;
if($this->process->isRunning())
return '';
$this->rawAnswer = $this->process->getContents();
$ret = $this->parsePingAnswer();
if(is_int($ret)) {
$this->pingIntVal = $ret;
return $ret;
}
else
return false;
}
private function parsePingAnswer()
{
preg_match('/Average = ([0-9]+)ms/is', $this->rawAnswer, $out);
if(!empty($out) && is_numeric($out[1]))
return (int)$out[1];
else
false;
}
}
class RunProcess {
public $cmd = '';
private $descriptors = array(
0 => array('pipe', 'r'),
1 => array('pipe', 'w'),
2 => array('pipe', 'w')
);
public $pipes = NULL;
public $resource = NULL;
private $exitcode = NULL;
function __construct($cmd = '')
{
$this->cmd = $cmd;
$this->resource = proc_open($this->cmd, $this->descriptors, $this->pipes);
}
public function getContents()
{
return stream_get_contents($this->pipes[1]);
}
public function isRunning()
{
$status = proc_get_status($this->resource);
if ($status['running'] === FALSE && $this->exitcode === NULL)
$this->exitcode = $status['exitcode'];
return $status['running'];
}
public function getExitcode()
{
return $this->exitcode;
}
}
?>
----
Server IP: 72.52.91.14
Probable Submitter: 109.63.236.84
----
Manual Page -- http://php.net/manual/en/function.proc-open.php
Edit -- https://master.php.net/note/edit/120148
Del: integrated -- https://master.php.net/note/delete/120148/integrated
Del: useless -- https://master.php.net/note/delete/120148/useless
Del: bad code -- https://master.php.net/note/delete/120148/bad+code
Del: spam -- https://master.php.net/note/delete/120148/spam
Del: non-english -- https://master.php.net/note/delete/120148/non-english
Del: in docs -- https://master.php.net/note/delete/120148/in+docs
Del: other reasons-- https://master.php.net/note/delete/120148
Reject -- https://master.php.net/note/reject/120148
Search -- https://master.php.net/manage/user-notes.php