log: stdout if on tty, in logfile otherwise
This commit is contained in:
		
							
								
								
									
										29
									
								
								dyndomain
									
									
									
									
									
								
							
							
						
						
									
										29
									
								
								dyndomain
									
									
									
									
									
								
							@@ -201,6 +201,10 @@ def write_zone_list(zone_filename, zone, update):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
def log(msg):
 | 
					def log(msg):
 | 
				
			||||||
    stamp = time.strftime("%Y-%m-%d %H:%M:%S")
 | 
					    stamp = time.strftime("%Y-%m-%d %H:%M:%S")
 | 
				
			||||||
 | 
					    if sys.stdout.isatty():
 | 
				
			||||||
 | 
					        for line in msg.split('\n'):
 | 
				
			||||||
 | 
					            print("%s - %s" % (stamp, msg))
 | 
				
			||||||
 | 
					    else:
 | 
				
			||||||
        with open(log_filename, 'a') as logfile:
 | 
					        with open(log_filename, 'a') as logfile:
 | 
				
			||||||
            for line in msg.split('\n'):
 | 
					            for line in msg.split('\n'):
 | 
				
			||||||
                logfile.write("%s - %s\n" % (stamp, msg))
 | 
					                logfile.write("%s - %s\n" % (stamp, msg))
 | 
				
			||||||
@@ -226,7 +230,7 @@ def ovh_update_zone(domain, zone, update, sync_zone):
 | 
				
			|||||||
                    result = client.get('/domain/zone/{}/record'.format(domain),
 | 
					                    result = client.get('/domain/zone/{}/record'.format(domain),
 | 
				
			||||||
                                        fieldType=prot, subDomain=name)
 | 
					                                        fieldType=prot, subDomain=name)
 | 
				
			||||||
                    for id in result:
 | 
					                    for id in result:
 | 
				
			||||||
                        print('OVH: delete entry #{} {} {}'.format(id, name, prot))
 | 
					                        log('OVH: delete entry #{} {} {}'.format(id, name, prot))
 | 
				
			||||||
                        client.delete('/domain/zone/{}/record/{}'.format(domain, id))
 | 
					                        client.delete('/domain/zone/{}/record/{}'.format(domain, id))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        for prot, name, addr in update['delete']:
 | 
					        for prot, name, addr in update['delete']:
 | 
				
			||||||
