Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Pull IP Address using PHP and WMI

Posted on 2011-09-29
18
Medium Priority
?
615 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to dynamically set the form action using jQuery.
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 …
Suggested Courses

564 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