代码拉取完成,页面将自动刷新
#include <iterator>
#include <map>
#include <string>
#if 0
#include <iostream>
#include <cstring>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
int main() {
char hostname[256];
char ip[INET_ADDRSTRLEN];
struct hostent *host;
// 获取主机名
if (gethostname(hostname, sizeof(hostname)) == -1) {
perror("获取主机名失败");
return 1;
}
printf("hostname: %s\n", hostname);
// 根据主机名获取IP地址
if ((host = gethostbyname(hostname)) == NULL) {
perror("获取IP地址失败");
return 1;
}
// 遍历IP地址列表
for (int i = 0; host->h_addr_list[i] != NULL; i++) {
strcpy(ip, inet_ntoa(*(struct in_addr*)host->h_addr_list[i]));
std::cout << "IP地址: " << ip << std::endl;
}
return 0;
}
#endif
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
int get_ips(std::map<std::string, std::string>& ips)
{
struct ifaddrs *ifaddr, *ifa;
int family;
char host[NI_MAXHOST];
if (getifaddrs(&ifaddr) == -1) {
perror("getifaddrs");
exit(EXIT_FAILURE);
}
int num = 0;
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa->ifa_addr == NULL)
continue;
if (strcmp(ifa->ifa_name, "lo") == 0) {
continue;
}
family = ifa->ifa_addr->sa_family;
printf("family: %d\n", family);
printf("name: %s\n", ifa->ifa_name);
if (family == AF_INET) {
int s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
printf("getnameinfo n = %d\n", s);
if (s != 0) {
printf("getnameinfo() failed: %s\n", gai_strerror(s));
exit(EXIT_FAILURE);
}
printf("%s\n", host);
ips.insert(std::pair<std::string, std::string>(ifa->ifa_name, host));
num++;
}
}
freeifaddrs(ifaddr);
return num;
}
std::string get_one(int n)
{
std::map<std::string, std::string> ips;
int s = get_ips(ips);
if (n > s) {
return "0.0.0.0";
}
std::map<std::string, std::string>::iterator iter;
for (iter = ips.begin(); iter != ips.end(); iter++) {
// std::cout << "Net name: " << iter->first << ", ip: " << iter->second << std::endl;
}
std::map<std::string, std::string>::iterator it = ips.begin();
it = std::next(it, n);
if (it != ips.end()) {
return (*it).second;
}
return "0.0.0.0";
}
std::string get_one(const std::string name)
{
std::map<std::string, std::string> ips;
int s = get_ips(ips);
std::map<std::string, std::string>::iterator it;
it = ips.find(name);
if (it != ips.end()) {
return it->second;
}
return "0.0.0.0";
}
int main (int argc, char *argv[])
{
std::string ip0 = get_one(0);
std::cout << "IP0: " << ip0 << std::endl;
std::string ip1 = get_one(1);
std::cout << "IP1: " << ip1 << std::endl;
std::string ip2 = get_one(2);
std::cout << "IP2: " << ip2 << std::endl;
std::string ip6 = get_one(6);
std::cout << "IP6: " << ip6 << std::endl;
std::string ip00 = get_one("enp3s0");
std::cout << "enp3s0 IP: " << ip00 << std::endl;
std::string ip01 = get_one("wlo1");
std::cout << "wlo1 IP: " << ip01 << std::endl;
std::string ip02 = get_one("wlo1x");
std::cout << "wlo1x IP: " << ip02 << std::endl;
#if 0
int n = get_ips(ips);
printf("get_ips count: %d\n", n);
std::map<std::string, std::string>::iterator iter;
for (iter = ips.begin(); iter != ips.end(); iter++) {
std::cout << "Net name: " << iter->first << ", ip: " << iter->second << std::endl;
}
std::map<std::string, std::string>::iterator iter1= ips.begin();
// iter1 = iter1 + 1;
iter1 = std::next(iter1, 3);
if (iter1 != ips.end()) {
std::cout << "Net[0] name: " << iter1->first << ", ip: " << (*iter1).second << std::endl;
}
else {
std::cout << "std::map iterator end()" << std::endl;
}
#endif
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。