Hello! There is such a php script:

ini_set('max_execution_time', 1000); ini_set("default_socket_timeout", 1); $ips=file("ip.txt"); function ssh_exec($n) { if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist"); if(!(@$con = ssh2_connect($n, 22))){ echo $n." - fail: unable to establish connection\n"; } else { if(!ssh2_auth_password($con, "user", "pass")) { echo $n." - fail: unable to authenticate\n"; } else { if (!($stream = ssh2_exec($con, "cat version" ))) { echo $n." - fail: unable to execute command\n"; } else { stream_set_blocking($stream, true); $data = ""; while ($buf = fread($stream,4096)) { $data .= $buf; } if (!$data = 'version.1') { echo $n." - ".$data;} else {echo $n." - ok\n";} fclose($stream); } } } } echo "<pre>"; foreach ($ips as $ip => $n) { ssh_exec(trim($n)); } echo "</pre>"; echo "Time: " . number_format(( microtime(true) - $startTime), 4) . " Seconds\n"; 

One connection takes 1.3 seconds, the problem is that IPs are 600+ and it takes about 13-15 minutes to interrogate all devices. Is it possible to optimize this process / script? SNMP is not an option yet.

  • you set stream_set_blocking yourself, when, obviously, you can process thirty items in non-blocking mode - etki
  • c stream_set_blocking ($ stream, 0) the difference is 1 second at 20 ip - vital mar
  • and I'm not talking about changing one line. - etki

1 answer 1

If from under the browser, it can be parallelized by sending the operation of the connection and checking to a separate thread; if it is simple, you can use pcntl_fork(); if you have access to the shell, you can do something like this:

 seq 600| parallel php your_script.php {} > log_check_ssh{} 

Ie, run your script 600 times where {} is the sequence number of your connection from the connection array and in the script read this number through $argv[1] , so you run 600 checks in parallel and save each check to the log named log_check_ssh{} .