Centos检测网卡状态

不解释,直接上代码 要求linux环境具备grep和awk(awk可选)

万能的网络,通过getifaddrs可以大大减少编码量,获得
C语言实现linux网卡检测-改进版 同样的效果。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>

int main(int argc, char *argv[])
{
    struct ifreq ifreq;
    int sock = 0;
    char mac[32] = "";

    if(argc < 2){
        printf("Usage: ./main eth0");
        return 1;
    }

    sock = socket(AF_INET,SOCK_STREAM,0);
    if(sock < 0)
    {
        perror("error sock");
        return 2;
    }

    strcpy(ifreq.ifr_name,argv[1]);
    if(ioctl(sock,SIOCGIFHWADDR,&ifreq) < 0)
    {
        perror("error ioctl");
        return 3;
    }

    int i = 0;
    for(i = 0; i < 6; i++){
        sprintf(mac+3*i, "%02X:", (unsigned char)ifreq.ifr_hwaddr.sa_data[i]);
    }
    mac[strlen(mac) - 1] = 0;
    printf("MAC: %s\n", mac);

    return 0;
}

Centos检测网卡状态

由于ifconfig命令没法看到网卡的一些状态,
以下有5种方法查看网卡状态,是否连通网线

How to check physical Network Link Status on Linux (RHEL/Centos)
原文:

There are several ways to check Link status on RHEL,
As far i used to check by 5- ways. Refer Below

1)
# dmesg | grep eth
…..
e1000: eth0 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
e1000: eth1 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None

2)
# mii-tool
eth0: negotiated 100baseTx-FD, link ok
eth1: negotiated 100baseTx-FD, link ok

3)
# ethtool eth0 | grep Link
Link detected: yes

4)
# cat /sys/class/net/eth0/operstate
up

5)
# ip link show
2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000 link/ether
00:0c:29:b0:ef:e4 brd ff:ff:ff:ff:ff:ff
3: eth1: mtu 1500 qdisc pfifo_fast qlen 1000 link/ether
00:0c:29:b0:ef:ee brd ff:ff:ff:ff:ff:ff

由于ifconfig命令没法看到网卡的一些状态,
以下有5种方法查看网卡状态,是否连通网线 How to check physical Network
Link Stat…

今天发现个可以检测系统文件完整性的shell脚本,自己试了下还可以吧,介绍给大家。
系统:centos 5.x
脚本内容:

#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 == 1)
 {
  memset( buffer, 0, BUFSIZ );
  memset( cmd, 0, 100 );
  sprintf(cmd, "ifconfig |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 = 2;
      }
      else
      {
    fprintf(stderr, "%s: DOWN\r\n",if_name);
    return 1;
      }
  }
 }

 if(ret == 2)
 {
  memset( buffer, 0, BUFSIZ );
  memset( cmd, 0, 100 );
  sprintf(cmd, "ifconfig %s | grep RUNNING |  awk '{print $3}'",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)
      {
    fprintf(stderr, "%s: LINKED\r\n",if_name);
    return 3;
      }
      else
      {
    fprintf(stderr, "%s: UNPLUGGED\r\n",if_name);
    return 2;
      }
  }
 }

 return -1;
}


int main(int argc, char* argv[])
{
    int i=0;
 if(argc != 2)
 {
  fprintf(stderr, "usage: %s ", argv[0]);
  return -1;
 }

    i = get_if_status(argv[1]);
    printf( "if_status = %d\n", i );
    return 0;
}
#include 
#include 
#include 

#include 
#include 
#include 
#include 
#include  


int c_ifaddrs_netlink_status(const char *if_name )
{
 struct ifaddrs *ifa = NULL, *ifList;  

 if (getifaddrs(&ifList) < 0)
 {
  return -1;
 }

 for (ifa = ifList; ifa != NULL; ifa = ifa->ifa_next)  
 {
  if(ifa->ifa_addr->sa_family == AF_INET)  
  {
   if(strcmp(ifa->ifa_name, if_name) ==0)
   {
    if(!(ifa->ifa_flags & IFF_UP))
    {
     printf("DEVICE_DOWN\r\n");
     freeifaddrs(ifList);
     return 1;
    }

    if(!(ifa->ifa_flags & IFF_RUNNING))
    {
     printf("DEVICE_UNPLUGGED\r\n");
     freeifaddrs(ifList);
     return 2;
    }

    printf("DEVICE_LINKED\r\n");
    freeifaddrs(ifList);
    return 3;
   }
  }  
 }  

 printf(stderr, "DEVICE_NONE\r\n");
 freeifaddrs(ifList);
 return 0;
}

int main(int argc, char* argv[])
{
 int i=0;
 if(argc != 2)
 {
  fprintf(stderr, "usage: %s \r\n", argv[0]);
  return -1;
 }

 i = c_ifaddrs_netlink_status(argv[1]);

 fprintf(stderr,"c_ifaddrs_netlink_status if_status = %d\n", i );
 return 0;
}

 

复制代码 代码如下:

