Solved

Pull IP Address using PHP and WMI

Posted on 2011-09-29
18
601 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

740 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