本文实例为大家分享了C语言+shell实现linux网卡状态检测的具体代码,供大家参考,具体内容如下
不解释,直接上代码 要求linux环境具备grep和awk(awk可选)
#include
#include
#include
#include
int get_if_status(char *if_name)
{
char buffer[BUFSIZ];
char cmd[100];
FILE *read_fp;
int chars_read;
int ret =0;
memset( buffer, 0, BUFSIZ );
memset( cmd, 0, 100 );
sprintf(cmd, "ifconfig -a | grep %s",if_name);
read_fp = popen(cmd, "r");
if ( read_fp != NULL )
{
chars_read = fread(buffer, sizeof(char), BUFSIZ-1, read_fp);
pclose(read_fp);
if (chars_read > 0)
{
ret = 1;
}
else
{
fprintf(stderr, "%s: NO FOUND\r\n",if_name);
return 0;
}
}
if(ret =