嵌入式编译 mips-linux-gnu-gcc -mips32 -EL -mhard-float -Wall -o netlink
netlink.c

getifaddrs
可以大大减少编码量,获得 C语言实现linux网卡检测-改进版 同样的效果。
#include #include #include #include #include #i…

输入:./mac eth0

cat my_filecheck.sh
#!/bin/bash
#
# 变量首先声明才能使用
shopt -s -o nounset
 
# 声明
 
# 建立日期
 
Date=$(date +’%Y%m%d%H%M%S’)
 
# 加入审核的目录         #
 
Dirs=”/bin /sbin /usr/bin /usr/sbin /lib /usr/local/sbin /usr/local/bin
/usr/local/lib”
 
# 临时文件               #
 
TMP_file=$(mktemp /tmp/check.XXXXXX)
 
# 文件checksum存储文件
FP=”/root/fp.$Date.chksum”
 
# 使用哪种checksum工具
Checker=”/usr/bin/md5sum”
Find=”/usr/bin/find”
 
# 函数区                #
 
scan_file() {
        local f
        for f in $Dirs
        do
                $Find $f -type f >> $TMP_file
        done
}
 
# 读取文件建立每个文件的checksum值
cr_checksum_list() {
        local f
        if [ -f $TMP_file ]; then
                for f in $(cat $TMP_file);
                        do
                                $Checker $f >> $FP
                done
        fi
}
rmTMP() {
        [ -f $TMP_file ] && rm -rf $TMP_file
}
 
 
# 主程序区
 
 
# 扫描列表
scan_file
 
# 建立文件的checksum值
cr_checksum_list
 
# 清理临时文件
rmTMP

测试结果

显示eth0的MAC地址.

执行脚本:

# ./netlink eth100
eth100: NO FOUND
if_status = 0
# 
# ifconfig eth0 down
# ./netlink eth0  
eth0: DOWN
if_status = 1
# 
# ifconfig eth0 up
# ./netlink eth0
eth0: UNPLUGGED
if_status = 2
#
# ./netlink eth0
eth0: LINKED
if_status = 3

复制代码 代码如下:

要求linux环境具备grep和awk(awk可选) #include #include #include
#include int get_if_status(char *if_name){ char
buffer[BUFSIZ];char cmd[100]…

./my_filecheck.sh

进行校验:

复制代码 代码如下:

md5sum  -c fp.20141205160628.chksum

如下:

复制代码 代码如下:

md5sum -c fp.20141209202544.chksum
/bin/gawk: OK
/bin/igawk: OK
/bin/ln: OK
/bin/loadkeys: OK
/bin/gzip: OK
/bin/mkdir: OK
/bin/date: OK
/bin/cat: OK
/bin/mountpoint: OK
/bin/taskset: OK
/bin/umount: OK
/bin/mount: OK
/bin/doexec: OK
/bin/kill: OK
/bin/sync: OK
/bin/unicode_start: OK
/bin/usleep: OK
/bin/mknod: OK
/bin/setserial: OK
/bin/cp: OK
/bin/mktemp: OK
/bin/setfont: OK
/bin/unicode_stop: OK
…..
/lib/modules/2.6.18-194.el5PAE/kernel/crypto/cast5.ko: OK
/lib/modules/2.6.18-194.el5PAE/kernel/crypto/crypto_blkcipher.ko: OK
/lib/modules/2.6.18-194.el5PAE/kernel/crypto/twofish.ko: OK
/lib/modules/2.6.18-194.el5PAE/kernel/crypto/authenc.ko: OK
/lib/modules/2.6.18-194.el5PAE/kernel/crypto/cbc.ko: OK
/lib/modules/2.6.18-194.el5PAE/kernel/crypto/crypto_hash.ko: OK
/lib/modules/2.6.18-194.el5PAE/kernel/crypto/seqiv.ko: OK
/lib/libproc-3.2.7.so: OK
/lib/libacl.so.1.1.0: OK
/lib/libauparse.so.0.0.0: OK
/lib/libdmraid.so.1.0.0.rc13-17: OK
/lib/libvolume_id.so.0.66.0: OK
/lib/libgobject-2.0.so.0.1200.3: OK
/lib/libnss_compat-2.5.so: OK
/lib/rtkaio/i686/nosegneg/librtkaio-2.5.so: OK
/lib/rtkaio/librtkaio-2.5.so: OK
/lib/libdmraid-events-isw.so.1.0.0.rc13: OK
/lib/libdevmapper-event-lvm2snapshot.so.2.02: OK
/lib/libblkid.so.1.0: OK
/lib/libdb-4.3.so: OK
/lib/libSegFault.so: OK
/lib/libiw.so.28: OK
/lib/libdmraid-events-isw.so.1.0.0.rc13-17: OK

可以看到很多OK.

如果只想看到错误的话,可以用下面这个命令:

复制代码 代码如下:

md5sum -c fp.20141209202544.chksum |grep -v “OK”

我的系统没有报错的,所以没有办法给大家结果.

相关文章