Created
July 31, 2013 15:52
-
-
Save jsl/6123252 to your computer and use it in GitHub Desktop.
Zerigo DDNS update script in Haskell
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 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 ++ | |
"&[email protected]&" ++ | |
"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