Linux下获取系统的IP,子网掩码,网关,MAC和配置文件的修改

一 获取Linux平台下的配置参数,如IP,子网掩码,GateWay和Mac地址

int GetComputerInfo(char *ip,char *zwym,char*brdaddr,char *mac)
{
	/*socket参数设置*/
	int sock; struct sockaddr_in sin; struct ifreq ifr;
	sock = socket(AF_INET, SOCK_DGRAM, 0);
	if (sock == -1)
	{
		perror("socket");
		return -1;     
	}  

	/*这个是网卡的标识符*/
	strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
	ifr.ifr_name[IFNAMSIZ - 1] = 0; 
   
	/*开辟用于保存即将获取参数的空间*/
	ip = (char*)malloc(sizeof(inet_ntoa(sin.sin_addr)));
	zwym = (char*)malloc(sizeof(inet_ntoa(sin.sin_addr)));
	mac = (char*)malloc(sizeof(inet_ntoa(sin.sin_addr)));
	brdaddr = (char*)malloc(sizeof(inet_ntoa(sin.sin_addr))); 
	
	/*获取ip*/
	if (ioctl(sock, SIOCGIFADDR, &ifr) < 0) 
	{
		perror("ioctl");
		return -1;
	}
	memcpy(&sin, &ifr.ifr_addr, sizeof(sin));
	sprintf(ip,"%s/n",inet_ntoa(sin.sin_addr));  
	
	/*获取子网掩码*/
	if (ioctl(sock, SIOCGIFNETMASK, &ifr)< 0) 
	{
		perror("ioctl");
		return -1;
	}
	memcpy(&sin, &ifr.ifr_addr, sizeof(sin));  
	sprintf(zwym,"%s/n",inet_ntoa(sin.sin_addr));
	
	/*获取网关*/               
	ioctl(sock,SIOCGIFBRDADDR,&ifr);
	memcpy(&sin, &ifr.ifr_addr, sizeof(sin));
	sprintf(brdaddr,"%s/n",inet_ntoa(sin.sin_addr)); 
	
	/*获取mac*/
	if (ioctl(sock, SIOCGIFHWADDR, &ifr)< 0)
	{        
		perror("ioctl");
        return -1;
    }
	memcpy(&sin, &ifr.ifr_addr, sizeof(sin)); 
	sprintf(mac,"%s/n",inet_ntoa(sin.sin_addr));
	
	return 0;
}

附录:

