如何在Linux中以编程方式检测IP地址更改? [英] How to detect IP address change programmatically in Linux?

查看:152
本文介绍了如何在Linux中以编程方式检测IP地址更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在Linux中使用C ++以编程方式检测本地计算机上的IP地址更改?

Is there a way to detect IP address changes on the local machine in Linux programmatically using C++?

推荐答案

获取当前IP使用:

    int s;
    struct ifreq ifr = {};

    s = socket(PF_INET, SOCK_DGRAM, 0);

    strncpy(ifr.ifr_name, "eth0", sizeof(ifr.ifr_name));

    if (ioctl(s, SIOCGIFADDR, &ifr) >= 0)
        printf("%s\n",
          inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));

使用您正在查看的界面替换eth0。您现在需要做的就是轮询变更。

Replace "eth0" with the interface you're looking at. All you now need to do is poll for a change.

这篇关于如何在Linux中以编程方式检测IP地址更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