snippets / ip

All snippets tagged ip (2)

  1. Connaître son adresse ip

    -- Nécessite le programme links

    1 #!/bin/bash
    2
    3 links -dump http://www.whatismyip.com/automation/n09230945.asp | sed -e 's/ //g'
    Posted by raf to shell ip shell links ... saved by 4 persons ... 2 comments ... 1 year
  2. dynamic dns daemon

    This program allow to update a dns from a dynamic ip. It use nsupdate to add and delete any record from a dns zone. It update it securly thanks a key you set before. Currently it support only anames.

      1 #!/usr/local/bin/python2.4
    2 #
    3 # Copyright (c) 2007 Benoit Chesneau <benoit@bchesneau.info>
    4 #
    5 # Permission to use, copy, modify, and distribute this software for any
    6 # purpose with or without fee is hereby granted, provided that the above
    7 # copyright notice and this permission notice appear in all copies.
    8 #
    9 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
    10 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
    11 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
    12 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
    13 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
    14 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
    15 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
    16 #
    17 # Description :
    18 # This program allow to update a dns from a dynamic ip. It use nsupdate to add
    19 # and delete any record from a dns zone. It update it securly thanks a key you
    20 # set before. Currently it support only anames.
    21 #
    22 # It require to create a configuration file /etc/ddns.conf or ~/.ddns.conf :
    23 # add a zone like this :
    24 # [domain1]
    25 # key=<path to your private key>
    26 # server = <name of dns>
    27 # zone = <zone name>
    28 # anames = <anames you want to add/update>
    29 #
    30 # Add as any zone you want by creating section [domainXX] in your
    31 # configuration file.
    32 #
    33 #
    34 # For more information on setting your dns ypu can go here :
    35 # * nsupdate: Painless Dynamic DNS: http://linux.yyz.us/nsupdate/
    36 # * A DDNS Server Using BIND and Nsupdate : http://www.oceanwave.com/technical-resources/unix-admin/nsupdate.html
    37 # * DynDNS et Subzone ARPA : http://wiki.gcu.info/doku.php?id=unix:dyndns&s=bind
    38
    39
    40 import re
    41 import urllib
    42 import ConfigParser
    43 import os
    44 import time
    45
    46 checkip_urls = (
    47 (r'Current IP Address:\s(?P<ip>[^<]*)', 'http://checkip.dyndns.org/'),
    48 (r'Current Address:\s(?P<ip>.*)$', 'http://ipdetect.dnspark.com/'),
    49 )
    50
    51 rIP=re.compile("(\d{1,3}(\.\d{1,3}){3})(?!/\d+)")
    52 cmd = '/usr/sbin/nsupdate'
    53
    54 def get_ip():
    55 """
    56 For now we get dynamic ip only from web.
    57 TODO: add detection from interface and router
    58 """
    59 f = None;
    60 ip=None
    61 for u in checkip_urls:
    62 pat = re.compile(u[0]);
    63 try:
    64 f = urllib.urlopen(u[1])
    65 except:
    66 continue
    67
    68 if f:
    69 for line in f.readlines():
    70 match = pat.search(line)
    71 if match is not None:
    72 ip=match.group('ip')
    73 if rIP.search(ip):
    74 return ip
    75
    76 return ip
    77
    78 def get_conffile():
    79 conffiles = ("~/.ddns.conf", "/etc/ddns.conf",)
    80 for c in conffiles:
    81 path = os.path.expanduser(c)
    82 if os.path.exists(path):
    83 return path
    84 return None
    85
    86 def get_conf():
    87 domains = []
    88 config = ConfigParser.ConfigParser()
    89 config.read(get_conffile())
    90 for section in config.sections():
    91 if section.startswith('domain'):
    92 anames = config.get(section, 'anames').split(',')
    93 domains.append({
    94 'key_path': config.get(section, 'key'),
    95 'anames': [aname.strip() for aname in anames],
    96 'server': config.get(section, 'server').strip(),
    97 'zone': config.get(section, 'zone').strip(),
    98 })
    99 return domains
    100
    101 def nsupdate(domain, ip):
    102 tmp_path = '/var/tmp/.ddns_%s.txt' % domain['server']
    103 aname_str=""
    104 for aname in domain['anames']:
    105 if aname_str != "": aname_str="\n"
    106 aname_str+="""update delete %(aname)s A
    107 update add %(aname)s 60 A %(ip)s
    108 show
    109 send""" % {
    110 'aname': aname,
    111 'ip': ip,
    112 }
    113 tmp_str = """server %(server)s
    114 zone %(zone)s
    115 %(aname_str)s
    116 """ % {
    117 'server': domain['server'],
    118 'zone': domain['zone'],
    119 'aname_str': aname_str,
    120
    121
    122 }
    123
    124 fdomain = open(tmp_path, 'w+')
    125 fdomain.write(tmp_str)
    126 fdomain.close()
    127 cmd_str = "%s -k %s -v %s" % (cmd,domain['key_path'],tmp_path)
    128 os.system(cmd_str)
    129
    130 def update_dns():
    131 ip_path = "/var/tmp/.ddns_ip"
    132 while True:
    133 ip=get_ip()
    134 if os.path.exists(ip_path):
    135 f = open(ip_path, 'r')
    136 old_ip = f.readline()
    137 f.close()
    138 else:
    139 old_ip = ""
    140
    141 if ip != old_ip:
    142 f=open(ip_path, 'w')
    143 f.write(ip)
    144 f.close()
    145 domains = get_conf()
    146 if not domains:
    147 print 'no domain configured yet.'
    148 os._exit(1)
    149 for domain in domains:
    150 nsupdate(domain, ip)
    151
    152 time.sleep(60)
    153
    154
    155 ip=get_ip()
    156 domains = get_conf()
    157 if not domains:
    158 print 'no domain configured yet.'
    159 os._exit(1)
    160
    161 try:
    162 if os.fork() > 0: os._exit(0) # exit father...
    163 except OSError, error:
    164 print 'fork #1 failed: %d (%s)' % (error.errno, error.strerror)
    165 os._exit(1)
    166
    167 os.chdir('/')
    168 os.setsid()
    169 os.umask(0)
    170
    171 try:
    172 pid = os.fork()
    173 if pid > 0:
    174 f = open('/tmp/ddns.pid', 'w')
    175 f.write("%i" % pid)
    176 f.close()
    177 os._exit(0)
    178 except OSError, error:
    179 print 'fork #2 failed: %d (%s)' % (error.errno, error.strerror)
    180 os._exit(1)
    181
    182 update_dns()
    Posted by benoitc to python dns daemon ip shell ... saved by 4 persons ... 1 comments ... 1 year
showing 10, 25, 50 items per pages

Pages : 1

Flux RSS friendsnippetLatest snippets


More...