Advertisement
P22DX

socks.php

Aug 1st, 2020
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. <?php
  2.  
  3. $host = $argv[1];
  4. $port = $argv[2];
  5. $timeout = ini_get('default_socket_timeout') ?: 1;
  6. $fileName = 'result.json';
  7. $portRange = explode('-', $port);
  8. $ports = range($portRange[0], $portRange[1]);
  9.  
  10. function socket_scan(string $host, int $port, int $timeout = 1) : bool
  11. {
  12. $errstr = $errno = null;
  13. if ($sock = @fsockopen($host, $port, $errno, $errstr, $timeout)) {
  14. stream_set_blocking($sock, false);
  15. fclose($sock);
  16. }
  17. return (bool)$sock;
  18. }
  19.  
  20. foreach ($ports as $port) {
  21. if (socket_scan($host, $port, $timeout)) {
  22. echo sprintf('%s:%d is open%s', $host, $port, PHP_EOL);
  23. $result['host'][$host]['port'][$port]['status'] = 'open';
  24. } else {
  25. echo sprintf('%s:%d is closed%s', $host, $port, PHP_EOL);
  26. $result['host'][$host]['port'][$port]['status'] = 'closed';
  27. }
  28. }
  29.  
  30. $jsonResult = json_encode($result, JSON_PRETTY_PRINT);
  31. $fp = fopen($fileName, 'a');
  32. fprintf($fp, $jsonResult);
  33. fclose($fp);
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement