我对IPv6协议知之甚少,很抱歉,这个问题听起来很愚蠢.

当我检索网络中所有IPv6地址的列表时,我得到一个名为scope的字段,如下所示:

inet6 addr: 2001:470:1:82::11/64 Scope:Global

inet6 addr: 2001:470:1:82::10/64 Scope:Global

inet6 addr: 2001:470:1:82::13/64 Scope:Global

inet6 addr: fe80::21d:9ff:fe69:2c50/64 Scope:Link

inet6 addr: 2001:470:1:82::12/64 Scope:Global

inet6 addr: 2001:470:1:82::15/64 Scope:Global

inet6 addr: 2001:470:1:82::14/64 Scope:Global

inet6 addr: 2001:470:1:82::5/64 Scope:Global

inet6 addr: 2001:470:1:82::17/64 Scope:Global

inet6 addr: 2001:470:1:82::6/64 Scope:Global

inet6 addr: 2001:470:1:82::16/64 Scope:Global

inet6 addr: 2001:470:1:82::7/64 Scope:Global

inet6 addr: 2001:470:1:82::19/64 Scope:Global

inet6 addr: 2001:470:1:82::8/64 Scope:Global

inet6 addr: 2001:470:1:82::18/64 Scope:Global

inet6 addr: 2001:470:1:82::9/64 Scope:Global

inet6 addr: 2001:470:1:82::1b/64 Scope:Global

inet6 addr: 2001:470:1:82::a/64 Scope:Global

inet6 addr: 2001:470:1:82::1a/64 Scope:Global

inet6 addr: 2001:470:1:82::b/64 Scope:Global

inet6 addr: 2001:470:1:82::1d/64 Scope:Global

inet6 addr: 2001:470:1:82::c/64 Scope:Global

inet6 addr: 2001:470:1:82::1c/64 Scope:Global

inet6 addr: 2001:470:1:82::d/64 Scope:Global

inet6 addr: 2001:470:1:82::1f/64 Scope:Global

inet6 addr: 2001:470:1:82::e/64 Scope:Global

inet6 addr: 2001:470:1:82::1e/64 Scope:Global

inet6 addr: 2001:470:1:82::f/64 Scope:Global

inet6 addr: ::1/128 Scope:Host

在我的应用程序中,我需要获取范围为“链接”的那些地址.我可以使用系统调用ifconfig然后解析输出以提取相应的地址.但问题是,我正在使用getifaddrs()调用,它返回结构ifaddr的链表,给出如下:

struct ifaddrs {

struct ifaddrs *ifa_next; /* Next item in list */

char *ifa_name; /* Name of interface */

unsigned int ifa_flags; /* Flags from SIOCGIFFLAGS */

struct sockaddr *ifa_addr; /* Address of interface */

struct sockaddr *ifa_netmask; /* Netmask of interface */

union {

struct sockaddr *ifu_broadaddr;

/* Broadcast address of interface */

struct sockaddr *ifu_dstaddr;

/* Point-to-point destination address */

} ifa_ifu;

#define ifa_broadaddr ifa_ifu.ifu_broadaddr

#define ifa_dstaddr ifa_ifu.ifu_dstaddr

void *ifa_data; /* Address-specific data */

};

问题是:如何从此列表中获取具有“链接”范围的地址?

解决方法:

一种方法是检查地址是否在fe80 :: / 10范围内. IPv6 address space可从IANA获取,详细说明了可用的范围.

我将源代码下载到net-tools(ifconfig的源包),它们看起来像是解析/ proc / net / if_inet6. (注释是我自己在以下代码中添加的内容 – 以下内容也非常简略,并且肯定不会编译.)

/* some defines collected around the place: */

#define _PATH_PROCNET_IFINET6 "/proc/net/if_inet6"

#define IPV6_ADDR_LOOPBACK 0x0010U

#define IPV6_ADDR_LINKLOCAL 0x0020U

#define IPV6_ADDR_SITELOCAL 0x0040U

#define IPV6_ADDR_COMPATv4 0x0080U

int scope; /* among other stuff */

/* looks like here we parse the contents of /proc/net/if_inet6: */

if ((f = fopen(_PATH_PROCNET_IFINET6, "r")) != NULL) {

while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n",

addr6p[0], addr6p[1], addr6p[2], addr6p[3],

addr6p[4], addr6p[5], addr6p[6], addr6p[7],

&if_idx, &plen, &scope, &dad_status, devname) != EOF) {

/* slightly later: */

printf(_(" Scope:"));

switch (scope) {

case 0:

printf(_("Global"));

break;

case IPV6_ADDR_LINKLOCAL:

printf(_("Link"));

break;

case IPV6_ADDR_SITELOCAL:

printf(_("Site"));

break;

case IPV6_ADDR_COMPATv4:

printf(_("Compat"));

break;

case IPV6_ADDR_LOOPBACK:

printf(_("Host"));

break;

default:

printf(_("Unknown"));

}

那么让我们来看看上面解析的内容:

$cat /proc/net/if_inet6

20010db8000008000000000000000001 03 40 00 00 eth0

fe800000000000000000000000004321 03 40 20 80 eth0

00000000000000000000000000000001 01 80 10 80 lo

因此,您可以看到左侧的第三列(0x00 Global,0x20 Link-Local和0x10 Loopback)是范围.使用网络工具代码中的上述常量,您可以找出它们的含义.需要进一步调查以确定这些常量的更权威的来源,以及解析/ proc / net / if_inet6是否是您的最佳选择.

标签:bash,linux,ip-address,ipv6

来源: https://codeday.me/bug/20190714/1461406.html

linux获取其他主机ipv6,linux – 如何获取IPv6主机的范围?相关推荐

  1. linux下ipv6地址的获取

    首先确认所使用的网络支持ipv6,装载的linux系统支持ipv6 网络设置里设置ipv6为"自动-DHCP"模式 根据得到的ipv6地址(2001::打头的是公用,fe80::是 ...

  2. linux c 获取mac地址吗,Linux系统下用C语言获取MAC地址

    最近在做一个小程序,需要用到在linux系统里编写C程序从而获取MAC地址,从网上搜了一遍,想总结一下.如果你就只需要单个功能的程序,可以采用方法一,见代码1,一般最好能够封装起来,写成获取MAC地址 ...

  3. 【小沐学C++】C++获取计算机硬件信息(Linux)

    C++获取计算机硬件信息(Windows) https://blog.csdn.net/hhy321/article/details/121258036 C++获取计算机硬件信息(Linux) htt ...

  4. python使用psutil获取系统(Windows Linux)所有运行进程信息实战:CPU时间、内存使用量、内存占用率、PID、名称、创建时间等;

    python使用psutil获取系统(Windows Linux)所有运行进程信息实战:CPU时间.内存使用量.内存占用率.PID.名称.创建时间等: psutil模块可以跨平台使用,支持Linux/ ...

  5. Atitit. 软件GUIbutton与仪表盘--webserver区--获取apache配置文件路径 linux and apache的启动、停止、重新启动...

    Atitit.   软件GUIbutton与仪表盘--webserver区--获取apache配置文件路径 linux and apache的启动.停止.重新启动 能够通过"netstat  ...

  6. linux脚本参数获取时间戳,Linux系统date命令的参数及获取时间戳的方法

    date指令相关用法示例 date 用法: date [OPTION]... [+FORMAT] date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]] ...

  7. Linux课程---7、shell技巧(获取帮助命令)

    Linux课程---7.shell技巧(获取帮助命令) 一.总结 一句话总结: ls --help:简单手册 man ls:内容手册 1.tab补全? 命令+tab:加快敲命令敲文件目录的速度,多敲几 ...

  8. linux uname内核,Linux下confstr与uname函数_获取C库与内核信息

    Linux下confstr与uname函数_获取C库与内核信息 #include #include  //uname int main(int argc, char **argv[]) { struc ...

  9. Android获取Linux图像信息,Android系统信息获取 之十三:Linux内核版本信息获取

    Android系统信息获取 之十三:Linux内核版本信息获取 Android系统是基于Linux的,各个Android版本对应的Linux版本不尽相同,我们这里不去追究各个Android对应的Lin ...

  10. C#编写运行在Linux环境下的采用Mediainfo来获取多媒体文件信息的代码

    C#编写运行在Linux环境下的采用Mediainfo来获取多媒体文件信息的代码 原文:C#编写运行在Linux环境下的采用Mediainfo来获取多媒体文件信息的代码 项目开始设计的是运行在wind ...

最新文章

  1. Git详解之二 Git基础
  2. 干货 | 蚂蚁金服是如何实现经典服务化架构往 Service Mesh 方向的演进的?
  3. 深度学习笔记:windows+tensorflow 指定GPU占用内存(解决gpu爆炸问题)
  4. Spring Cloud 入门 之 Ribbon 篇(二)
  5. Apache-ab 接口性能测试
  6. linux下,保存退出vim编辑器(转)
  7. 监控IIS的运行状态
  8. bluecam连接步骤说明_迈拓维距Type-C扩展坞手机连接电视图文教程
  9. python做一个微型美颜图片处理器,十行代码即可完成
  10. vue2 - 基于Export2Excel.js导出Excel案例(js-xlsx插件二次封装使用)
  11. 手把手逆向Playcanvas天空盒编码(一次乌龙的任务)
  12. 英文字母或者中文字母文本替换
  13. 算法 - 递归实现汉诺塔(The Tower of Hanoi)
  14. 文本摘要相关论文汇总
  15. Windows 10 各版本
  16. C语言 六大门派身份识别
  17. errors were encountered while processing qmail qmail-run
  18. 不要再让我们听到抽胆黑熊的哭泣
  19. 企业如何保护“特权访问”的安全?
  20. 行为金融(四):投资者心理与行为偏差

热门文章

  1. 第14条:理解“类对象”的用意
  2. multipath管理存储多路径
  3. Linux运维人员的前生后世!
  4. fedora 35 安装各种桌面环境命令整理
  5. 将本地docker镜像推送到阿里云镜像仓库
  6. Python字符串前加u/r/b的作用
  7. kibana操作elasticsearch:创建索引库
  8. Rabbitmq消息的Confirm确认机制
  9. SpringMVC一个Controller处理所有用户请求的并发问题
  10. Spring注解开发-属性依赖注入