2011-10-21 44 views
0

私はローカルIPアドレスを返すC#関数を持っています。MACアドレスとIPアドレス

private string GetLocalIPByHostName() 
    { 
     string host = Dns.GetHostName(); 
     string LocalIP = string.Empty; 
     IPHostEntry ip = Dns.GetHostEntry(host); 
     foreach (IPAddress _IPAddress in ip.AddressList) 
     { 
      if (_IPAddress.AddressFamily.ToString() == "InterNetwork") 
      { 
       LocalIP = _IPAddress.ToString(); 

      } 
     } 
     return LocalIP; 
    } 

このローカルIPアドレスを使用して、私はMACアドレスを取得しようとしました。

protected string GetMACAddressByIP(string ip) 
    { 
     try 
     { 
      ManagementObjectSearcher query= new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration"); 
      ManagementObjectCollection queryCollection = query.Get(); 
      bool Found = false; 
      foreach(ManagementObject _ManagementObject in queryCollection) 
      { 
       if (_ManagementObject["IPAddress"] != null) 
       { 
        string _IPAddress; 
        _IPAddress = string.Join(".", (string[])_ManagementObject["IPAddress"]); 

        if(!_IPAddress.Equals("")) 
        { 
         if(_IPAddress.Equals(ip.Trim())) 
         { 
           Found = true; 
         } 
        } 

        if(Found == true) 
        { 
         if (_ManagementObject["macaddress"] != null) 
         { 
          if (!_ManagementObject["macaddress"].Equals("")) 
          { 
           return (string)_ManagementObject["macaddress"]; 
          } 
         } 
        } 
        else 
        { 
         Found = false; 
        } 
       } 
      } 

      MessageBox.Show("No Mac Address Found"); 
      return ""; 
     } 
     catch(Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
      return ""; 
     } 
    } 

2つの機能が正しく機能します。
しかし、私がしたいのは、同じLANネットワーク上で他のPCのIPアドレスを取得することです。
私はそれらのIPアドレスを取得した場合はその後、私の

GetMACAddressByIP(string ip) 

関数への入力値となります。

しかし私の問題は、他のPCのIPアドレスを取得する方法がわかりません。

private List<string> GetRemoteIPs(string LocalIPAddress) 
    { 
     List<string> RemoteIPs = new List<string>(); 

      /*** Here code will be as suggestion of yours. ****/  

     return RemoteIPs; 
    } 

すると、次の質問はすでにオフにあるPCのMACアドレスを取得することができ、この
ですか?

すべての解決策は本当に感謝しています。

+1

、それは http://stackoverflow.com/questions/1993891/list-the-ip-address-of-all-computers-connected-toで、この質問の可能重複です-a-single-lan 役立つかどうかを確認してください。 – Tariqulazam

答えて

3
// Get all active IP connections on the network 
    private void btnSearch_Click(object sender, EventArgs e) 
    { 
     System.Net.NetworkInformation.IPGlobalProperties network = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties(); 
     System.Net.NetworkInformation.TcpConnectionInformation[] connections = network.GetActiveTcpConnections(); 

     foreach (System.Net.NetworkInformation.TcpConnectionInformation connection in connections) 
     { 

     } 
    } 
+1

ようこそstackoverflowへ!ポストの精度を向上させるためのサンプルコードの簡単な説明を提供する方が良いでしょう。 –

0

一般的に、電源がオフになっているPCのMACアドレスを取得することはできません。これは、パケットで送信されるハードウェア識別子です。唯一の希望は、ローカルシステムのARPテーブルをチェックすることです。たとえば、コマンドラインでarp -a

と入力します。これは実行可能ではありませんが、これは可能です。実際にあなたがIPを知っていても、あなたが持っているテクニックは、すべてのリモート状況(もしあれば)で動作しないと確信しています。

0

