Click here to Skip to main content
15,885,980 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have an output of ipconfig command stored in a string variable
now every one knows it has alot of info but i only want ipv4 address in all of this info
how can i extract this specific ipv4 addrress from all this other data of ipconfig
i can't use any other thing then string variable to store the result of ipconfig
i am mentioning below what i am trying to search and get the info after this specific string is matched


Windows IP Configuration


Ethernet adapter Ethernet:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Ethernet adapter Ethernet 4:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Unknown adapter Local Area Connection:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Wireless LAN adapter Local Area Connection* 9:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Wireless LAN adapter Local Area Connection* 14:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Ethernet adapter Ethernet 3:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Unknown adapter VPN - VPN Client:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Wireless LAN adapter Wi-Fi:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::190f:cbb9:3f5d:46fd%7
   IPv4 Address. . . . . . . . . . . : 192.168.1.55
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.1.1

Ethernet adapter vEthernet (Default Switch) 3:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::1589:a303:4153:48fa%48
   IPv4 Address. . . . . . . . . . . : 192.168.204.145
   Subnet Mask . . . . . . . . . . . : 255.255.255.240
   Default Gateway . . . . . . . . . :


What I have tried:

char* search2 = "IPv4 Address. . . . . . . . . . . :";
Posted
Updated 12-Feb-20 5:09am
v2

As a quick and dirty ™ approach, relying on ipconfig consistent output format, you may write
C++
#include <iostream>
#include <sstream>
using namespace std;

int main()
{
  const string Pattern = "   IPv4 Address. . . . . . . . . . . : ";
  const size_t Len = Pattern.length();

  string line;

  while ( getline(cin, line) )
  {
    if ( line.find(Pattern) != string::npos)
    {
      string ip4 = line.substr(Len);
      cout << ip4 << "\n";
    }
  }
}


The above program, running with posted data as input, outputs
192.168.1.55
192.168.204.145



[update]
(as per OP request...)
A function taking a string as argument and returning the first IPV4 as a string.
C++"
std::string get_first_ipv4(const std::string & ipconfig_info)
{
  const string Pattern = "   IPv4 Address. . . . . . . . . . . : ";
  const size_t Len = Pattern.length();

  istringstream iss( ipconfig_info );
  string line;

  while ( getline(iss, line) )
  {
    if ( line.find(Pattern) != string::npos)
    {
      return line.substr(Len);
    }
  }
  return string{};
}

[/update]
 
Share this answer
 
v3
Comments
Member 12899279 12-Feb-20 6:56am    
its returning nothing because you have used cin while all the data i posted above that i got from ipconfig is inside a string variable named output well name doesn't matter
CPallini 12-Feb-20 6:59am    
You may either
* redirect ipconfig as stdin to my program.
or
* use a istringstream instead of cin (just construct the istringstream object using your string as argument).
CPallini 12-Feb-20 7:37am    
I'm showing you the istringstream technique in my updated solution.
Maciej Los 12-Feb-20 7:27am    
5ed!
CPallini 12-Feb-20 7:37am    
Thank you, Maciej!
Personally, I think parsing the output from ipconfig is far too much of a hack when there are APIs that can be used. There are a few articles about that kind of thing at this site. Here is one you should take a look at : The "New ipconfig" and the IP Helper API[^]
 
Share this answer
 
We can't tell you exactly how to do it, because we haven't got any access to the actual string, and that going to be fundamental to what you need to do. Without the actual data, it's not possible to design any code that will 100% grab exactly what you need and nothing else - even with it, you probably need a good number of samples to work form to be sure that you are correctly isolating just the right information each time.

So start with your data, and look for the bit you want. Look to see if there is anything around it - preferably before and after that uniquely says "get this bit" and that isn't repeated anywhere that you don't get what you want.

Then consider looking at a Regular Expression (exactly how you use them depends on you development system / the environment the code will run in) and see if you can use one to isolate just the IP address. A basic IP address match in a regex would look like this:
\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}
But you may need to expand that depending on the rest of your data, as that will match any sequence of up to three digits separated by dots.

As a helper, get a copy of Expresso[^] - it's free, and it examines and generates Regular expressions.
 
Share this answer
 
Comments
k5054 12-Feb-20 6:23am    
Member 12899279 12-Feb-20 6:26am    
i have attached the string output now can you help me figure it out how to get ipv4address only?
OriginalGriff 12-Feb-20 6:38am    
No, because we don't know which of the IP addresses in there you want - and I can see two in there without even looking closely. And that's assuming that some of your other ones don't come online later.

Start by thinking about exactly what you are tryign to do - and why uyouj ar eusing Command outputs to do it when there are probably API ways to get exactly what you wanted anyway. Then look at your data and start thinking about what I said above.
Member 12899279 12-Feb-20 6:41am    
i have mentioned Ipv4 address th first one under wireless adapter lan wifi
Member 12899279 13-Feb-20 1:13am    
i cannot use api because it uses libraries like iphlpapi and that have some dependencies that i don't want anyways thanks
Thinking about it, regex is way too heavy for this. Here's an alternate solution that works with the c-strings returned from _popen()

C++
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>


int main()
{
    FILE *pipe = _popen("ipconfig", "r");
    
    if(pipe == NULL) {
        perror("popen");
        return 1;
    }

    char buffer[256];
    const char *MatchIPv4  = "IPv4 Address";

    std::vector<std::string> ipaddrs;  // store all the addresses

    while(fgets(buffer, sizeof buffer, pipe) != NULL) {

        char *ptr;   // temporary var for doing work

        // find the right input line
        if( (ptr = strstr(buffer, MatchIPv4)) != NULL) {

            // IP addr starts after the :
            ptr = strchr(ptr, ':');

            // first char of IP address is a digit, so find first digit
            size_t begin = strcspn(ptr, "0123456789");
            
            // IP address is first char not in set [0-9.]
            size_t length = strspn(ptr+begin, "0123456789.");

            // create a C++ string:
            ipaddrs.push_back(std::string(ptr+begin, length));
        }
    } 

    _pclose(pipe);

    for(auto  ipaddr : ipaddrs ) {
        std::cout << ipaddr << std::endl;
    }
        

    return 0;
}


This doesn't make any assumptions beyond
1) the lines of interest contain "IPv4 Address".
2) there's only one ':' which precedes the IP address

There's an even simpler solution, if you are willing to make the assumption that the output format of the output from ipconfig won't change - or at least won't change while your program is of use to you.

C++
if( strstr(buffer, MachIPv4) != NULL) {
     char *ptr = buffer + 39;       // start of IP address
     char *end = strchr(ptr, '\n'); // find terminating \n from fgets
     *end = '\0';                   // turn it into a nul byte
     ipaddrs.push_back(std::string(ptr));
}
 
Share this answer
 
Comments
Member 12899279 12-Feb-20 12:26pm    
i can't use popen as it opens cmd which i dont want to show not even for a split second
k5054 12-Feb-20 13:32pm    
Yeah, that's because one should not use _popen() in a windows desktop context. See the note near the bottom of this document _popen, _wpopen | Microsoft Docs[^] The follow the link at the end of the note about creating child processes in a windows program. Better yet, follow Rick York's advice and use the IP helper API.
Member 12899279 13-Feb-20 1:13am    
i cannot use api because it uses libraries like iphlpapi and that have some dependencies that i don't want anyways thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900