Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $host = $argv[1];
- $port = $argv[2];
- $timeout = ini_get('default_socket_timeout') ?: 1;
- $fileName = 'result.json';
- $portRange = explode('-', $port);
- $ports = range($portRange[0], $portRange[1]);
- function socket_scan(string $host, int $port, int $timeout = 1) : bool
- {
- $errstr = $errno = null;
- if ($sock = @fsockopen($host, $port, $errno, $errstr, $timeout)) {
- stream_set_blocking($sock, false);
- fclose($sock);
- }
- return (bool)$sock;
- }
- foreach ($ports as $port) {
- if (socket_scan($host, $port, $timeout)) {
- echo sprintf('%s:%d is open%s', $host, $port, PHP_EOL);
- $result['host'][$host]['port'][$port]['status'] = 'open';
- } else {
- echo sprintf('%s:%d is closed%s', $host, $port, PHP_EOL);
- $result['host'][$host]['port'][$port]['status'] = 'closed';
- }
- }
- $jsonResult = json_encode($result, JSON_PRETTY_PRINT);
- $fp = fopen($fileName, 'a');
- fprintf($fp, $jsonResult);
- fclose($fp);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement