#include <stdio.h> #include <sys/types.h> #include <sys/param.h> #include <sys/ioctl.h> #include <sys/socket.h> #include <net/if.h> #include <netinet/in.h> #include <net/if_arp.h> #include <arpa/inet.h> #include <unistd.h> #include <string.h> static int get_mac(const char *nic_name, char *mac) { struct ifreq tmp; int sock_mac; char mac_addr[30]; sock_mac = socket(AF_INET, SOCK_STREAM, 0); if(sock_mac == -1) { perror("create socket fail\n"); return -1; } memset(&tmp, 0, sizeof(tmp)); strncpy(tmp.ifr_name, nic_name, sizeof(tmp.ifr_name)-1); if((ioctl(sock_mac, SIOCGIFHWADDR, &tmp)) < 0) { perror("mac ioctl error\n"); return -1; } sprintf(mac_addr, "%02X:%02X:%02X:%02X:%02X:%02X", (unsigned char)tmp.ifr_hwaddr.sa_data[0], (unsigned char)tmp.ifr_hwaddr.sa_data[1], (unsigned char)tmp.ifr_hwaddr.sa_data[2], (unsigned char)tmp.ifr_hwaddr.sa_data[3], (unsigned char)tmp.ifr_hwaddr.sa_data[4], (unsigned char)tmp.ifr_hwaddr.sa_data[5] ); // printf("%s:%s\n", nic_name, mac_addr); close(sock_mac); memcpy(mac, mac_addr, strlen(mac_addr)); printf("%s:%s\n", nic_name, mac); return 0; } static int get_ip(const char *eth_name, char *ip_addr) { int sock; struct sockaddr_in sin; struct ifreq ifr; sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock == -1) { perror("socket"); return -1; } memset(&ifr, 0, sizeof(ifr)); strncpy(ifr.ifr_name, eth_name, sizeof(ifr.ifr_name)); if (ioctl(sock, SIOCGIFADDR, &ifr) < 0) { perror("ioctl"); return -1; } memcpy(&sin, &ifr.ifr_addr, sizeof(sin)); close(sock); memcpy(ip_addr, inet_ntoa(sin.sin_addr), strlen(inet_ntoa(sin.sin_addr))); printf("%s:%s\n", eth_name, ip_addr); return 0; } /************************************************* Function: set_ip_addr Description: set ip addr Input: eth_name ip_addr Output: Return: 0 sucess -1 failed Others: *************************************************/ int set_ip(const char *eth_name, const char* ip_addr) { struct ifreq ifr; int fd = 0; struct sockaddr_in *paddr; if((NULL == eth_name) || (NULL == ip_addr)) { perror("illegal call function SetGeneralIP!"); return -1; } if ((fd = socket(AF_INET,SOCK_DGRAM,0)) < 0) { perror("socket....setip..false!!!"); return -1; } strcpy(ifr.ifr_name, eth_name); paddr = (struct sockaddr_in *)&(ifr.ifr_addr); bzero(paddr, sizeof(struct sockaddr_in)); paddr->sin_addr.s_addr = inet_addr(ip_addr); paddr->sin_family = AF_INET; if (ioctl(fd, SIOCSIFADDR, &ifr) < 0) { close(fd); perror("ioctl..set_ip_addr..false!!!"); return -1; } close(fd); return 0; } int main(int argc, char *argv[]) { char *eth_name = NULL; char mac_addr[100] = { 0 }; char ip_addr[100] = { 0 }; int ret = 0; if(!(argc == 2 || argc == 3)) { printf("Usage:\n"); printf("%s <ethx> [ip_addr]\n", argv[0]); return -1; } eth_name = argv[1]; strcpy(ip_addr, argv[2]); ret = set_ip(eth_name, ip_addr); if(0 != ret) { return -1; } ret = get_mac(eth_name, mac_addr); if(0 != ret) { return -1; } ret = get_ip(eth_name, ip_addr); if(0 != ret) { return -1; } printf("eth_name:%s \n""mac_addr:%s \n""ip_addr:%s \n", eth_name, mac_addr, ip_addr); return 0; }
linux下获取/设置mac和ip
最新推荐文章于 2024-04-14 19:40:01 发布