1 people like it.

Get local IPv4-address of the computer

Get local IPv4-address of the computer

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
open System;
open System.Net;
open System.Net.NetworkInformation;

let GetLocalIpv4Address() = 
    let interfaces = NetworkInterface.GetAllNetworkInterfaces()
    let unicastAddresses = 
        interfaces |> Seq.map (fun i -> i.GetIPProperties().UnicastAddresses) |> Seq.concat |> Seq.toList
    let item = 
        unicastAddresses |> Seq.find (fun a -> a.IPv4Mask.ToString() <> "0.0.0.0" && a.IPv4Mask.ToString() <> "255.0.0.0")
    //|> Seq.find (fun a -> a.PrefixLength = 16)
    item.Address.ToString()
namespace System
namespace System.Net
namespace System.Net.NetworkInformation
val GetLocalIpv4Address : unit -> string

Full name: Script.GetLocalIpv4Address
val interfaces : NetworkInterface []
type NetworkInterface =
  member Description : string
  member GetIPProperties : unit -> IPInterfaceProperties
  member GetIPv4Statistics : unit -> IPv4InterfaceStatistics
  member GetPhysicalAddress : unit -> PhysicalAddress
  member Id : string
  member IsReceiveOnly : bool
  member Name : string
  member NetworkInterfaceType : NetworkInterfaceType
  member OperationalStatus : OperationalStatus
  member Speed : int64
  ...

Full name: System.Net.NetworkInformation.NetworkInterface
NetworkInterface.GetAllNetworkInterfaces() : NetworkInterface []
val unicastAddresses : UnicastIPAddressInformation list
module Seq

from Microsoft.FSharp.Collections
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
val i : NetworkInterface
NetworkInterface.GetIPProperties() : IPInterfaceProperties
val concat : sources:seq<#seq<'T>> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.concat
val toList : source:seq<'T> -> 'T list

Full name: Microsoft.FSharp.Collections.Seq.toList
val item : UnicastIPAddressInformation
val find : predicate:('T -> bool) -> source:seq<'T> -> 'T

Full name: Microsoft.FSharp.Collections.Seq.find
val a : UnicastIPAddressInformation
property UnicastIPAddressInformation.IPv4Mask: IPAddress
IPAddress.ToString() : string
property IPAddressInformation.Address: IPAddress
Raw view Test code New version

More information

Link:http://fssnip.net/7P7
Posted:8 years ago
Author:Tuomas Hietanen
Tags: ipv4 , networking