同じLAN内のコンピュータのIPアドレスを見つける方法の1つは、すべての可能なIP。 IPとMAC​​の両方のアドレスをワンショットで得ることができます。下記のWindowsに細かい作業(がアップ WinXPの上でテストして)とLinux(Ubuntuの上でテスト、Suseの、RedHatの)モノとされる上

1

方法。あなたの最初の質問については

/// <summary>Get Network Interface Addresses information of current machine.</summary> 
/// <returns>Returns Array of Tuple of Mac Address, IP Address, and Status.</returns> 
public virtual Tuple<string, IPAddress, OperationalStatus>[] GetNetworkInterfaceAddresses() 
{ 
    List<Tuple<string, IPAddress, OperationalStatus>> list = new List<Tuple<string, IPAddress, OperationalStatus>>(); 

    NetworkInterfaceType[] acceptedNetInterfaceTypes = new NetworkInterfaceType[] 
      { 
       NetworkInterfaceType.Ethernet, 
       NetworkInterfaceType.Ethernet3Megabit, 
       NetworkInterfaceType.FastEthernetFx, 
       NetworkInterfaceType.FastEthernetT, 
       NetworkInterfaceType.GigabitEthernet, 
       NetworkInterfaceType.Wireless80211 
      }; 

    List<NetworkInterface> adapters = NetworkInterface.GetAllNetworkInterfaces() 
     .Where(ni => acceptedNetInterfaceTypes.Contains(ni.NetworkInterfaceType)).ToList(); 

    #region Get the Mac Address 

    Func<NetworkInterface, string> getPhysicalAddress = delegate(NetworkInterface am_adapter) 
    { 
     PhysicalAddress am_physicalAddress = am_adapter.GetPhysicalAddress(); 
     return String.Join(":", am_physicalAddress.GetAddressBytes() 
      .Select(delegate(byte am_v) 
      { 
       string am_return = am_v.ToString("X"); 
       if (am_return.Length == 1) 
       { 
        am_return = "0" + am_return; 
       } 

       return am_return; 
      }).ToArray()); 
    }; 

    #endregion 

    #region Get the IP Address 

    Func<NetworkInterface, IPAddress> getIPAddress = delegate(NetworkInterface am_adapter) 
    { 
     IPInterfaceProperties am_ipInterfaceProperties = am_adapter.GetIPProperties(); 
     UnicastIPAddressInformation am_unicastAddressIP = am_ipInterfaceProperties.UnicastAddresses 
      .FirstOrDefault(ua => ua.Address != null && ua.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork); 

     if (am_unicastAddressIP == null) 
     { 
      return null; 
     } 

     return am_unicastAddressIP.Address; 
    }; 

    #endregion 

    // It's possible to have multiple UP Network Interface adapters. So, take the first order from detected Network Interface adapters. 
    NetworkInterface firstOrderActiveAdapter = adapters.FirstOrDefault(ni => ni.OperationalStatus == OperationalStatus.Up); 

    string macAddress; 
    IPAddress ipAddress; 

    if (firstOrderActiveAdapter != null) 
    { 
     macAddress = getPhysicalAddress(firstOrderActiveAdapter); 
     ipAddress = getIPAddress(firstOrderActiveAdapter); 
     if (ipAddress == null) 
     { 
      throw new Exception("Unable to get the IP Address v4 from first order of Network Interface adapter of current machine."); 
     } 

     list.Add(new Tuple<string, IPAddress, OperationalStatus>(macAddress, ipAddress, firstOrderActiveAdapter.OperationalStatus)); 
     adapters.Remove(firstOrderActiveAdapter); 
    } 

    foreach (NetworkInterface adapter in adapters) 
    { 
     macAddress = getPhysicalAddress(adapter); 
     ipAddress = getIPAddress(adapter); 
     list.Add(new Tuple<string, IPAddress, OperationalStatus>(macAddress, ipAddress, adapter.OperationalStatus)); 
    } 

    if (firstOrderActiveAdapter == null) 
    { 
     throw new Exception("Unable to get the Active Network Interface of the current machine."); 
    } 

    return list.ToArray(); 
} 
関連する問題