Solved

Pull IP Address using PHP and WMI

Posted on 2011-09-29
18
599 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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
selecting both the price and sum(price ) with mysql statement 5 47
Selecting SUM data from mysql table with PHP 4 20
Insert values are dynamic 11 42
error log using ftp 7 41
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

803 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