How to detect public IP address (of router/gateway) change in .Net?

牧云@^-^@ 提交于 2019-12-07 09:18:30

问题


I am aware of the NetworkChange.NetworkAddressChanged event, but that is only for the local network interfaces.

What is the best way to detect the public IP address of the gateway has changed? Presumably one has to do it by polling an external reliable address, such a google.com...?


回答1:


Here's one by Akamai: http://whatismyip.akamai.com/




回答2:


If the router supports it, you could use SNMP to query for its public IP address (or, at least, when I tried to do this I had to query for all IP addresses and filter out the private address(es)). You'll need to identify the OIDs (object IDs) that correspond to the IP address values, which I believe should be the same across all routers. Poll the router at regular intervals and compare the previous result to the current result. Alternatively, set up an SNMP Trap so the router will notify your application of a change in IP address. It might take more work to implement than simply processing the HTML from a "what is my IP" website, but this will be more efficient, possibly more reliable, won't generate any WAN traffic, and you won't have to rely on a website you don't control that could break your application by blocking you or changing the structure of their HTML.

#SNMP is an open source SNMP library for .NET.




回答3:


Using an external API is the only reliable method. Most of them who reveals your IP address will block a machine made request and ask for a CAPTCHA input. Here is a good one which allows machines to make request:

http://ip.nefsc.noaa.gov



来源:https://stackoverflow.com/questions/6182829/how-to-detect-public-ip-address-of-router-gateway-change-in-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!