Chralu / gandyn

Gandi domain updater script for dynamic IP addresses.
72 stars 32 forks source link

retrieve actual IP address via dns #15

Open gwalarn opened 9 years ago

gwalarn commented 9 years ago

We can retriev our actual IP via: dig +short myip.opendns.com @resolver1.opendns.com

It's easier, faster and more sure than a web site.

Chralu commented 8 years ago

Hi @gwalarn,

you're right about this, moreover I just tried the command you gave, it fails on a "connection timed out". Maybe is it because of my working place network.

anyway thanks for the tip, I'll try to investigate.

Cheers,

gwalarn commented 8 years ago

Hi, If a timeout occurs when accessing to opendns dns servers, maybe you do the query from a machine connected to an ISP which prohibits dns queries to other servers than those of your ISP? Can you verify that you can join the Google servers for example with: dig +short github.com @8.8.8.8 and try the query from an other ISP to confirm the obligation to use a "serveur DNS menteur" that don't respect the net neutrality.

Cheers

tintamarre commented 8 years ago

Something like that ?

import dns.resolver # $ pip install dnspython

resolver = dns.resolver.Resolver(configure=False)
resolver.nameservers = ["208.67.222.222", "208.67.220.220"]
public_ip = resolver.query('myip.opendns.com')[0]
return public_ip
gwalarn commented 8 years ago

Thanks for the programming example, I never program in python but when i do a comparison of public_ip (result of resolver.query('myip.opendns.com')[0] and a string containing my IP like this:

current_ip_address = "1.1.1.1" # remplace 1.1.1.1 with my real IP resolver = dns.resolver.Resolver(configure=False) resolver.nameservers = ["208.67.222.222", "208.67.220.220"] public_ip = resolver.query('myip.opendns.com')[0] if current_ip_address != previous_ip_address: print('KO') else: print('OK')

the result is "KO"

Are the variable typed in python? when i write: public_ip = str(resolver.query('myip.opendns.com')[0]) the comparison is OK

PS: If we prefer use google than opendns our public IP can be obtain with this dns request: dig TXT +short o-o.myaddr.l.google.com @ns1.google.com | sed 's/"//g'

ghost commented 7 years ago

Hi, I added a new IP provider because I wasn't able to get IP from the two previous ones.

My updates in adapter.py :

class IPYunohost( object ):
  def get_public_ip( self ):
    """Returns the current public IP address. Raises an exception if an issue occurs."""
    try:
      url_page = 'http://ip.yunohost.org'
      public_ip = None

      f = urllib.request.urlopen(url_page)
      data = f.read().decode("utf8")
      f.close()
      pattern = re.compile('\d+\.\d+\.\d+\.\d+')
      result = pattern.search(data, 0)
      if result == None:
        raise ipretriever.Fault('Service '+url_page+' failed to return the current public IP address')
      else:
        public_ip = result.group(0)
    except urllib.error.URLError as e:
      raise ipretriever.Fault(e)
    return public_ip

and in gandyn.py line 141 :

public_ip_retriever = ipretriever.adapter.IPYunohost()

Best regards

tintamarre commented 7 years ago

This is what I currently use : https://gist.github.com/tintamarre/067fbf48bee4081fc46348d11f10625a

I got Public IP from this command :

public_ip=`dig +short myip.opendns.com @resolver1.opendns.com`