Skip to content

Instantly share code, notes, and snippets.

@Denchyaknow
Created January 18, 2025 00:34
Show Gist options
  • Save Denchyaknow/5c5e4aafcbfcf20f1c0751173d1c81f5 to your computer and use it in GitHub Desktop.
Save Denchyaknow/5c5e4aafcbfcf20f1c0751173d1c81f5 to your computer and use it in GitHub Desktop.
HowTo_PollNetworkWanInfo
public class PollNetworkInfo : MonoBehaviour
{
private string ip_address_value = "N/A";
private string subnet_mask_value = "N/A";
private string gateway_value = "N/A";
private string mac_address_value = "N/A";
void PollNetworkAdapters()
{
// Reset in case we don't find anything
ip_address_value = "N/A";
subnet_mask_value = "N/A";
gateway_value = "N/A";
mac_address_value = "N/A";
foreach (NetworkInterface adapter in NetworkInterface.GetAllNetworkInterfaces())
{
// We only want an active, non-loopback interface
if (adapter.OperationalStatus == OperationalStatus.Up && adapter.NetworkInterfaceType != NetworkInterfaceType.Loopback)
{
mac_address_value = adapter.GetPhysicalAddress().ToString();
var ipProperties = adapter.GetIPProperties();
foreach (UnicastIPAddressInformation unicast in ipProperties.UnicastAddresses)// Unicast addresses for IP and subnet
{
if (unicast.Address.AddressFamily == AddressFamily.InterNetwork)
{
ip_address_value = unicast.Address.ToString();
if (unicast.IPv4Mask != null)
{
subnet_mask_value = unicast.IPv4Mask.ToString();
}
}
}
foreach (GatewayIPAddressInformation g in ipProperties.GatewayAddresses)// Gateway
{
if (g.Address.AddressFamily == AddressFamily.InterNetwork)
{
gateway_value = g.Address.ToString();
}
}
// Break if you only want the first active interface
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment