Last Update: Jul 11, 2021

Today’s sponsor is Depot. Depot is a remote container build service that’s half the cost of GitHub Actions runners and up to 40x faster than other build services. Try it for free.


If you’re like me, you constantly need your IP in Windows. When working with network interfaces and multiple servers, I find myself checking my IP a lot.

I built a little utility for myself that only shows the IP addresses on the host, rather than typing ipconfig and getting a long list, I can simply type “ip” and it will only show IP addresses.

C++ Version of the Tool

The following compiles with GCC for Windows, and probably a lot of other compilers as well. This utility is standalone and only needs to be copied into a folder within your path.

Create a file called ip.cpp and add the following:

#include <iostream>
#include <stdio.h>
#include <typeinfo>
using namespace std;

int main()
{
    FILE *dir;
    char direntry[80];

    dir = popen("ipconfig", "r");

    while (!feof(dir)) {

        fgets(direntry, sizeof(direntry), dir);

        string output = direntry;

        if ((output.find("IP") != string::npos)) {
            cout << direntry << "\n";
        }

    }
    return 0;
}

This simple executable will cut down your time by only showing the information you need at the time. I hope this helps someone.

C# Version of the Tool

I have also created a C# version of this tool, but it requires the .NET framework to be installed. Create a file called IP.cs with the following code:

using System;

namespace iptools
{
    class ip
    {

        static void Main(string[] args)
        {

            //Create process
            System.Diagnostics.Process pProcess = new System.Diagnostics.Process();

            //the command we want to run
            pProcess.StartInfo.FileName = "ipconfig";

            // we don't want this to output the original command
            pProcess.StartInfo.UseShellExecute = false;

            //Set output of program to be written to process output stream
            pProcess.StartInfo.RedirectStandardOutput = true;

            //Start the process
            pProcess.Start();

            //Get program output
            string strOutput = pProcess.StandardOutput.ReadToEnd();

            // split it by line
            string[] ourText = strOutput.Split('\n');

            Console.WriteLine("\n\n");

            foreach (string line in ourText)
            {
                if (line.Contains("IP"))
                {
                    Console.WriteLine(line + "\n");
                }
            }
			// close the process
            pProcess.Close();
        }
    }
}

Compile this file from the command line:

csc ip.cs

and it’s ready!

Summary

These files both provide a simple executable to output your ip address where you can read it quickly. If you have multiple adapters they will show up as well.

I hope this helps!


If you’d like to learn more, here’s a great C++ fundamentals course.

Advanced C++ developers wanting to level up should check out High-performance computing in C++



Today’s sponsor is Depot. Depot is a remote container build service that’s half the cost of GitHub Actions runners and up to 40x faster than other build services. Try it for free.



Published: Dec 12, 2012 by Jeremy Morgan. Contact me before republishing this content.