module System.Info.MAC.Fetch where
import Data.MAC
import Control.Monad
import Data.List
import System.Process
import System.Info
import System.IO
import Text.ParserCombinators.Parsec
fetch =
case os of
"windows" -> win32
_ -> nixen
nixen :: IO (Maybe MAC)
nixen = do
(_, o, _, h) <- runInteractiveCommand "ifconfig"
waitForProcess h
outputs <- hGetContents o
return $ join $ ifconfig outputs
win32 :: IO (Maybe MAC)
win32 = do
(_, o, _, h) <- runInteractiveCommand "ipconfig /all"
waitForProcess h
outputs <- hGetContents o
return $ join $ ipconfig outputs
where
locations = map (++ "\\ipconfig")
[ "c:\\windows\\system32"
, "c:\\winnt\\system32"
]
ipconfig = parse' "ipconfig" $ do
manyTill anyChar $ try $ string "Physical Address"
manyTill anyChar $ char ':'
spaces
hexen <- sepHex '-'
return . maybeMAC . intercalate ":" $ hexen
ifconfig = parse' "ifconfig" $ do
manyTill anyChar markers
spaces
hexen <- sepHex ':'
return . maybeMAC . intercalate ":" $ hexen
where
markers = choice $ map (try . string) [ "ether", "HWaddr" ]
parse' source parser = eitherToMaybe . parse parser source
where
eitherToMaybe (Left _) = Nothing
eitherToMaybe (Right r) = Just r
maybeMAC :: String -> Maybe MAC
maybeMAC s =
case reads s of
[(mac, _)] -> Just mac
_ -> Nothing
sepHex = sepBy (sequence [hexDigit, hexDigit]) . char