my_ipaddrshow

Talk is CHEAP, show me the CODE #1 - my_ipaddrshow

In this post, let us build a linux command named my_ipaddrshow.

Motivation

My kid is in summer vacation now and she copies me a lot and has now started using one of my laptops (for playing games). As the laptop has a lot of restriction (because of her paranoid parents), she often resorts to pester me to install stuff. And I have to go to her laptop and do the stuff. I sometimes want to ssh connect to her laptop but she would not know the ip address, so, I thought of creating a command to help her.

The existing command ip addr show is TOO MUCH for smallerkids/laymen. What will they ever do with that much amount of information?

We need to design the command in such a way that it is foolproof even for a child/layman.

Design

I could write a bash oneliner to parse the ip addr show for “UP” and print the appropriate ip addres and create an alias my_ipaddrshow, but it is really boring.

So, could we write a program to query the network device underneath (my wifi nic card) using “if_req” struct using IOCTL, SIOCGIFADDR, like below?

  struct ifreq if_req;
  int sock_fd;
  char if_name[] = "wlp0s20f3";
  
  sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (sock_fd == -1) {
    perror("socket");
    return 1;
  }

  //lets build ifreq
  //only ipv4
  if_req.ifr_addr.sa_family = AF_INET;
  strncpy(if_req.ifr_name, if_name, IFNAMSIZ - 1);

  if (ioctl(sock_fd, SIOCGIFADDR, &if_req) == -1) {
    perror("ioctl SIOCGIFADDR");
    close(sock_fd);
    return 1;
  }

  cout << if_name << endl;
  cout << inet_ntoa(((struct sockaddr_in *)&if_req.ifr_addr)->sin_addr) << endl;
  
  close(sock_fd);

We could, but it is still boring. Why is it boring? Because it still requires us to input the name of the wifi device.

Can we do that without giving the wifi device name? Now, this would make a wonderful interview question.

Yes, we can!

Programming Problem:

Write a program to print the ip address of the machine without using the interface name.

Solution:

Given Facts:

  • The computer is connected to a wifi network to access the internet, so definitely there is an ip address
  • The computer can ping any server hosted in the internet

With these information, we could definitely create a program that would print the ip address of the machine.

  sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (sock_fd == -1) {
    perror("socket");
    return 1;
  }

  //lets build an external inet address
  //only ipv4
  sockaddr_in remote_addr;
  remote_addr.sin_family = AF_INET;
  remote_addr.sin_port = htons(12345); // Arbitrary port

  if (inet_pton(AF_INET, "8.8.8.8", &remote_addr.sin_addr) <= 0) { // Example: Google DNS
    perror("inet_pton");
    close(sock_fd);
  }

  //lets connect to the external ip address
  if (connect(sock_fd, (struct sockaddr*)&remote_addr, sizeof(remote_addr)) == -1) {
    perror("connect");
    close(sock_fd);
    return 1;
  }

  //let us use getsockname() to get the local ip address
  sockaddr_in local_addr;
  socklen_t addr_len = sizeof(local_addr);
  if (getsockname(sock_fd, (struct sockaddr*)&local_addr, &addr_len) == -1) {
    perror("getsockname");
    close(sock_fd);
    return 1;
  }

  char ip_str[INET_ADDRSTRLEN];
  if (inet_ntop(AF_INET, &local_addr.sin_addr, ip_str, sizeof(ip_str)) == nullptr){
    perror("inet_ntop");
    close(sock_fd);
    return 1;
  }

  close(sock_fd); 

What do you think? Let me know your thoughts in the comments below!




Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • Worthy Words #4: Personality Traits
  • RANT: Truths about Social Media and AI
  • Justice for Indian Startups - QCommerce
  • My Hackathon Ideas - 2025
  • The Deepseek Wave