如何获取远程计算机的本地网络IP地址?

时间:2021-08-27 23:32:09

A remote computer has two address: LAN and VPN

远程计算机有两个地址:LAN和VPN

I tried to get LAN IP address by following code:

我尝试通过以下代码获取LAN IP地址:

hostent *host = gethostbyname(hostname);
if (host) strcpy(ip, inet_ntoa(*((struct in_addr *)host->h_addr)));

So, sometime it return LAN, but sometime VPN. I know that there're many address in h_addr_list field, but how can I know which address is local?

有时它会返回LAN,但有时会返回VPN。我知道h_addr_list字段中有很多地址,但是我怎么知道哪个地址是本地的呢?

I tried to use GetAdaptersInfo(), it has field to determine local ip or not, but just return ip address of my computer. I don't know how to get ip for remote computer.

我尝试使用GetAdaptersInfo(),它有确定本地ip的字段,但只返回我的计算机的ip地址。我不知道如何获得远程计算机的ip。

Anyone give me some advices, please!

请给我一些建议!

2 个解决方案

#1


2  

As per your question you state that you can get the lan IP of your computer and get the ip's of the remote computer be it both LAN or VPN. To determine which IP on the remote computer is on your lan you can use a subnet test, I assume that your VPN is on a different subnet of course, and you can essentially test if the first 2-3 octets of your computer's ip matches that of the 2-3 octets of the remote ip. This is the only way I can think of to do it.

根据你的问题,你说你可以得到你的计算机的局域网IP,并获得远程计算机的IP,无论是局域网还是VPN。确定哪个IP在远程计算机上在你的局域网可以使用子网测试中,我假设您的VPN是在不同的子网当然,本质上你可以测试,如果你电脑的前2 - 3字节的IP匹配的2 - 3远程IP的八位字节。这是我能想到的唯一办法。

So, loop through the remote IP's as you say you can get them and check them against the local ip address of your computer's octets.

因此,循环遍历远程IP,就像您说的那样,您可以获取它们,并根据计算机的本地IP地址检查它们。

#2


0  

After playing around with this problem, I realize that the adapters on remote string could not be public due to security issue. Thus, we don't have an exactly way to know which IP address is VPN.

在解决了这个问题之后,我意识到远程字符串上的适配器由于安全问题不能公开。因此,我们无法确切地知道哪个IP地址是VPN。

Moreover, VPN address is created by VPN server. Therefore, that VPN address could be anything, even similar to our local IP. In other words, we cannot guess VPN address any way.

VPN地址是由VPN服务器创建的。因此,VPN地址可以是任何东西,甚至类似于我们的本地IP。换句话说,我们无法猜测VPN地址。

#1


2  

As per your question you state that you can get the lan IP of your computer and get the ip's of the remote computer be it both LAN or VPN. To determine which IP on the remote computer is on your lan you can use a subnet test, I assume that your VPN is on a different subnet of course, and you can essentially test if the first 2-3 octets of your computer's ip matches that of the 2-3 octets of the remote ip. This is the only way I can think of to do it.

根据你的问题,你说你可以得到你的计算机的局域网IP,并获得远程计算机的IP,无论是局域网还是VPN。确定哪个IP在远程计算机上在你的局域网可以使用子网测试中,我假设您的VPN是在不同的子网当然,本质上你可以测试,如果你电脑的前2 - 3字节的IP匹配的2 - 3远程IP的八位字节。这是我能想到的唯一办法。

So, loop through the remote IP's as you say you can get them and check them against the local ip address of your computer's octets.

因此,循环遍历远程IP,就像您说的那样,您可以获取它们,并根据计算机的本地IP地址检查它们。

#2


0  

After playing around with this problem, I realize that the adapters on remote string could not be public due to security issue. Thus, we don't have an exactly way to know which IP address is VPN.

在解决了这个问题之后,我意识到远程字符串上的适配器由于安全问题不能公开。因此,我们无法确切地知道哪个IP地址是VPN。

Moreover, VPN address is created by VPN server. Therefore, that VPN address could be anything, even similar to our local IP. In other words, we cannot guess VPN address any way.

VPN地址是由VPN服务器创建的。因此,VPN地址可以是任何东西,甚至类似于我们的本地IP。换句话说,我们无法猜测VPN地址。