?
Solved

Pull IP Address using PHP and WMI

Posted on 2011-09-29
18
Medium Priority
?
622 Views
Last Modified: 2012-05-12
When I run the code below and try to echo the ipaddr I keep getting an error, Object of class variant could not be converted to string. How do I convert this into just the IP address? Thanks
<?php

			$host = "computer number";
			$user = "doamin\user";
			$pass = "password";
			
    //$obj = new COM ( 'winmgmts://'. $displayasset .'/root/CIMV2' );
	$wmiLocator = new COM('WbemScripting.SWbemLocator');
	$obj = $wmiLocator->ConnectServer($host, 'root\cimv2', $user, $pass);

   // $fso = new COM ( "Scripting.FileSystemObject" );   

	$ipaddr			  	   =	$obj->ExecQuery("SELECT IPAddress From Win32_NetworkAdapterConfiguration Where IPEnabled = True");
	

settype($ipaddr, "string");
echo $ipaddr;


?>

Open in new window

0
Comment
Question by:maximus81
  • 8
  • 6
  • 4
18 Comments
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36814772
Are you getting "Class 'COM' not found in <name of file> on line 8?"

The class COM isn't defined in the PHP you are using.  What version of PHP are you using?

At a command/shell prompt type

php -version

to find out.
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36814804
And this may seem like a stupid question, since you said WMI but I'd better make sure.  Is your PHP server running in Windows?
0
 

Author Comment

by:maximus81
ID: 36814843
I am using PHP version 5.3.8 and I am running Windows Server 2003. Here is the fill error:

( ! ) Catchable fatal error: Object of class variant could not be converted to string in C:\wamp\www\atsmanage\test.php on line 16
Call Stack
#      Time      Memory      Function      Location
1      0.0006      365808      {main}( )      ..\test.php:0
2      0.0443      367944      settype ( )      ..\test.php:16

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36816002
In your php.ini file, do you have a line like this?

;com.allow_dcom = true

Open in new window


If so, remove the semiolon and try again.  (or if it's set to false, change false to true.)  Otherwise, I'm stuck.  (Perhaps someone else can help).
0
 

Author Comment

by:maximus81
ID: 36816059
Still get the same error message.
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36816511
Oh.  If you didn't restart the server, you'll need to.  Otherwise, I think I'm out of ideas.  

If it doesn't work, hopefully someone else will be by soon.
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 36816679
I did something like that, just give me a sec.
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 36816829
Take a look here and replace where necessary:

  

	$ipaddr			  	   =	$obj->ExecQuery("SELECT IPAddress From Win32_NetworkAdapterConfiguration Where IPEnabled = True");
	
	if ( $ipaddr->count > 0 ){
        foreach($ipaddr as $row){
            //var_dump($row);
            $key = "ipaddress";
            $type = gettype($row->$key);
            //echo $row->$key;
            //var_dump($row->$key);
            
            foreach($row->$key as $k => $v){
                var_dump($v);
            }
        }
	}

Open in new window

0
 

Author Comment

by:maximus81
ID: 36816894
This works but how do I display only the IP address instead of the both the IP and MAC address? Thanks
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36817251
For starters, don't use var_dump for display.   Try

echo $row->$key;

If that gives too much information, please copy & paste what it shows and I (or someone) can help you further.
0
 

Author Comment

by:maximus81
ID: 36817270
Getting this error from that:

( ! ) Catchable fatal error: Object of class variant could not be converted to string in C:\wamp\www\atsmanage\easset.php on line 306
Call Stack
#      Time      Memory      Function      Location
1      0.0008      468032      {main}( )      ..\easset.php:0
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 36817332
What is your output with the code I provided ? I think there shouldn't be a mac.
0
 

Author Comment

by:maximus81
ID: 36817350
Sorry, its not a MAC address, on the Windows 7 machines it gets the IPV6 address also. How do I just get the normal value so i can insert it into a variable using PHP so i can insert it into my database?
0
 

Author Comment

by:maximus81
ID: 36817352
Here is the output I get:

string '172.16.101.237' (length=14)
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36817387
That's from var_dump, right?  Just  use  echo instead.

This might work

echo ( $v );

or

printf ( "%s\n", $v );

or

echo $v . "\n";
0
 

Author Comment

by:maximus81
ID: 36817418
It displays correctly but when I add it to the database it shows only the first number. How do get the whole string?
0
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 1000 total points
ID: 36817498
Try this loop

            foreach($row->$key as $k => $v){
                //var_dump($v);
                $num = substr($v,0,substr($v,0,2));
                echo $num;
            }

Open in new window

0
 

Author Closing Comment

by:maximus81
ID: 36817525
This worked perfect. Thank you for all your help.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
I recently worked on a Wordpress site that utilized the popular ContactForm7 (https://contactform7.com/) plug-in that only sends an email and does not save data. The client wanted the data saved to a custom CRM database. This is my solution.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

584 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question