Skip to content

Instantly share code, notes, and snippets.

@horus
Last active December 17, 2015 22:39
Show Gist options
  • Select an option

  • Save horus/5683209 to your computer and use it in GitHub Desktop.

Select an option

Save horus/5683209 to your computer and use it in GitHub Desktop.
import Control.Arrow ((&&&))
import Data.Bits (shiftR, (.&.), Bits(..))
import Data.Functor ((<$>))
import Data.List (intercalate)
import Data.Maybe (listToMaybe, fromMaybe)
import Network.BSD (getHostByName, HostEntry(..))
import System.Environment (getArgs)
main :: IO ()
main = do
arg <- fromMaybe (error "no arguments given") . listToMaybe <$> getArgs
longIp <- hostAddresses `fmap` getHostByName arg
mapM_ (putStrLn . showIPv4Addr) longIp
showIPv4Addr :: (Num a, Bits a, Show a) => a -> String
showIPv4Addr = toIPStr . ((a &&& b) &&& (c &&& d))
where d = flip shiftR 24
c = (.&. 0x00ff) . flip shiftR 16
b = (.&. 0x0000ff) . flip shiftR 8
a = (.&. 0x000000ff)
toIPStr ((a, b), (c, d)) = intercalate "." . map show $ [a, b, c, d]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment