网卡事件监测并且发送邮件

来源:互联网 发布:中国金融数据库 编辑:程序博客网 时间:2024/06/06 02:24

网卡事件监测

看的别人的

#include <sys/types.h>  #include <sys/socket.h>  #include <asm/types.h>  #include <linux/netlink.h>  #include <linux/rtnetlink.h>  #include <stdlib.h>  #include <stdio.h>  #include <sys/ioctl.h>  #include <linux/if.h>  #include <string.h>  #define BUFLEN 20480  int main(int argc, char *argv[])  {      int fd, retval;      char buf[BUFLEN] = {0};      int len = BUFLEN;      struct sockaddr_nl addr;      struct nlmsghdr *nh;      struct ifinfomsg *ifinfo;      struct rtattr *attr;      fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);      setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &len, sizeof(len));      memset(&addr, 0, sizeof(addr));      addr.nl_family = AF_NETLINK;      addr.nl_groups = RTNLGRP_LINK;      bind(fd, (struct sockaddr*)&addr, sizeof(addr));      while ((retval = read(fd, buf, BUFLEN)) > 0)      {          for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, retval); nh = NLMSG_NEXT(nh, retval))          {              if (nh->nlmsg_type == NLMSG_DONE)                  break;              else if (nh->nlmsg_type == NLMSG_ERROR)                  return;              else if (nh->nlmsg_type != RTM_NEWLINK)                  continue;              ifinfo = NLMSG_DATA(nh);              printf("%u: %s", ifinfo->ifi_index,                      (ifinfo->ifi_flags & IFF_LOWER_UP) ? "up" : "down" );              attr = (struct rtattr*)(((char*)nh) + NLMSG_SPACE(sizeof(*ifinfo)));              len = nh->nlmsg_len - NLMSG_SPACE(sizeof(*ifinfo));              for (; RTA_OK(attr, len); attr = RTA_NEXT(attr, len))              {                  if (attr->rta_type == IFLA_IFNAME)                  {                      printf(" %s", (char*)RTA_DATA(attr));                      break;                  }              }              printf("\n");              # 监测到之后 发送地址到一个邮箱,主要用于            使用树莓派            char arg[100] = "/home/logan/sendmail.sh";            system(arg);            printf("send email success \n");          }      }      return 0;  } 

发送邮件

#!/bin/shifconfig  | grep -A 1 -iE "eth[0-9]|wlan[0-9]" | mailx -v -s "rap-ip"  740207611@qq.com

mailx 12.4 支持外部smtp协议,使用163邮箱可以不应ssl安全链接,比较方便

0 0