本文转自:
https://blog.csdn.net/qq_29344757/article/details/80004483
https://blog.csdn.net/qq_29344757/article/details/80004746
一、获取/设置Linux系统的网络信息
1.获取IP地址、子网掩码地址、Mac地址、广播地址
#include <stdio.h>
#include <string.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <errno.h>
int GetNetworkInfo(void)
{
int fd;
int interfaceNum = 0;
struct ifreq buf[16];
struct ifconf ifc;
struct ifreq ifrcopy;
char mac[32] = {0};
char ip[32] = {0};
char broadAddr[32] = {0};
char subnetMask[32] = {0};
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
perror("socket");
close(fd);
return -1;
}
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = (caddr_t)buf;
/* 执行完这句后ifc.ifc_len已经不一定就等于sizeof(buf),而是小于或等于sizeof(buf),取决于网卡的个数 */
if (!ioctl(fd, SIOCGIFCONF, (char *)&ifc))
{
interfaceNum = ifc.ifc_len / sizeof(struct ifreq);
while (interfaceNum-- > 0)
{
ifrcopy = buf[interfaceNum];
if (ioctl(fd, SIOCGIFFLAGS, &ifrcopy))
{
printf("ioctl: %s [%s:%d]\n", strerror(errno), __FILE__, __LINE__);
close(fd);
return -1;
}
//获取IP地址
if (!ioctl(fd, SIOCGIFADDR, (char *)&buf[interfaceNum]))
{
snprintf(ip, sizeof(ip), "%s",
(char *)inet_ntoa(((struct sockaddr_in *)&(buf[interfaceNum].ifr_addr))->sin_addr));
printf("device ip: %s\n", ip);
}
else
{
printf("ioctl: %s [%s:%d]\n", strerror(errno), __FILE__, __LINE__);
close(fd);
return -1;
}
//获取子网掩码地址
if (!ioctl(fd, SIOCGIFNETMASK, &buf[interfaceNum]))
{
snprintf(subnetMask, sizeof(subnetMask), "%s",
(char *)inet_ntoa(((struct sockaddr_in *)&(buf[interfaceNum].ifr_netmask))->sin_addr));
printf("device subnetMask: %s\n", subnetMask);
}
else
{
printf("ioctl: %s [%s:%d]\n", strerror(errno), __FILE__, __LINE__);
close(fd);
return -1;
}
//获取mac地址
if (!ioctl(fd, SIOCGIFHWADDR, (char *)(&buf[interfaceNum])))
{
memset(mac, 0, sizeof(mac));
snprintf(mac, sizeof(mac), "%02x:%02x:%02x:%02x:%02x:%02x",
(unsigned char)buf[interfaceNum].ifr_hwaddr.sa_data[0],
(unsigned char)buf[interfaceNum].ifr_hwaddr.sa_data[1],
(unsigned char)buf[interfaceNum].ifr_hwaddr.sa_data[2],
(unsigned char)buf[interfaceNum].ifr_hwaddr.sa_data[3],
(unsigned char)buf[interfaceNum].ifr_hwaddr.sa_data[4],
(unsigned char)buf[interfaceNum].ifr_hwaddr.sa_data[5]);
printf("device mac: %s\n", mac);
}
else
{
printf("ioctl: %s [%s:%d]\n", strerror(errno), __FILE__, __LINE__);
close(fd);
return -1;
}
//获取广播地址
if (!ioctl(fd, SIOCGIFBRDADDR, &buf[interfaceNum]))
{
snprintf(broadAddr, sizeof(broadAddr), "%s",
(char *)inet_ntoa(((struct sockaddr_in *)&(buf[interfaceNum].ifr_broadaddr))->sin_addr));
printf("device broadAddr: %s\n", broadAddr);
}
else
{
printf("ioctl: %s [%s:%d]\n", strerror(errno), __FILE__, __LINE__);
close(fd);
return -1;
}
}
}
else
{
printf("ioctl: %s [%s:%d]\n", strerror(errno), __FILE__, __LINE__);
close(fd);
return -1;
}
close(fd);
return 0;
}
2.设置IP地址、子网掩码、网关地址
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <string.h>
#include <net/route.h>
#include <unistd.h>
#include <stdio.h>
int SetNetworkInfo(const char *ifname, const char *ipaddr, const char *netmask, const char *gwip)
{
struct sockaddr_in sin;
struct ifreq ifr;
int fd;
bzero(&ifr, sizeof(struct ifreq));
if (!ifname == NULL || !ipaddr || !gwip) return -1;
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd == -1)
{
perror("Not create network socket connection\n");
return -1;
}
//设置IP地址
strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
ifr.ifr_name[IFNAMSIZ - 1] = 0;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(ipaddr);
memcpy(&ifr.ifr_addr, &sin, sizeof(sin));
if (ioctl(fd, SIOCSIFADDR, &ifr) < 0)
{
perror( "Not setup interface\n ");
return -1;
}
//设置子网掩码
bzero(&ifr, sizeof(struct ifreq));
strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
ifr.ifr_name[IFNAMSIZ - 1] = 0;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(netmask);
memcpy(&ifr.ifr_addr, &sin, sizeof(sin));
if(ioctl(fd, SIOCSIFNETMASK, &ifr ) < 0)
{
perror("net mask ioctl error\n");
return -1;
}
//设置网关地址
struct rtentry rm;
bzero(&rm, sizeof(struct rtentry));
rm.rt_dst.sa_family = AF_INET;
rm.rt_gateway.sa_family = AF_INET;
rm.rt_genmask.sa_family = AF_INET;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(gwip);
memcpy(&rm.rt_gateway, &sin, sizeof(sin));
rm.rt_dev = (char *)ifname;
rm.rt_flags = RTF_UP | RTF_GATEWAY ;
if(ioctl(fd, SIOCADDRT, &rm ) < 0)
{
perror("gateway ioctl error\n");
return -1;
}
//激活接口
ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0)
{
perror("SIOCSIFFLAGS");
return -1;
}
return 0;
}
二、编程获取cpu、内存和硬盘的使用率
1. 获取cpu使用率
#include <stdio.h>
#include <unistd.h>
/* cpu_info_t结构体存放cpu相关信息 */
typedef struct _cpu_info
{
char name[20];
unsigned int user;
unsigned int nice;
unsigned int system;
unsigned int idle;
unsigned int iowait;
unsigned int irq;
unsigned int softirq;
}cpu_info_t;
/* 从/proc/stat文件中获取cpu的相关信息 */
void get_cpu_occupy(cpu_info_t* info)
{
FILE* fp = NULL;
char buf[256] = {0};
fp = fopen("/proc/stat", "r");
fgets(buf, sizeof(buf), fp);
sscanf(buf, "%s %u %u %u %u %u %u %u", info->name, &info->user, &info->nice,
&info->system, &info->idle, &info->iowait, &info->irq, &info->softirq);
fclose(fp);
}
/* 计算cpu的使用率 */
double calc_cpu_rate(cpu_info_t* old_info, cpu_info_t* new_info)
{
double od, nd;
double usr_dif, sys_dif, nice_dif;
double user_cpu_rate;
double kernel_cpu_rate;
od = (double)(old_info->user + old_info->nice + old_info->system + old_info->idle + old_info->iowait + old_info->irq + old_info->softirq);
nd = (double)(new_info->user + new_info->nice + new_info->system + new_info->idle + new_info->iowait + new_info->irq + new_info->softirq);
if (nd - od)
{
user_cpu_rate = (new_info->user - old_info->user) / (nd - od) * 100;
kernel_cpu_rate = (new_info->system - old_info->system) / (nd - od) * 100;
return user_cpu_rate + kernel_cpu_rate;
}
return 0;
}
int main(void)
{
cpu_info_t info1;
cpu_info_t info2;
get_cpu_occupy(&info1);
sleep(2); /* 休息2s以统计cpu使用率 */
get_cpu_occupy(&info2);
printf("CPU的使用率: %.2lf%\n", calc_cpu_rate(&info1, &info2));
return 0;
}
2. 获取内存使用率.
#include <stdio.h>
typedef struct _mem_info_t
{
char name[20];
unsigned long total;
char name2[20];
}mem_info_t;
void get_memory_occupy()
{
FILE* fp = NULL;
char buf[256] = {0};
mem_info_t info;
double mem_total, mem_used_rate;
fp = fopen("/proc/meminfo", "r");
fgets(buf, sizeof(buf), fp);
sscanf(buf, "%s %lu %s\n", info.name, &info.total, &info.name2);
mem_total = info.total;
fgets(buf, sizeof(buf), fp);
sscanf(buf, "%s %lu %s\n", info.name, &info.total, &info.name2);
mem_used_rate = (1 - info.total / mem_total) * 100;
mem_total = mem_total / (1024 * 1024); //KB -> GB
printf("内存大小为: %.0lfG, 占用率为: %.2lf\n", mem_total, mem_used_rate);
}
int main(void)
{
get_memory_occupy();
return 0;
}
3.获取磁盘使用率
#include <stdio.h>
void get_hard_drive_rate()
{
FILE* fp = NULL;
int h = 0;
char buf[80], a[80], d[80], e[80], f[80];
double b, c;
double total = 0, used = 0;
fp = popen("df", "r");
fgets(buf, 256, fp);
while (6 == fscanf(fp, "%s %lf %lf %s %s %s", a, &b, &c, d, e, f))
{
total += b;
used += c;
}
pclose(fp);
printf("磁盘总大小: %.4lfG, 使用率: %.2lf%\n", total / (1024 * 1024), used / total * 100);
}
int main(void)
{
get_hard_drive_rate();
return 0;
}