Jump to content

Recommended Posts

I know that php has a function that can execute ping command but i'm not sure how it works.

I am trying to get the result of the ping command to determine:

[*]IP address has a reply (PC/device is turned on)

[*]IP address has no reply (ip address is vacant and not assigned to any pc/device

[*]IP address has a response (PC/device is turned off)

 

is this "do-able" in php??

 

this is my script:

function ping($ip){
// #5882FA blue color indicates Fiber Module
// #FACC2E orange color indicates cascade/uplink cable
	// #81F781 Green color indicates connected/turned on
// #FE2E2E Red color indicates disconnected/turned off
// #585858 dark gray color indicates connected patch panel port is vacant at user side

if($ip=="Cascade" OR $ip=="Uplink"){
	return $status="style=\"background-color:#FACC2E;cursor:pointer;\""; /* orange */
}elseif ($ip=="Fibre Module"){
	return $status="style=\"background-color:#5882FA;cursor:pointer;\""; /* blue */
}elseif ($ip==null or $ip==""){
	return $status="style=\"cursor:pointer;background-image:url(images/x.gif);\""; /* dark gray */	
}else{		
	$str = exec("ping -n 1 -w 1 $ip", $input, $result);
	if ($result == 0){
		return $status="style=\"background-color:#81F781;cursor:pointer;\""; /* green */
		//return "on";
	}else{
		return $status="style=\"background-color:#FE2E2E;cursor:pointer;\""; /* red */
		//return "off";
	}  
}
}

 

i can get it display on or off and i've tested it working. but i just can't figure out if i could use this command to determine if the IP address is vacant or unassigned. I'm using this for a IP list inventory system built in PHP. I have careted a page where the the 3com switch is drawn in tabulated format with each port assigned (via PHP) to the corresponding IP address of the device/PC and indicates the port color as described in the above function. Surprisingly it works well for this switch inventory. I just want to create another page for IP address inventory using the same function. To put is simple i want to have the same function as the sofware "Advance IP-Scanner" which does the same thing as well only thing is my resources are limited to PHP (WAMP).

 

any help guys?

Link to comment
https://forums.phpfreaks.com/topic/257952-php-ping-command/
Share on other sites

I'm not 100% sure what you mean here, "vacant or unassigned" = the same thing?

 

Vacant (adjective)

1. having no contents; empty; void: a vacant niche.

2. having no occupant; unoccupied: no vacant seats on this train.

3. not in use: a vacant room.

 

Unassigned (adjective)

1. unassigned; not assigned.

 

.. but this may help?

 

<?php
// Set the IP you want to ping here
$ip = ??;

// Run the ping to the IP
exec ("ping -n 1 -w 1 $ip", $ping_output);

// Loop through the returned lines from the output
$i=0;
while($i < count($ping_output)) {
  echo "Line $i: " . $ping_line . "<br>";
  $i++;
}
?>

 

Take note of the line that will either say "Reply from *ipaddress*: bytes=.." or "Request timed out." then you can call that with $ping_output[0] (where 0 is the line number) and pop that in a conditional like:

 

<?php
..
if(strpos($ping_output[0],"Reply")) {
  echo "$ip replied, so it must be active!";
}else {
  echo "$ip did not reply so it may not be assigned?";
}
..
?>

 

Then ditch the loop and mash it all together?

 

Yes / No

 

Note: Exact code may not work, not on my dev machine atm.

Link to comment
https://forums.phpfreaks.com/topic/257952-php-ping-command/#findComment-1322178
Share on other sites

Make that..

<?php
// Set the IP you want to ping here
$ip = "10.1.1.1";

// Run the ping to the IP
exec ("ping -n 1 -w 1 $ip", $ping_output);

// Loop through the returned lines from the output
$i=0;
while($i < count($ping_output)) {
  echo "Line $i: " . $ping_output[$i] . "<br>";
  $i++;
}
?>

 

And on my box it's Line 2 we want to look at so this:

<?php
// Set the IP you want to ping here
$ip = "10.1.1.99";

// Run the ping to the IP
exec ("ping -n 1 -w 1 $ip", $ping_output);

if(preg_match("/Reply/i", $ping_output[2])) {
  echo "$ip replied, so it must be active!";
}else {
  echo "$ip did not reply so it may not be assigned?";
}
?>

..should work.

Link to comment
https://forums.phpfreaks.com/topic/257952-php-ping-command/#findComment-1322182
Share on other sites

l0gic, thanks for the English lesson.

 

I could understand the ping "reply" and "request time out". Just curious

if  a PC is assigned an IP address but it is turned of the reply would be returned as "request time out" (which is assigned but not vacant)?

if an IP address is not assigned to any PC would the ping reply also return "request time out" (which is not assigned and vacant)?

 

how to differentiate among the IP address (including those turned off pc)? Let say i enter an ip address to search its availability, can php "ping" or "other commands"  to give the output as below:

[*]IP address is available  - not assigned to any PC/device

[*]IP addres is assigned - the device is turned on

[*]IP address is assigned - the device is turned off

Link to comment
https://forums.phpfreaks.com/topic/257952-php-ping-command/#findComment-1322200
Share on other sites

Yeah I don't think you're going to be able to achieve what you're trying to do with just a PHP script alone. The only way I can see this working is if you manually collect all IP addresses for each PC/Device and store them in a database and use that as a point of reference when it comes to availbility.

 

Or, have each PC/Device run a script or call of some kind when they start-up to enter or update their IP in the database. You could use each devices MAC address as an identifier for this also.

 

And then use the above PHP to check if each IP is active or not by pinging it.

 

Can you explain in anymore detail exactly what you're trying to do and how the network it will be running on is set up? Is each device manually configured to connect to the network, does DHCP come into play?

 

Almost seems like it would be easier to scrape the data from your router/network hardware.

Link to comment
https://forums.phpfreaks.com/topic/257952-php-ping-command/#findComment-1322231
Share on other sites

Yeah I don't think you're going to be able to achieve what you're trying to do with just a PHP script alone. The only way I can see this working is if you manually collect all IP addresses for each PC/Device and store them in a database and use that as a point of reference when it comes to availability.

 

Actually i have a PC-inventory system done in PHP. I have created a vbs file to run on individual PC to capture the details of the hardware (WMIC) info such as serial, brand,model,monitor brand,monitor serial,HD,RAM,Processor,IP address, MAC address, software name & license, printer connected to and printer port. This VBS file will export the result in HTML format and on submit button click the records will be updated into database based on MAC address as key field to minimize duplicate inventory.

 

I was curious to find out if I could have a "IP lookup" function in the inventory system that i built in PHP where the system will generate a list of ip and indicate if its being assigned (PC turned off or PC turned on) and also indicate if the IP is vacant. Currently i can only identify the ip availability based on IP address that is in the database but could not show if its turned on or off or vacant. For example : I have assigned a IP address for a pc - and the pc is turned off. when i run the ip range search it will display the IP in ascending order and this particular IP will appear as "not in use". When i assign that particular IP to a new device it appears as "IP address Conflict". How do i solve this?

 

please refer my screenshot.

post-97500-13482403272909_thumb.jpg

Link to comment
https://forums.phpfreaks.com/topic/257952-php-ping-command/#findComment-1322593
Share on other sites

Ok, so you have a way of passing all the details from each PC to your database, that helps a lot. I would also include the date and time that the information is submitted.

 

When you have that setup it's just a case of putting the right stuff in the right place and using it creatively.

 

You'll then have a database with a collection of IP addresses as reported back by each PC and the date and time that the information was supplied. So have a list of IP addresses that are assigned and the last time they were used, which would be a pretty good indication that they're assigned. The date and time is handy because if that IP address hasn't been used for like a month then it gives you something to investigate and maybe even an IP address to re-assign to a new machine.

 

I think though you may be over complicating it a tad. If I were you and was trying to do this, and each PC would report it's information to say a PHP script on your server that will insert or update records in your database then I'd just make an extra table in the database call it what you will, 'ip_list' or something.

 

ip_address   |   assigned   |   last_use
192.168.0.1  |   true       |   February 28, 2012
192.168.0.2  |   true       |   February 28, 2012
192.168.0.3  |   false      |   none
192.168.0.4  |   false      |   none

 

Obviously you'd fill this out with a script and not by hand.. And could add more fields as it suits.

 

<?php
// WARNING: I'd only run this once!
$i = 1;
while($i < 255) {
  $ip = "192.168.0.".$i;
  $sql = "INSERT INTO ip_list (ip_address, assigned, last_use) VALUES ('$ip', 'false', 'none')"
  // And the rest to run the query, etc.
  $i++;
}
?>

 

Then a simple bit of SQL can find you all the assigned IP address, loop through them to see if they're online or offline.

 

$sql = "SELECT ip_adress, assigned, last_use FROM ip_list WHERE assigned='true'";

 

..and another query can return you IP address that aren't assigned..

 

$sql = "SELECT ip_adress, assigned, last_use FROM ip_list WHERE assigned='false' LIMIT 10";

 

I'd use a LIMIT so you don't get a wall of IPs crit you for over nine thousand damage, limit can be set as a variable too.

 

Can you see where I'm heading with this?

 

It sounds like you have all the tools to put this in place and the scripts running on each PC would keep the ip_list table as up to date as I'd imagine you'd need.

Link to comment
https://forums.phpfreaks.com/topic/257952-php-ping-command/#findComment-1322646
Share on other sites

I was curious to find out if I could have a "IP lookup" function in the inventory system that i built in PHP where the system will generate a list of ip and indicate if its being assigned (PC turned off or PC turned on) and also indicate if the IP is vacant. Currently i can only identify the ip availability based on IP address that is in the database but could not show if its turned on or off or vacant.

 

There is no way with PING or any other network tool to distinguish between an IP with a device that is off, or an IP that has no device attached.  You just need to keep your own list of IPs somewhere and what they are attached too.  When you need to find an un-allocated ip you consult the list. This list (your database) is your authoritativesource as to whether or not an IP is available.  Based on your screen shot I am guessing you have a record in your DB for every IP, whether it is assigned something or not.  So what you do to find a vacant ip is just query the database for any IP address without an assigned device.

 

How do i solve this?

 

Make sure your list is accurate, and don't assign duplicate IP's.  Or install a DHCP server and let it manage your IP space for you automatically.

 

 

Link to comment
https://forums.phpfreaks.com/topic/257952-php-ping-command/#findComment-1322648
Share on other sites

Thanks l0gic for lengthy and well explained theory. I think i will stick with your theory for the update of "IP status" using date. I do have a table for IP list, now i have to add in another 2 more field as you mentioned.

 

my vbs file does not send data all the time, it only send when i click submit button after the vbs script has produced the HTML file and even if the user click on submit it will not be updated as my key field is the MAC address of the machine and it will cut-off before it reaches the INSERT INTO sql query if it detects a duplicate MAC Address entry plus my inventory system has a user login security feature where only "admin" level will be able to Add/Edit/Delete records and "User" level only able to view records. No direct access to any php page without login details.

 

kicken,

Thanks for the info on PHP ability based on my request. I only have records of assigned IP list in a table, i just wanted to differentiate between vacant,turned-off,turned-on machines. l0gics theory looks good to me. I will try it out and update this forum.

 

Thanks to both of you.

wait for my updates.

Link to comment
https://forums.phpfreaks.com/topic/257952-php-ping-command/#findComment-1322666
Share on other sites

If you are using dhcp on a server you dont need to rely on the pc users clicking any submit buttons, you can run scripts that log use and manage sessions, you can assign ip addresses based on mac address and you can also get the computer name along with time of log in and log out.

You can grab the data from these logs and do with it what you will in any php page you wish to monitor the information on.

 

I have a walled garden setup using a dhcp server, where logs are recorded in a mysql db, along with there current status, mac address, host name and assigned IP, you can graph usage, track events and assigned IP addresses from specified ranges, even as far as to say certain machines can only use certain ranges or specific IP addresses. I even control what content is available to them with this method.

 

I am not sure of your complete scenario but that may be what your looking for.

Link to comment
https://forums.phpfreaks.com/topic/257952-php-ping-command/#findComment-1322695
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.