Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Port scanner on PHP
<?php
ini_set('max_execution_time', 0);
ini_set('memory_limit', -1);
$host = 'google.com';
$ports = array(21, 25, 80, 81, 110, 143, 443, 587, 2525, 3306);
foreach ($ports as $port)
{
$connection = @fsockopen($host, $port, $errno, $errstr, 2);
if (is_resource($connection))
{
echo '<h2>' . $host . ':' . $port . ' ' . '(' . getservbyport($port, 'tcp') . ') is open.</h2>' . "\n";
fclose($connection);
}
else
{
echo '<h2>' . $host . ':' . $port . ' is not responding.</h2>' . "\n";
}
}
@mandrakevenom

This comment has been minimized.

Copy link

@mandrakevenom mandrakevenom commented May 12, 2018

thank you

@ferdousulhaque

This comment has been minimized.

Copy link

@ferdousulhaque ferdousulhaque commented Jul 8, 2018

Thank you. Very helpful.

@crypto-maniac

This comment has been minimized.

Copy link

@crypto-maniac crypto-maniac commented Oct 16, 2018

hello , this is an old thread but im curious to know how to put the url as variable and all in a loop
so the script is able to process many URL from a .txt
thanks for the help

@bragle

This comment has been minimized.

Copy link

@bragle bragle commented Nov 11, 2018

@crypto-maniac I think you should try to google a little bit before asking for help (cause you would have figured it out if you tried), but here's some code to get you started:

$document = file_get_contents('urls.txt');
$hosts = explode(PHP_EOL, $document);
$ports = [21, 25, 80, 81, 110, 143, 443, 587, 2525, 3306];

foreach($hosts as $host){
	//just copy pasta from line 8 to 22
}
@rafaelvr

This comment has been minimized.

Copy link

@rafaelvr rafaelvr commented Apr 12, 2020

Congratulations on the code, it helped a lot but I have a problem when testing port 3389 rdp, could you help me?

@namcity86

This comment has been minimized.

Copy link

@namcity86 namcity86 commented Sep 28, 2020

Example: I want to check for 100 IP addresses, with 3 ports 20, 40, 60. Can anyone help me with a complete code to run the check port loop of 100 IP addresses? Thanks everyone

@mysterious4579

This comment has been minimized.

Copy link

@mysterious4579 mysterious4579 commented Nov 7, 2020

Example: I want to check for 100 IP addresses, with 3 ports 20, 40, 60. Can anyone help me with a complete code to run the check port loop of 100 IP addresses? Thanks everyone

Do you still need help?

@v1ckyz

This comment has been minimized.

Copy link

@v1ckyz v1ckyz commented Nov 20, 2020

how to scan range ip address?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment