Skip to content

Instantly share code, notes, and snippets.

@mdunsmuir
Created March 19, 2015 00:11
Show Gist options
  • Select an option

  • Save mdunsmuir/3b30a7bf99f524a3e924 to your computer and use it in GitHub Desktop.

Select an option

Save mdunsmuir/3b30a7bf99f524a3e924 to your computer and use it in GitHub Desktop.

Revisions

  1. mdunsmuir created this gist Mar 19, 2015.
    18 changes: 18 additions & 0 deletions ShowIP.hs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    import System.Environment
    import Control.Exception
    import Control.Monad
    import Network.Socket

    main = do
    args <- getArgs
    case args of
    [host] -> catch (displayIPs host) $ \e -> do
    let err = show (e :: IOException)
    putStrLn $ "couldn't look up host " ++ host ++ " (" ++ err ++ ")"
    _ -> fail "usage: ShowIP <hostname>"

    displayIPs :: String -> IO ()
    displayIPs host = do
    let query = defaultHints { addrFamily = AF_INET, addrSocketType = Stream }
    infos <- getAddrInfo (Just query) (Just host) Nothing
    forM_ infos (putStrLn . show)