1. 程式人生 > >Linux下程式設計設定IP,閘道器

Linux下程式設計設定IP,閘道器

1.拼湊成指令實現:

#include <string.h>
#include <stdlib.h>

int main(void)
{
	char IP[24] = "192.168.1.225";
	char Mask[24] = "255.255.0.0";
	char gateway[24] = "10.240.0.0";
	char cmd_IP_Mask[64] = {0};
	char cmd_GW[64] = {0};
	sprintf(cmd_IP_Mask, "ifconfig eth0 %s netmask %s", IP, Mask);
	sprintf(cmd_GW, "route add default gw %s netmask %s", gateway);

	system(cmd_IP_Mask);
	system(cmd_GW);

	return 0;
}

2.從檔案中獲取IP地址,然後設定IP:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define FILE_NAME "ip.txt"
int main(void)
{
	FILE *fp;
	char IP[20];
	char Mask[20];
	char gateway[20];
	char cmd_IP_Set[64] = {0};
	char cmd_GW[64] = {0};
	if( (fp=fopen(FILE_NAME,"r"))==NULL )
  {
      printf("Open %s failed ! \n", FILE_NAME);
      exit(0);
  }
  fgets(IP, 20, fp);
  fgets(Mask, 20, fp);
  fgets(gateway, 20, fp);
  
  printf("IP : %s ", IP);
  printf("Mask : %s ", Mask);
  printf("gateway : %s \n", gateway);
  
  sprintf(cmd_IP_Set, "ifconfig eth0 %s", IP);
  system(cmd_IP_Set);
  sprintf(cmd_IP_Set, "ifconfig eth0 netmask %s", Mask);
  system(cmd_IP_Set);
	sprintf(cmd_IP_Set, "route add default gw %s", gateway);
	system(cmd_IP_Set);

return 0;
}


2.從檔案中獲取IP地址,檢測IP地址的合法性,然後設定IP:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>

#define FALSE 0
#define TRUE 1

#define FILE_NAME "ip.txt"

#if 1
int is_valid_ip_address(const char *addr)
{
    int num= 0;
    int i = 0, j = 0;
    int value;
    char buf[4] ;
    int count = 0;
	int k =0,m=0;
	char test_num[20] = {0};

	while ( *(addr + i) )
	{
		if ( *(addr + i) == '.' )
		{
			++count;
			if(((count == 3) && (strlen(addr + i)>6))
				||(count > 3))
			{
				printf(" count = %d ******** %s  %d\n", count, (addr + i),strlen(addr + i));
				return FALSE;
			}	
		} 
		++i;
	}

	i = j = value = count = 0;	
    memset(buf, 0, sizeof(buf));
    while ( *(addr + i) )
    {
    	++num;
		//printf("^^^^^^^^^^^^^^^^^^^^^ %d \n",*buf);
        while ((*(addr + i) != '.') && (i< 15))
        {
        		//printf("i = %d ",i);
        		printf("num = %d   %x \n",num, atoi(addr + i));

				if(*(addr + i) == 0x0d)           // windows.
				{
					if(*(addr + i+1) == 0x0a)
					{
						printf("*************\n");
						break;
					}
				}

				if(*(addr + i) == 0x0a)          // linux.
				{
					if(*(addr + i-1) != 0x0d)
					{
						printf("222*************\n");
						//++i;
						//continue;
						break;
					}
				}				
				
				/*if(num == 4)
				{
					sprintf(test_num,"%x", (addr + i));
					printf(">>>>>>> : %s \n" test_num);
				}*/
					//printf(" ******** %d  %s  %d\n", i, (addr + i),strlen(addr + i));
        		printf("@@@@@@@@@ :%s  \n",(addr + i));
            if (( ! isdigit(*(addr + i)) ) && (num != 4))
            {
            		printf("111111111\n");
					//printf("  %s  %d\n", (addr + i),strlen(addr + i));
                	return FALSE;
                
            }
            *(buf + j) = *(addr + i);
            ++i;
            ++j;
        }
        *(buf + j) = '\0';
		//if(num == 4)
		///{
			//printf("num = %d \n",num);
			//for(k=0;k<j;++k)
				//printf("## %02x ",*buf);
		//}
		printf("\n^^^^^^^^^^^ %s  %d \n",buf,strlen(buf));
        value = atoi(buf);

        if ( (value > 255) || (value < 0) )
        {
        	printf("222222222\n");
            return FALSE;    
        }
        
        if ( *(addr + i) == '.' )
        {
        	++count;
            printf("pinggle come here : %d  \n", count);	
			printf("i = %d \n",i);
            ++i;
            j = 0;
            memset(buf, 0, sizeof(buf));
            continue;
        }
        else
        {
            break;
        }
    }

	printf("buf = %s  %d \n",buf, strlen(buf));
	//m = strlen(buf)-2;
	m = strlen(buf);
	for(k=0;k<m;++k)
	{
		printf("%c ", buf[k]);
		printf("  chech ! \n");
		if ( ! isdigit(buf[k]) ) 
        {
        	printf("444: %d  \n",num);
            return FALSE;	
		}
	}
	return TRUE;

}
#endif

#if 0
int is_valid_ip_address(const char *str)
{
	printf("%s >>>>>>>>> \n",str);
#if 1
    struct in_addr addr;
    int ret;
    volatile int local_errno;

    errno = 0;
    ret = inet_pton(AF_INET, str, &addr);
    local_errno = errno;
    if (ret > 0)
        fprintf(stderr, "\"%s\" is a valid IPv4 address\n", str);
    else if (ret < 0)
        fprintf(stderr, "EAFNOSUPPORT: %s\n", strerror(local_errno));
    else 
        fprintf(stderr, "\"%s\" is not a valid IPv4 address\n", str);

    return ret;
#endif

}
#endif

int main(void)
{
	FILE *fp;
	char IP[20];
	char Mask[20];
	char gateway[20];
	char cmd_IP_Set[64] = {0};
	char cmd_Mask[64] = {0};
	char cmd_GW[64] = {0};
	if( (fp=fopen(FILE_NAME,"r"))==NULL )
  {
      printf("Open %s failed ! \n", FILE_NAME);
      exit(0);
  }
  fgets(IP, 20, fp);
  fgets(Mask, 20, fp);
  fgets(gateway, 20, fp);
  
  //printf("check : %d \n",is_valid_ip_address(IP));
  if(is_valid_ip_address(IP))
  {
  	  printf("IP address is Valid ! \n");
	  printf("IP : %s ", IP);
	  printf("Mask : %s ", Mask);
	  printf("gateway : %s \n", gateway);

	  sprintf(cmd_IP_Set, "ifconfig eth0 %s", IP);
	  system(cmd_IP_Set);
//	  printf("set ip return : %d \n",system(cmd_IP_Set));
	  sprintf(cmd_IP_Set, "ifconfig eth0 netmask %s", Mask);
	  system(cmd_IP_Set);
//	  printf("set netmask return : %d \n",system(cmd_IP_Set));
	  sprintf(cmd_GW, "route add default gw %s", gateway);
      system(cmd_GW);
//	  printf("set wg is %d \n",system(cmd_GW));
	  printf("Set IP address OK ! \n");
  }
  else
  {
  	printf("Error : IP address is Invalid ! \n");
	printf("Set IP address Failed ! \n");
  }

return 0;
}

說明:子網掩碼和閘道器未做檢查,當IP地址和閘道器不在同一網段時,IP設定正確,閘道器會設定失敗。這個與子網掩碼有關,但本程式還未實現此種情況的判斷,望後來者可以補上。