Skip to content

Instantly share code, notes, and snippets.

@jsl
Created July 31, 2013 15:52
Show Gist options
  • Save jsl/6123252 to your computer and use it in GitHub Desktop.
Save jsl/6123252 to your computer and use it in GitHub Desktop.
Zerigo DDNS update script in Haskell
import Text.XML.Light
import Network.Curl
import System.IO
import System.Environment
import System.Exit
extractIP :: String -> String
extractIP xmlString =
let
element = parseXMLDoc xmlString >>= findElement (QName "ipv4" Nothing Nothing)
in
case element of
Nothing -> "0.0.0.0"
Just x -> strContent x
updateUrl :: String -> String
updateUrl ipAddress = "http://update.zerigo.com/dynamic?" ++
"host=nyc.stackbuilders.com&ip=" ++ ipAddress ++
"&user=foo@bar.com&" ++
"password=55555555555555"
printErrorResponse :: CurlCode -> IO ()
printErrorResponse respCode = hPutStrLn stderr $ "Error: " ++ show respCode
postIP :: String -> IO ()
postIP ipAddress = do
case ipAddress of
"0.0.0.0" -> putStrLn "Not posting non-routable address 0.0.0.0."
_ -> do
putStrLn $ "Posting " ++ ipAddress
(code, body) <- curlGetString (updateUrl ipAddress) []
case code of
CurlOK -> putStrLn "A OK"
_ -> printErrorResponse code
main :: IO ()
main = do
(code, body) <- curlGetString "http://checkip.zerigo.com" []
case code of
CurlOK -> postIP $ extractIP body
_ -> putStrLn $ "Error: " ++ show code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment