Linux Implementation Shell command supports IPv4 address translation

Source: Internet
Author: User
Tags strtok

I've been dealing with IP addresses for a few days. GdB comes out with a variable, go to the calculator to look at the binary, and then every eight bits to calculate the IP address, really patience. Searched the information on the Internet, and did not find a similar tool. Maybe everyone thinks it's too easy to write such a thing. but actually, Whether it is simple or not, it is a good command to actually produce a convenient command. Since there is no ready-made available, then simply write one yourself. There are no lines of code to count.

Let's talk about the twists and turns of the process. So simple a program, write out all kinds of problems.

1. The address's shaping representation is unsigned int. The Atoi function was called when the address translation was originally made. tried many times, the result is not right. Of course, there is the right time. Compare the differences, later discovered that the original Atoi the return value of int, When I pass the argument to Atoi to an integer range greater than almost 200 million, the accuracy begins to problem and the result is discarded. So there was a mistake.

2. The above problem is not good, change a train of thought. Use sscanf. However, there is no difference between the way and the above method, or it will be wrong. Found a half day to find. You should use%u instead of%d.

3. Use Strtok. The value obtained by default overrides the first parameter. That is, the first parameter of Strtok is a value-result parameter.

So thanks, I've finished. Supports bidirectional conversions. The code is as follows:

#include <unistd.h> #include <netinet/in.h> #include <sys/socket.h> #include <stdio.h># Include <stdlib.h> #include <string.h> #include <arpa/inet.h>int main (int argc, char * * argv) {    if ( ARGC < 2)    {        printf ("usage:unless-args\n");        return 0;    }    int isstr = 0;    int i = 0;    for (i = 0; i < strlen (argv[1]); i++)    {        if (argv[1][i] = = '. ')        {            isstr = 1;        }    }    if (isstr)    {        unsigned int ipnum = INET_ADDR (argv[1]);        printf ("%s:%u\n", argv[1], ipnum);    }    else    {        unsigned int ipnum;        char *ip = NULL;        struct IN_ADDR addr;        SSCANF (Argv[1], "%u", &ipnum);        Addr.s_addr = Ipnum;        ip = inet_ntoa (addr);        printf ("%u:%s \ n", Ipnum, IP);    }    return 0;}

You can use the method to compile an executable file and then put it in the/usr/bin directory. It can be used directly in the back. Remember to modify the executable permissions.

Sometimes, seemingly simple things, to achieve or there will be unexpected results. It is only through experience that we can draw conclusions. Remember the previous time to the HW written test, the pen question has not been made, not not, in my opinion is actually very simple. But it was wrong. Come back and tell GF. She says you just can't do it, it's not easy. Think about it.

Those who do not like chicken soup can look directly at the code, hoping to help you.

Linux Implementation Shell command supports IPv4 address translation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.