@@ -59,6 +59,14 @@ def __call__(self, *args):
5959 # call the proxy's method with the modified arguments
6060 return getattr (self .proxy , method )(* key_args )
6161
62+ def get_external_ip_from_url (url ):
63+ '''Get all the IP addresses found at a given URL.'''
64+
65+ # open the website, download its data, and return all IP strings found
66+ data = urllib2 .urlopen (url , timeout = 10 ).read ()
67+ addys = IP_ADDRESS_REGEX .findall (data )
68+ return addys
69+
6270def get_external_ip (attempts = 100 , threshold = 3 ):
6371 '''Return our current external IP address, or None if there was an error.'''
6472
@@ -89,9 +97,7 @@ def get_external_ip(attempts=100, threshold=3):
8997 provider = current_providers .pop ()
9098
9199 try :
92- # open the website, download its data, and search for IP strings
93- data = urllib2 .urlopen (provider , timeout = 10 ).read ()
94- addys = IP_ADDRESS_REGEX .findall (data )
100+ addys = get_external_ip_from_url (provider )
95101
96102 # add a single address to the counter randomly to help prevent false
97103 # positives. we don't add all the found addresses to guard against adding
@@ -100,9 +106,9 @@ def get_external_ip(attempts=100, threshold=3):
100106 # the chances that several sites will return the same false-positive
101107 # number?
102108 if len (addys ) > 0 :
103- log .info ('Got external address from provider: %s' , provider )
104109 ip = random .choice (addys )
105110 ip_counts .update ({ ip : 1 })
111+ log .info ('Got IP from provider %s: %s' , provider , ip )
106112
107113 # check for agreeing IP addresses, and return the first address that meets
108114 # or exceeds the count threshold.
@@ -138,14 +144,24 @@ def is_valid_dynamic_record(name, record):
138144 '''Return True if the record matched the given name and is an A record.'''
139145 return record ['name' ] == name and record ['type' ].lower () == 'a'
140146
141- def main ():
147+ def test_providers ():
148+ '''Test all IP providers and log the IPs they return.'''
149+
150+ for provider in load_providers ():
151+ log .info ('IPs found at %s:' , provider )
152+
153+ try :
154+ for ip in get_external_ip_from_url (provider ):
155+ log .info (' %s' , ip )
156+ except Exception , e :
157+ log .warning ('Error getting external IP address from %s: %s' , provider , e )
158+
159+ def update_ip ():
142160 '''
143161 Check our external IP address and update Gandi's A-record to point to it if
144162 it has changed.
145163 '''
146164
147- import sys
148-
149165 # load the config file so we can get our variables
150166 log .info ('Loading config file...' )
151167 config = load_config ()
@@ -252,5 +268,13 @@ def main():
252268 log .info ('Set zone %d as the active zone version.' , new_version_id )
253269 log .info ('Dynamic record successfully updated to %s!' , external_ip )
254270
271+ def main (args ):
272+ # test all providers if specified, otherwise update the IP
273+ if args [- 1 ] == 'test' :
274+ test_providers ()
275+ else :
276+ update_ip ()
277+
255278if __name__ == '__main__' :
256- main ()
279+ import sys
280+ main (sys .argv )
0 commit comments