Jeremy Morgan

My Blog about Programming, Tech, SEO, Marketing and whatever else I come up with.

A Cleaner Display of Your IP in Windows


Author:


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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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!


Do you like articles like this?

I’m constantly hacking on stuff and writing about happenings in the programmer world. You can subscribe to my feed here, or you can get the programmer newsletter 100% spam free!

Comments