【问题标题】:Is there something like a ssid for lan? / Can I identify the current network?有没有类似局域网的 ssid 之类的东西? / 我可以识别当前的网络吗?
【发布时间】:2015-12-10 18:09:16
【问题描述】:

我正在编写一个 c# 程序,我应该将 PC 连接到的当前网络与数据库进行比较。

使用 W-LAN 很容易(SSID),但是如果用户通过 LAN 连接,我如何识别网络并且 SSID 是最佳解决方案?

谢谢。

【问题讨论】:

  • 识别网络是什么意思?您想要网络的唯一标识符吗?
  • 是的,我可以“确定”这是我正在搜索的网络。
  • 我认为 SSID 非常不可靠。我的意思是您多久连接一次“免费 Wifi”?你说的局域网是什么意思?相同的公司,相同的本地链接,相同的什么?我可能会倾向于获取当前默认网关的 MAC 地址?
  • 拥有大量子网的大型网络是什么情况。 MAC 地址方法是否适用于这里,或者在另一个子网中是否有另一个默认网关?
  • 网络上确实没有任何东西可以将其识别为唯一。 . SSID 只是网络的名称,很容易更改。您可以尝试获取网关/路由器的 MAC,但是是的,没有关于拥有单个网关的“规则”(尽管您的计算机应该只选择一个)。您可以获得网络上所有 MAC 的列表,如果它的某个百分比相同,则假设它是一个特定的网络......如果路由器被更换,则路由器 MAC 有问题,MAC 改变......跨度>

标签: c# networking identification ssid


【解决方案1】:

如果你想走默认网关的 MAC 路由试试这个:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Threading.Tasks;

namespace NetworkIdentifier
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var macAddressOfGateway = GetMacAddressOfGateway();
            Console.WriteLine(macAddressOfGateway);
            Console.ReadLine();
        }

        private static string GetMacAddressOfGateway()
        {
            var networkInterface = GetBestNetworkInterface();
            var gatewayAddress = networkInterface.GetIPProperties().GatewayAddresses.FirstOrDefault();
            if (gatewayAddress == null)
            {
                throw new InvalidOperationException("No gateway!");
            }

            var mac = GetMacAddress(gatewayAddress.Address);

            return MacToString(mac);
        }

        private static string MacToString(byte[] mac)
        {
            return string.Format("{0:X2}:{1:X2}:{2:X2}:{3:X2}:{4:X2}:{5:X2}", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
        }

        public static NetworkInterface GetBestNetworkInterface()
        {
            var publicIpAddress = new IPAddress(0x08080808); // Googles DNS IP of 8.8.8.8 (Seems like a relatively safe hardcoded IP to pick :))
            var ipv4AddressAsUInt32 = BitConverter.ToUInt32(publicIpAddress.GetAddressBytes(), 0);
            uint interfaceindex;
            var result = Win32ApiCalls.GetBestInterface(ipv4AddressAsUInt32, out interfaceindex);
            if (result != 0)
            {
                throw new Win32Exception(result);
            }

            var networkCards = NetworkInterface.GetAllNetworkInterfaces().Where(x => x.OperationalStatus == OperationalStatus.Up);
            return networkCards.FirstOrDefault(networkInterface => networkInterface.GetIPProperties().GetIPv4Properties().Index == interfaceindex);
        }

        public static IPAddress GetDefaultGateway()
        {
            var networkInterface = GetBestNetworkInterface();
            if (networkInterface == null) return null;
            var address = networkInterface.GetIPProperties().GatewayAddresses.FirstOrDefault();
            return address.Address;
        }

        public static byte[] GetMacAddress(IPAddress address)
        {
            byte[] mac = new byte[6];
            uint len = (uint)mac.Length;
            byte[] addressBytes = address.GetAddressBytes();
            uint dest = ((uint)addressBytes[3] << 24)
              + ((uint)addressBytes[2] << 16)
              + ((uint)addressBytes[1] << 8)
              + ((uint)addressBytes[0]);
            if (Win32ApiCalls.SendARP(dest, 0, mac, ref len) != 0)
            {
                throw new Exception("The ARP request failed.");
            }
            return mac;
        }
    }

    public static class Win32ApiCalls
    {
        [DllImport("iphlpapi.dll", CharSet = CharSet.Auto)]
        public static extern int GetBestInterface(UInt32 destAddr, out UInt32 bestIfIndex);

        [DllImport("iphlpapi.dll", ExactSpelling = true)]
        public static extern int SendARP(uint destIP, uint srcIP, byte[] macAddress, ref uint macAddressLength);
    }
}

这需要大量的清理、警告、错误检查等(并且不适用于非 Windows 主机,所以甚至不要打扰 dnxcore :))

** 告售者 :)

编辑:刚刚删除了我注释掉的行,因为它们没有添加任何有用的东西

【讨论】:

    【解决方案2】:

    默认网关将为您提供您所连接的路由器的 IP 地址(或为您分配 IP 地址的任何设备)。或者您可以获取网关的MAC地址等。

    【讨论】:

    • 非常感谢,我还没有考虑 MAC 地址。
    • @Someone,这不一定适用于某些网络,因为它们可能使用多个网关或可能使用第一跳冗余协议。您可以在同一网络上的不同主机上获得不同的网关 IP 和 MAC 地址。实际的网络地址与我认为您能够识别特定网络的距离差不多。
    • 是的@RonMaupin 是正确的,除非您对网络结构等有更多了解,否则确实没有“好的”方法来识别它。您可以跟踪到已知地址并构建地图这样,去你想要的任何水平。但如果对网络至少有一些了解,这将主要是一场猜谜游戏。
    • @Someone,实际的网络地址有什么问题?网络上的每个主机都可以免费使用它。
    • 但是可以说获得正确的网络并不重要,我只想在每次 SSID 为“ExampleNetwork”时做一些事情。至少这样的事情在局域网中是可能的吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-08
    • 2011-04-07
    • 2020-03-11
    • 2011-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多