1. 程式人生 > 其它 >openwrt獲取info原始碼

openwrt獲取info原始碼

static char *getipolloinfo(__maybe_unused struct api_data *root)
{
	FILE *fstream=NULL;  
	char ipaddr[16];
	char macaddr[18];
	char gateway[16];
	char netmask[16];
	char fpgaversion[6];
	for(int i=0;i<5;i++)
	{
		switch(i)
		{
			case 0:
				memset(ipaddr,0,sizeof(ipaddr));
				if(NULL==(fstream=popen("ifconfig br-lan | grep \"inet addr\" | awk '{ print $2}' | awk -F: '{print $2}'| tr -d ['\n']","r")))  
				{ 
					applog(LOG_WARNING,"get ip addr execute command failed\n"); 
					continue;  
				} 
				if(NULL!=fgets(ipaddr, sizeof(ipaddr), fstream)) 
				{ 
					applog(LOG_WARNING,"ipaddr=%s\n",ipaddr);

				} 
				else
				{
					continue;
				}
			break;
			case 1:
				memset(macaddr,0,sizeof(macaddr));
				if(NULL==(fstream=popen("cat /sys/class/net/br-lan/address| tr -d ['\n']","r")))  
				{ 
					applog(LOG_WARNING,"get mac addr execute command failed\n"); 
					continue;  
				} 
				if(NULL!=fgets(macaddr, sizeof(macaddr), fstream)) 
				{ 
					applog(LOG_WARNING,"macaddr=%s\n",macaddr);
				} 
				else
				{
					continue;
				}
			break;
			case 2:
				memset(gateway,0,sizeof(gateway));
				if(NULL==(fstream=popen("netstat -rn|grep br-lan|awk '{print $2}'|awk 'NR==1'| tr -d ['\n']","r")))  
				{ 
					applog(LOG_WARNING,"get gateway execute command failed\n"); 
					continue;  
				} 
				if(NULL!=fgets(gateway, sizeof(gateway), fstream)) 
				{ 
					applog(LOG_WARNING,"gateway=%s\n",gateway);
				} 
				else
				{
					continue;
				}
			break;
			case 3:
				memset(netmask,0,sizeof(netmask));
				if(NULL==(fstream=popen("ifconfig br-lan | grep \"Mask\" | awk '{ print $4}' |awk -F: '{print $2}'| tr -d ['\n']","r")))  
				{ 
					applog(LOG_WARNING,"get Netmask execute command failed\n"); 
					continue;  
				} 
				if(NULL!=fgets(netmask, sizeof(netmask), fstream)) 
				{ 
					applog(LOG_WARNING,"netmask=%s\n",netmask);
				} 
				else
				{
					continue;
				}
			break;
			case 4:
				memset(fpgaversion,0,sizeof(fpgaversion));
				if(NULL==(fstream=popen("cat /tmp/fpgaversion| tr -d ['\n']","r")))  
				{ 
					applog(LOG_WARNING,"get fpga version command failed\n"); 
					continue;  
				} 
				if(NULL!=fgets(fpgaversion, sizeof(fpgaversion), fstream)) 
				{ 
					applog(LOG_WARNING,"fpgaversion=%s\n",fpgaversion);
				} 
				else
				{
					continue;
				}

			break;	
		}
		
		pclose(fstream);
	}

	root = api_add_string(root, "IP", ipaddr, true);
	root = api_add_string(root, "Gateway", gateway, true);
	root = api_add_string(root, "Netmask", netmask, true);
	root = api_add_string(root, "MAC", macaddr, true);
	root = api_add_string(root, "FPGA Version", fpgaversion, true);
	 return (char *)root;
	
}