-
Notifications
You must be signed in to change notification settings - Fork 136
/
utils.py
136 lines (115 loc) · 4.11 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python
# coding=utf-8
"""
Copyright (C) 2010-2013, Ryan Fan <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
"""
import socket
from socket import error as socket_error
import sys
from datetime import datetime
import uuid
import requests
class DDNSUtils(object):
# To support "*" subdomain definition,
# We need generate a non-exist subdomain name
# Here we generate a random UUID for that
#fake_subdomain = ''.join([random.choice(string.lowercase) for i in xrange(12)])
RANDOM_UUID = uuid.uuid4().hex
"""
Utils class wrapper
"""
@staticmethod
def err(msg):
"""
Output error message
:param msg: Message to be displayed
"""
sys.stderr.write("{0}\t[ERROR]\t{1}\n".format(DDNSUtils.get_current_time(), msg))
@staticmethod
def info(msg):
"""
Output informative message
:param msg: Message to be displayed
"""
sys.stdout.write("{0}\t[INFO]\t{1}\n".format(DDNSUtils.get_current_time(), msg))
@staticmethod
def err_and_exit(msg):
"""
Output error message and exit
:param msg: Message to be displayed
"""
sys.stderr.write("{0}\t[ERROR]\t{1}\n".format(DDNSUtils.get_current_time(), msg))
sys.exit(1)
@classmethod
def get_current_public_ip(cls):
"""
Get current public IP
@return IP address or None
"""
try:
ret = requests.get("http://members.3322.org/dyndns/getip")
except requests.RequestException as ex:
cls.err("network problem:{0}".format(ex))
return None
if ret.status_code != requests.codes.ok:
cls.err("Failed to get current public IP: {0}\n{1}" \
.format(ret.status_code, ret.content))
return None
return ret.content.decode('utf-8').rstrip("\n")
@classmethod
def get_interface_address(cls, ifname):
import netifaces as ni
try:
ip = ni.ifaddresses(ifname)[ni.AF_INET][0]['addr']
return ip
except KeyError:
cls.err("Can't find the interface {}".format(ifname))
return None
@classmethod
def get_interface_ipv6_address(cls, ifname):
import netifaces as ni
try:
ip = ni.ifaddresses(ifname)[ni.AF_INET6][0]['addr']
return ip
except KeyError:
cls.err("Can't find the interface {}".format(ifname))
return None
@classmethod
def get_dns_resolved_ip(cls, subdomain, domainname):
"""
Get current IP address resolved by DNS server
:param subdomain: sub domain
:param domainname: domain name
:return: IP address or None
"""
ip_addr = None
try:
if subdomain == "@":
hostname = domainname
elif subdomain == "*":
hostname = "{0}.{1}".format(cls.RANDOM_UUID, domainname)
else:
hostname = "{0}.{1}".format(subdomain, domainname)
ip_addr = socket.gethostbyname(hostname)
except socket_error as ex:
cls.err("DomainRecord[{0}] cannot be resolved because of:{1}" \
.format(hostname, ex))
return ip_addr
@staticmethod
def get_current_time():
"""
Get human readable standard timestamp
:return: timestamp string
"""
return datetime.now().strftime("%Y-%m-%d %H:%M:%S")