- 论坛徽章:
- 0
|
调用演示...
#include <stdio.h>\r\n#include <string.h>\r\nint main()\r\n{\r\n char gwIP[20]={0};\r\n get_gw_ip(gwIP);\r\n printf(\"%s\\n\",gwIP); \r\n return 0;\r\n}\r\n\r\nint get_gw_ip(char gwIP[])\r\n{\r\n FILE *fpr;\r\n char devicecfg[50];\r\n char *device=\"eth0\";\r\n sprintf(devicecfg,\"/etc/sysconfig/network-scripts/ifcfg-%s\",device);\r\n char buf[1024],*p;\r\n int i;\r\n \r\n memset(gwIP,0,sizeof(gwIP));\r\n \r\n if((fpr=fopen(devicecfg, \"r\"))==NULL)\r\n {\r\n printf(\"open device config failedn\");\r\n return -1;\r\n }\r\n memset(buf, 0, sizeof(buf));\r\n while(fgets(buf, sizeof(buf),fpr)!= NULL)\r\n {\r\n if(NULL == (p=strstr(buf,\"GATEWAY=\"))) continue;\r\n p=p+8;\r\n strncpy(gwIP,p,strlen(p)-1); \r\n break; \r\n }\r\n fclose(fpr);\r\n if(0==strlen(gwIP))\r\n return -1;\r\n else\r\n return 0;\r\n} | \n\n[ 本帖最后由 hackersea 于 2008-8-27 22:42 编辑 ] |
|