int main()
{
	int sfd;
	struct ifconf interface;
	struct ifreq ifreq1;
	struct sockaddr_in* addr = NULL;
	unsigned char mac[6];
	unsigned char ip[4];
	unsigned char brdaddr[4];
	unsigned char netmask[4],netmask1[4];
	struct in_addr myip;
	sfd = socket(PF_INET,SOCK_DGRAM,0);


	strcpy(ifreq1.ifr_name,"eth0");//eth0位网卡名,也可搜索得到本地网卡名称
	ioctl(sfd,SIOCGIFHWADDR,&ifreq1);

	memcpy(mac,ifreq1.ifr_hwaddr.sa_data,6);
	ioctl(sfd,SIOCGIFADDR,&ifreq1);

	addr = (struct sockaddr_in*)&ifreq1.ifr_addr;

	//printf("%x/n", addr->sin_addr.s_addr);
	*(u_int32_t *)ip = addr -> sin_addr.s_addr;
	ioctl(sfd,SIOCGIFBRDADDR,&ifreq1);
	*(u_int32_t *)brdaddr = addr -> sin_addr.s_addr;

	ioctl(sfd,SIOCGIFNETMASK,&ifreq1);
	*(u_int32_t *)netmask = addr -> sin_addr.s_addr; 

	printf("MAC:%02X:%02X:%02X:%02X:%02X:%02X/n",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
	printf("IP ADDR:%d.%d.%d.%d/n",ip[0],ip[1],ip[2],ip[3]);
	printf("GATEWAY:%d.%d.%d.%d/n",brdaddr[0],brdaddr[1],brdaddr[2],brdaddr[3]);
	printf("NETMASK:%d.%d.%d.%d/n",netmask[0],netmask[1],netmask[2],netmask[3]);

	return 0;
}

二 修改Linux系统配置文件的参数

void SaveInfo(char*filename,struct node *head) 
{
	FILE* fp; /*文件指针*/
	struct  node *p; /*数据链表头*/
	int flag=1, count=0;  /*flag为是否保存成功标志,count为保存数据数目*/      
	fp = fopen(filename,"w");  /*为输出打开一个二进制的文件*/

	if(fp == NULL) 
	{         
		printf("\n=====>提示:重新打开文件时发生错误!\n");                    
		exit(1);
	}

	/*将p连接在head的后面*/
	p = head;

	/*判断p是否为空,以便进行循环*/
	while(p)
	{
		/*把p中的内容写到fp中*/
		if(fwrite(p,sizeof(struct node) – 4 , 1, fp) == 1) 
		{
			p=p->next; /*p向后移动一*/
			count++;/*用于统计保存书籍记录的数目*/
		}
		/*如果fwrite(p,sizeof(struct book),1,fp)返回值不是一,提前跳出循环*/
		else 
		{ 
			flag=0;                        
			break;
		}
	}

	if(flag == 1)
	{
		   printf("\n=====>提示:文件保存成功.(有%d条记录已经保存).\n",count); 
	}  
	
	fclose(fp); //关闭fp文件 
}

int  ModifyInfo(char *ip_addr,char*netmask_addr,char*brdaddr_addr,char*mac_addr)
{ 
	/*配置文件里面的9条数据,处理模式*/
	struct node *head,*p[9];
 
	/*配置文件的路径*/
	char *filename = "/etc/sysconfig/network-scripts/ifcfg-eth0";
 
	/*为保存配置文件相关数据开辟空间*/
	int i;
	for(i= 0;i < 9 ;i ++)
	{
		p[i] = (node *)malloc(sizeof(node));
    }
      
	/*把配置文件数据,做成链表,头存放在head里面*/
    head = p[0];
 
	/*即将要处理的配置文件*/
	memset(p[0]->name, 0, N);
	strcpy(p[0]->name  , "#  Advanced Micro Devices[AMD");//28
	memset(p[0]->value, 0, N);
	strcpy(p[0]->value , "]   79c970 [PCnet32 LANCE    ");//28
	memset(p[1]->name, 0, N);
	strcpy(p[1]->name  , "\nDEVICE=                    ");
	memset(p[1]->value, 0, N);
	strcpy(p[1]->value,  "eth0                         ");
	memset(p[2]->name, 0, N);
	strcpy(p[2]->name ,  "\nBOOTPROT=                  ");
	memset(p[2]->value, 0, N);
	strcpy(p[2]->value , "static                       ");
	memset(p[7]->name, 0, N);
	strcpy(p[7]->name ,  "\nONBOOT=                    ");
	memset(p[7]->value, 0, N);
	strcpy(p[7]->value,  "yes                          ");
	memset(p[8]->name, 0, N);
	strcpy(p[8]->name ,  "\nTYPE=                      ");
	memset(p[8]->value, 0, N);
	strcpy(p[8]->value, "Ethernet                     ");
	
	/*修改的IP,子网掩码,网关和MAC*/
 
	/*IP*/
	memset(p[3]->name, 0, N);
	strcpy(p[3]->name , "\nIPADDR=");    
	for(i = 8;i < N ;i ++)
		p[3]->name[i] = ' ';
	memset(p[3]->value, 0, N);
	for( i = 0; ip_addr[i] != '\0'; i ++)
		p[3]->value[i] = ip_addr[i];
	for(j = i;j < N;j ++)
		p[3]->value[j] = ' ';
    
	/*子网掩码*/
	memset(p[4]->name,0,N);
	strcpy(p[4]->name ,"\nNETMASK=");
	for(i = 9; i < N; i ++)
		p[4]->name[i] = ' ';

	memset(p[4]->value,0,N);
	for( i = 0; netmask_addr[i] != '\0'; i ++)
		p[4]->value[i] = netmask_addr[i];
	for(j = i;j < N; j ++)
       p[4]->value[j] = ' ';    
 
	/*网关*/
	memset(p[5]->name, 0, N);
	strcpy(p[5]->name , "\nGATEWAY=");
	for(i = 9; i < N; i ++)
		p[5]->name[i] = ' ';
    memset(p[5]->value, 0, N);
    for( i = 0; brdaddr_addr[i] != '\0'; i ++)
	    p[5]->value[i] = brdaddr_addr[i];
    for(j = i;j < N; j ++)
		p[5]->value[j] = ' ';

	/*MAC*/
	memset(p[6]->name, 0, N);
	strcpy(p[6]->name , "\nHWADDR=");
	for(i = 8; i < N; i ++)
		p[6]->name[i] = ' ';
	memset(p[6]->value, 0, N);
	for(i = 0; mac_addr[i] != '\0'; i ++)
		p[6]->value[i] = mac_addr[i];
	for(j = i; j < N; j ++)
		p[6]->value[j] = ' '; 
 
	/*把数据做成链表连接起来*/
	p[0]->next = p[1];
	p[1]->next = p[2];p[2]->next = p[3];p[3]->next = p[4];p[4]->next = p[5];
	p[5]->next = p[6];p[6]->next = p[7];p[7]->next = p[8];p[8]->next = NULL;
 
	/*保存配置文件*/
	SaveInfo(filename,head);
 
	/*成功则返回1*/
	return 1;
}





你可能感兴趣的:(Linux)