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设置正确,网关会设置失败。这个与子网掩码有关,但本程序还未实现此种情况的判断,望后来者可以补上。
发布了0 篇原创文章 · 获赞 15 · 访问量 17万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览