| View previous topic :: View next topic |
| Author |
Message |
Localhost I post too much
Reputation: 0
Joined: 28 Apr 2007 Posts: 3402
|
Posted: Wed Jun 04, 2008 5:00 pm Post subject: [PHP] Pinging A Site |
|
|
| Code: | <?php
$serverHost = "www.cheatengine.org";
$serverPort = 80;
function createSocket()
{
$socket = @socket_create( AF_INET, SOCK_STREAM, SOL_TCP );
@socket_set_option( $socket, SOL_SOCKET, SO_RCVTIMEO, array("sec"=>10, "usec"=>100) );
if( !$socket ){
die( 'Failed to create a new socket.' );
}else{
return $socket;
}
}
function CheckIfRunning()
{
global $serverHost, $serverPort;
$socket = createSocket();
$connection = @socket_connect( $socket, gethostbyname( $serverHost ), $serverPort );
if( !$connection )
echo( 'Server is not online.' );
else
echo( 'Server is online!' );
@socket_close( $socket );
}
?>
<HTML>
<HEAD>
<TITLE>Server Check</TITLE>
</HEAD>
<BODY>
<p> <?php CheckIfRunning(); ?> </p>
</BODY>
</HTML> |
This is not my own work.
How can this be done, but with multiple hosts and ports? |
|
| Back to top |
|
 |
Noz3001 I'm a spammer
Reputation: 26
Joined: 29 May 2006 Posts: 6220 Location: /dev/null
|
Posted: Wed Jun 04, 2008 5:01 pm Post subject: |
|
|
| Use an array for the serverHost variable and loop through it. |
|
| Back to top |
|
 |
Localhost I post too much
Reputation: 0
Joined: 28 Apr 2007 Posts: 3402
|
Posted: Wed Jun 04, 2008 5:22 pm Post subject: |
|
|
how do i "loop" through it.
Also... i think it will be the same serverHost, but different IPs... Thanks  |
|
| Back to top |
|
 |
Noz3001 I'm a spammer
Reputation: 26
Joined: 29 May 2006 Posts: 6220 Location: /dev/null
|
Posted: Wed Jun 04, 2008 5:28 pm Post subject: |
|
|
| Code: | for ( $i = 0 ; i < $numberOfHosts ; $i++ ) {
// Ping serverHost[i]
} |
I don't know PHP so I cant really help you any more. The commented bit is where you do your ping on $serverHost[i]. |
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8588 Location: 127.0.0.1
|
Posted: Wed Jun 04, 2008 5:28 pm Post subject: |
|
|
| Code: | $site_array = array( "www.cheatengine.org", "www.google.com" );
for( $i=0; $i<count($site_array); $i++ )
{
// Adjust ping code inside here.
} |
A small example of what would be needed.
If you are wondering the above code is mine. I wrote it for another person in another topic. _________________
- Retired. |
|
| Back to top |
|
 |
Localhost I post too much
Reputation: 0
Joined: 28 Apr 2007 Posts: 3402
|
Posted: Wed Jun 04, 2008 5:33 pm Post subject: |
|
|
ah. I am very confused now. I have to ping multiple ports on the same IP.
Thanks  |
|
| Back to top |
|
 |
Noz3001 I'm a spammer
Reputation: 26
Joined: 29 May 2006 Posts: 6220 Location: /dev/null
|
Posted: Wed Jun 04, 2008 5:35 pm Post subject: |
|
|
| Well its the same thing but you replace the host names with the ports. |
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8588 Location: 127.0.0.1
|
Posted: Wed Jun 04, 2008 5:35 pm Post subject: |
|
|
If you need that, then use 1 static address, and array the ports instead. Then adjust the code to use the for loop variable for the port array instead. Should be easy enough to fix. _________________
- Retired. |
|
| Back to top |
|
 |
Noz3001 I'm a spammer
Reputation: 26
Joined: 29 May 2006 Posts: 6220 Location: /dev/null
|
Posted: Wed Jun 04, 2008 5:38 pm Post subject: |
|
|
| Wiccaan wrote: | | If you need that, then use 1 static address, and array the ports instead. Then adjust the code to use the for loop variable for the port array instead. Should be easy enough to fix. |
And if he wants to do a range of ports he can just use a variable and ++ it in a loop until it hits the last port. |
|
| Back to top |
|
 |
