Solved

Pull IP Address using PHP and WMI

Posted on 2011-09-29
18
597 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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 250 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to count occurrences of each item in an array.
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now