Tuesday, December 27, 2011

F#, Pings and finding an IP

For some reason, WINS server was down and I couldn't connect remotely to a PC in my room. Big deal, you may think, but at the moment the PC was running display-less and I didn't have a spare display to connect to it.

The idea was to ping each and every machine in 192.168.0.xxx range, disconnect the PC, ping the range again, get the difference.

A cool way to do it, I guess, involves PowerShell, but I don't know how to use it. I know F# a bit :)

Here is the code:

open System
open System.Net
open System.Net.NetworkInformation

let p = new Ping()

let isOnline n = p.Send(new IPAddress([|192uy; 168uy; 0uy; (byte)n|]), 100).Status = IPStatus.Success
let div2 n = n % 2 = 0

let online1 = [2..254] |> Seq.filter isOnline |> Seq.toArray

// At this moment I physically disconnected the PC I needed to discover

let online2 = [2..254] |> Seq.filter isOnline |> Seq.toArray

// here it is :)
let pc = Set.difference (Set.ofArray online1) (Set.ofArray online2)

// it's 75

// connected the PC, and ... 
isOnline 75
// Hurra, it's online

Luckily, DCHP server was up :) and the PC got its IP back when connected.

No comments:

Post a Comment