Localhost I post too much
Reputation: 0
Joined: 28 Apr 2007 Posts: 3402
|
Posted: Wed Jun 04, 2008 5:41 pm Post subject: |
|
|
| Code: |
<?php
$serverHost = "www.google.com";
$serverPort = array(80,8080);
for( $i=0; $i<count($serverPort); $i++ )
{
function createSocket()
{
$socket = @socket_create( AF_INET, SOCK_STREAM, SOL_TCP );
@socket_set_option( $socket, SOL_SOCKET, SO_RCVTIMEO, array("sec"=>10, "usec"=>100) );
if( !$socket ){
die( 'Failed to create a new socket.' );
}else{
return $socket;
}
}
function CheckIfRunning()
{
global $serverHost, $serverPort;
$socket = createSocket();
$connection = @socket_connect( $socket, gethostbyname( $serverHost ), $serverPort );
if( !$connection )
echo( 'Server is not online.' );
else
echo( 'Server is online!' );
@socket_close( $socket );
}
}
?>
<HTML>
<HEAD>
<TITLE>Server Check</TITLE>
</HEAD>
<BODY>
<p> <?php CheckIfRunning(); ?> </p>
</BODY>
</HTML>
|
So like that?
Also how do i display it so it is like:
80: is open
8080: is closed
etc etc |
|
| Back to top |
|
 |
Noz3001 I'm a spammer
Reputation: 26
Joined: 29 May 2006 Posts: 6220 Location: /dev/null
|
Posted: Wed Jun 04, 2008 5:53 pm Post subject: |
|
|
| Code: | $serverHost = "www.google.com";
$socket = @socket_create( AF_INET, SOCK_STREAM, SOL_TCP );
@socket_set_option( $socket, SOL_SOCKET, SO_RCVTIMEO, array("sec"=>10, "usec"=>100) );
for( $i = 80 ; $i < 8080 ; $i++ )
{
$connection = @socket_connect( $socket, gethostbyname( $serverHost ), $i );
if( !$connection )
echo( 'Port ' + $i ' + is not open!' );
else
echo( 'Port ' + $i + ' is open!' );
} |
I don't know PHP so correct any errors and change stuff. It might work  |
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8588 Location: 127.0.0.1
|
Posted: Wed Jun 04, 2008 9:22 pm Post subject: |
|
|
Heres a simple port scanner. Adjust the timeouts to make it faster if needed:
| Code: | <?php
//
// Site Variables
//
$site_link = "www.google.com";
$site_port = array( 80, 90 );
//
// createSocket()
//
// Creates a new connection socket object. Note, you need
// to close this socket yourself after creating it!
//
function createSocket()
{
$socket = @socket_create( AF_INET, SOCK_STREAM, SOL_TCP );
@socket_set_option( $socket, SOL_SOCKET, SO_RCVTIMEO, array("sec"=>10, "usec"=>0) );
@socket_set_option( $socket, SOL_SOCKET, SO_SNDTIMEO, array("sec"=>10, "usec"=>0) );
if( !$socket ){
die( 'Failed to create a new socket.\n' );
}else{
return $socket;
}
}
//
// checkPorts()
//
// Scans the given site for open ports based on the port array
// above. The scan starts with the first port and scans upto
// the second port in the array.
//
function checkPorts()
{
global $site_link, $site_port;
echo( 'Site: ' . $site_link . " " . gethostbyname( $site_link ) . "<br />\n" );
for( $i = $site_port[0]; $i <= $site_port[1]; $i++ )
{
// Create Socket Instance
$socket = createSocket();
// Make Connection
$connection = @socket_connect( $socket, gethostbyname( $site_link ), $i );
// Output Result
if( !$connection ){
echo( 'Port: ' . $i . " is currently closed.<br /> \n" );
}else{
echo( 'Port: ' . $i . " is currently open.<br /> \n" );
}
// Close The Open Socket
@socket_close( $socket );
}
}
?>
<HTML>
<HEAD>
<TITLE>Port Check</TITLE>
</HEAD>
<BODY>
<p> <?php checkPorts(); ?> </p>
</BODY>
</HTML> |
_________________
- Retired. |
|
| Back to top |
|
 |
|