Last active
December 17, 2015 22:39
-
-
Save horus/5683209 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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