0

I have a multidimensional array containing IP addresses and their corresponding Gateways. The structure of the array is shown below. I'd like to loop through the array and assign the first column of IP addresses to my network interface. I expect the interface to at some point to have several IP Addresses assigned to it. Later I intend to add a function to ping the gateways.

I can't seem to get the value of just the first column with which to use to execute my command. I'm sure I'm referencing the array incorrectly. Here's what I have so far.

char eth0IntName[256];

const char *arrayIPAddress[3][2]={
    {"192.168.16.65", "192.168.16.66"},
    {"192.168.17.65", "192.168.17.66"},
    {"192.168.18.65", "192.168.18.66"}};
    
int main(int argc, char *argv[])
{

    char cmdStr[256];
    
    strcpy(eth0IntName, "eth0");
    
    for(int i=0; i<3; i++)
    {
        sprintf(cmdStr, "/sbin/ip addr add %s/30 dev %s", (char *)&arrayIPAddress[i][0], eth0IntName);
        printf("Executing: /sbin/ip addr add %s/30 dev %s", (char *)&arrayIPAddress[i][0], eth0IntName);
    }

    return 0;
    
}
2
  • 1
    What are the symptoms? What do you get? What is the difference to what you want? Commented Dec 10, 2020 at 18:10
  • On the sprintf line I'm passing in (char *)&arrayIPAddress[i][0]. For some reason this would be some weird gobbledy gook. I realized I didn't need to append the (char *)& portion and that cleaned it up. I also realized that I wasn't actually executing the command. I needed to add the line; system(cmdStr); before the printf line. Commented Dec 10, 2020 at 18:53

1 Answer 1

1

I was overcomplicating it. My sprintf line was wrong. Also, I needed to actually execute the command. I added system(cmdStr); after that line. Here's how the code looks now.

char eth0IntName[256];

const char *arrayIPAddress[3][2]={
    {"192.168.16.65", "192.168.16.66"},
    {"192.168.17.65", "192.168.17.66"},
    {"192.168.18.65", "192.168.18.66"}};
    
int main(int argc, char *argv[])
{

    char cmdStr[256];
    
    strcpy(eth0IntName, "eth0");
    
    for(int i=0; i<3; i++)
    {
        sprintf(cmdStr, "/sbin/ip addr add %s/30 dev %s", arrayIPAddress[i][0], eth0IntName);
        system(cmdStr);
        printf("Executing: /sbin/ip addr add %s/30 dev %s", arrayIPAddress[i][0], eth0IntName);
    }

    return 0;
    
}
Sign up to request clarification or add additional context in comments.

1 Comment

You may want to move the last printf line before the call to system. Also, you may want to add an fflush(stdout); after the printf but before the call to system, to make sure that the printf line has been put to the screen and is not waiting in the buffer, while the system function call is being executed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.