Jump to content
xisto Community
Sign in to follow this  
NeOniX

Checking For Open Ports From Php If you want to check the status of a server at a specific port you can

Recommended Posts

Some days ago, i needed to check ports of a server from a webpage, for advising of its status.

I simply used a great php code that's fsockopen. I'll explain it in the following example:

 

(Imagine a file called 'checkports.php', containing the next)

<?$address =" Xisto.com"; //Here you can specify the address you want to check ports$port = "80"; //Here you can specify the port you want to check from $address$checkport = fsockopen($address, $port, $errnum, $errstr, 2); //The 2 is the time of ping in secs//Here down you can put what to do when the port is closedif(!$checkport){	   echo "The port ".$port." from ".$address." seems to be closed.";  //Only will echo that msg}else{//And here, what you want to do when the port is open	   echo "The port ".$port." from ".$address." seems to be open."; //The msg echoed if port is open}?>

Well, that's all, but you can do with it a lot of interesting stuff, i.e., you put that on a host with PHP, and check if another host is up, and it is, redirect to... etc.

 

Enjoy! :)

Edited by NeOniX (see edit history)

Share this post


Link to post
Share on other sites

Awsome code! Thanks a ton for it! I'm not really sure what I can use it for, but it sounds cool!Do you need any special server permissions to run this script? Will it run on all servers?

Share this post


Link to post
Share on other sites

Do you need any special server permissions to run this script? Will it run on all servers?


Lol er... well dunno if all servers let u using fsockopen(), cuz i actually use my own server, so i suppose it depends of the host. You could post a topic on a forum askin' 4 that. :)
Anyway, yesterday i try to use on a free hosting server with PHP, and it gave me back an error like 'Due to security reasons...' :(

(note: tried out in Xisto hosting and works)
Edited by NeOniX (see edit history)

Share this post


Link to post
Share on other sites

Well, i didnt know if Xisto.com disable some function like fsockopen(), shell_exec(), passthru(), and many other PHP function to communicate with filesystem. I think it may be disable for security reason...! :D. By the way fsockopen() is usefull for grabbing page, etc besid file_get_contents() function.

Share this post


Link to post
Share on other sites

Thnk you very muchI appreciate B)but maybe you have this error:"A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond"that because you have to put error handler

Share this post


Link to post
Share on other sites

Sometime we need to find next available free port on which we can start a service.

The following code can be used to get next available free port between 8000 - 1000, you can change the range!

function get_next_free_port() {	for($i=8000; $i<=10000; $i++) {		$conn = @fsockopen("localhost", $i);		if ($conn) {			fclose($conn);		}		else {			return $i;		}	}  }

I just extended the above code, so that it could help newbie like me :P J

Thanks,
Sachin.
Edited by moderator (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.