@@ -238,43 +242,45 @@ def ovh_update_zone(domain, zone, update, sync_zone):
 | 
				
			|||||||
            for id in result:
 | 
					            for id in result:
 | 
				
			||||||
                r = client.get('/domain/zone/%s/record/%d' % (domain, id))
 | 
					                r = client.get('/domain/zone/%s/record/%d' % (domain, id))
 | 
				
			||||||
                if r['fieldType'] == prot and r['target'] == addr:
 | 
					                if r['fieldType'] == prot and r['target'] == addr:
 | 
				
			||||||
                    print("OVH: delete entry for %s %s %s (#%s)" % (name, prot, addr, id))
 | 
					                    log("OVH: delete entry for %s %s %s (#%s)" % (name, prot, addr, id))
 | 
				
			||||||
                    client.delete('/domain/zone/%s/record/%d' % (domain, id))
 | 
					                    client.delete('/domain/zone/%s/record/%d' % (domain, id))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        for prot, name, addr in update['add']:
 | 
					        for prot, name, addr in update['add']:
 | 
				
			||||||
            if not prot in ['A', 'AAAA']:
 | 
					            if not prot in ['A', 'AAAA']:
 | 
				
			||||||
                continue
 | 
					                continue
 | 
				
			||||||
            print("OVH: create entry for %s %s %s" % (name, prot, addr))
 | 
					            log("OVH: create entry for %s %s %s" % (name, prot, addr))
 | 
				
			||||||
            client.post('/domain/zone/%s/record' % domain,
 | 
					            client.post('/domain/zone/%s/record' % domain,
 | 
				
			||||||
                    fieldType=prot,
 | 
					                    fieldType=prot,
 | 
				
			||||||
                    subDomain=name,
 | 
					                    subDomain=name,
 | 
				
			||||||
                    target=addr,
 | 
					                    target=addr,
 | 
				
			||||||
                    ttl=60)
 | 
					                    ttl=60)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        print("OVH: Refresh zone %s" % domain)
 | 
					        log("OVH: Refresh zone %s" % domain)
 | 
				
			||||||
        client.post('/domain/zone/%s/refresh' % domain)
 | 
					        client.post('/domain/zone/%s/refresh' % domain)
 | 
				
			||||||
        return True
 | 
					        return True
 | 
				
			||||||
    except:
 | 
					    except:
 | 
				
			||||||
        print('OVH update error\n')
 | 
					        log('OVH update error\n')
 | 
				
			||||||
        return False
 | 
					        return False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def livebox_rule_id(words):
 | 
					def livebox_rule_id(words):
 | 
				
			||||||
    id='dyndomain_{}'.format('_'.join(words))
 | 
					    id='dyndomain_{}'.format('_'.join(words))
 | 
				
			||||||
    id = id.replace('.', '_')
 | 
					    id = id.replace('.', '_')
 | 
				
			||||||
    return id
 | 
					    return id
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def livebox_delete_port_nat(port, addr):
 | 
					def livebox_delete_port_nat(port, addr):
 | 
				
			||||||
    print('livebox: delete PortNat {} to {}'.format(port, addr))
 | 
					    log('livebox: delete PortNat {} to {}'.format(port, addr))
 | 
				
			||||||
    id = livebox_rule_id([port])
 | 
					    id = livebox_rule_id([port])
 | 
				
			||||||
    r = sysbus.requete('Firewall:deletePortForwarding', { 'id': id, 'origin': 'webui' })
 | 
					    r = sysbus.requete('Firewall:deletePortForwarding', { 'id': id, 'origin': 'webui' })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def livebox_delete_pinhole(port, addr):
 | 
					def livebox_delete_pinhole(port, addr):
 | 
				
			||||||
    print('livebox: delete pinhole {} to {}'.format(port, addr))
 | 
					    log('livebox: delete pinhole {} to {}'.format(port, addr))
 | 
				
			||||||
    id = livebox_rule_id([port, addr])
 | 
					    id = livebox_rule_id([port, addr])
 | 
				
			||||||
    r = sysbus.requete('Firewall:deletePinhole', { 'id': id, 'origin': 'webui' })
 | 
					    r = sysbus.requete('Firewall:deletePinhole', { 'id': id, 'origin': 'webui' })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def livebox_add_port_nat(port, addr):
 | 
					def livebox_add_port_nat(port, addr):
 | 
				
			||||||
    print('livebox: add PortNat {} to {}'.format(port, addr))
 | 
					    log('livebox: add PortNat {} to {}'.format(port, addr))
 | 
				
			||||||
    id = livebox_rule_id([port])
 | 
					    id = livebox_rule_id([port])
 | 
				
			||||||
    a = {
 | 
					    a = {
 | 
				
			||||||
        'id': id,
 | 
					        'id': id,
 | 
				
			||||||
@@ -288,7 +294,7 @@ def livebox_add_port_nat(port, addr):
 | 
				
			|||||||
    r = sysbus.requete('Firewall:setPortForwarding', a)
 | 
					    r = sysbus.requete('Firewall:setPortForwarding', a)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def livebox_add_pinhole(port, addr):
 | 
					def livebox_add_pinhole(port, addr):
 | 
				
			||||||
    print('livebox: add pinhole {} to {}'.format(port, addr))
 | 
					    log('livebox: add pinhole {} to {}'.format(port, addr))
 | 
				
			||||||
    id = livebox_rule_id([port, addr])
 | 
					    id = livebox_rule_id([port, addr])
 | 
				
			||||||
    a = {
 | 
					    a = {
 | 
				
			||||||
        'id': id,
 | 
					        'id': id,
 | 
				
			||||||
@@ -326,7 +332,7 @@ def send_update_mail(mail_to, mail_from, zone_domain, update, mail_ignore_list,
 | 
				
			|||||||
    if not okmail:
 | 
					    if not okmail:
 | 
				
			||||||
        return
 | 
					        return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    print('Send email to %s' % mail_to)
 | 
					    log('Send email to %s' % mail_to)
 | 
				
			||||||
    msg = EmailMessage()
 | 
					    msg = EmailMessage()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    msg['Subject'] = "Livebox update in %s" % zone_domain
 | 
					    msg['Subject'] = "Livebox update in %s" % zone_domain
 | 
				
			||||||
@@ -354,7 +360,6 @@ def send_update_mail(mail_to, mail_from, zone_domain, update, mail_ignore_list,
 | 
				
			|||||||
    s = smtplib.SMTP('localhost')
 | 
					    s = smtplib.SMTP('localhost')
 | 
				
			||||||
    s.send_message(msg)
 | 
					    s.send_message(msg)
 | 
				
			||||||
    s.quit()
 | 
					    s.quit()
 | 
				
			||||||
    #print(txt)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
load_conf()
 | 
					load_conf()
 | 
				
			||||||
@@ -366,7 +371,7 @@ if not ping(wan_hostname):
 | 
				
			|||||||
sysbus.load_conf()
 | 
					sysbus.load_conf()
 | 
				
			||||||
r = sysbus.auth(False)
 | 
					r = sysbus.auth(False)
 | 
				
			||||||
if not r:
 | 
					if not r:
 | 
				
			||||||
    print('Error: cannot authenticate on livebox')
 | 
					    log('Error: cannot authenticate on livebox')
 | 
				
			||||||
    sys.exit(1)
 | 
					    sys.exit(1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
hosts = get_hosts()
 | 
					hosts = get_hosts()
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user