def add_ip_range(self, ip_range, value): """Adds an entry to this map. ip_range can be in the following forms: "1.2.3.4" "1.2.3.0/8" ("1.2.3.4", "1.2.3.44") """ # Convert ranges in CIDR format into (start, end) tuple if isinstance(ip_range, six.string_types) and "/" in ip_range: # ignore bad value if not iptools.validate_cidr(ip_range): return ip_range = iptools.cidr2block(ip_range) # Find the integer representation of first 2 parts of the start and end IPs if isinstance(ip_range, tuple): # ignore bad ips if not iptools.validate_ip(ip_range[0]) or not iptools.validate_ip( ip_range[1]): return # Take the first 2 parts of the begin and end ip as integer start = iptools.ip2long(ip_range[0]) >> 16 end = iptools.ip2long(ip_range[1]) >> 16 else: start = iptools.ip2long(ip_range) >> 16 end = start # for each integer in the range add an entry. for i in range(start, end + 1): self.ip_ranges.setdefault(i, {})[iptools.IpRange(ip_range)] = value
def add_ip_range(self, ip_range, value): """Adds an entry to this map. ip_range can be in the following forms: "1.2.3.4" "1.2.3.0/8" ("1.2.3.4", "1.2.3.44") """ # Convert ranges in CIDR format into (start, end) tuple if isinstance(ip_range, six.string_types) and "/" in ip_range: # ignore bad value if not iptools.validate_cidr(ip_range): return ip_range = iptools.cidr2block(ip_range) # Find the integer representation of first 2 parts of the start and end IPs if isinstance(ip_range, tuple): # ignore bad ips if not iptools.validate_ip(ip_range[0]) or not iptools.validate_ip(ip_range[1]): return # Take the first 2 parts of the begin and end ip as integer start = iptools.ip2long(ip_range[0]) >> 16 end = iptools.ip2long(ip_range[1]) >> 16 else: start = iptools.ip2long(ip_range) >> 16 end = start # for each integer in the range add an entry. for i in range(start, end+1): self.ip_ranges.setdefault(i, {})[iptools.IpRange(ip_range)] = value
def __init__(self, cidr, low_gateway=False): if not iptools.validate_cidr(cidr): raise ConfigError("CIDR must be in proper format. e.g. 192.168.1.0/24") self.base, self.msk = cidr.split('/') self.mask = int(self.msk) self.num_addr = 2**(32-self.mask) self.first_address = iptools.ip2long(self.base) self.last_address = iptools.ip2long(self.base) + self.num_addr - 1 self.low_gateway = low_gateway
def getGateway(self): """ Return the gateway address of the CIDR range >>> network = Subnet('192.168.1.0/24') >>> network.getGateway '192.168.1.254' """ if self.low_gateway: return iptools.long2ip(iptools.ip2long(self.base) + 1) else: return iptools.long2ip(iptools.ip2long(self.base) + self.num_addr - 2)
def __getitem__(self, ip): # integer representation of first 2 parts base = iptools.ip2long(ip) >> 16 for ip_range, value in self.ip_ranges.get(base, {}).items(): if ip in ip_range: return value raise KeyError(ip)
def get_db_prep_value(self, value, connection, prepared=False): if value is None: return value elif isinstance(value, str): return iptools.ip2long(value) elif isinstance(value, int) or isinstance(value, long): return value
def get_prep_lookup(self, lookup_type, value): if value is None: return value elif isinstance(value, str) or isinstance(value, unicode): return iptools.ip2long(value) elif isinstance(value, int): return int else: assert False, "This should never happen"
def contains(self, ip): """ Check if the IP is in the subnet >>> network = Subnet('192.168.1.0/24') >>> network.contains('192.168.1.1') True :param ip: An IP address :type ip: str """ ip = str(ip) ip_as_dec = iptools.ip2long(ip) if (ip_as_dec >= self.first_address) and (ip_as_dec <= self.last_address): return True else: return False
vstatus[2].auth, vstatus[2].cci, vstatus[2].cgms, subscribed, available, copy_protected ) hdhr_ip = '10.0.8.215' hdhr_tuner = 1 udp_relay_listen_port = 1236 libhdhr = LibHdhr() device = libhdhr.device_create(HDHOMERUN_DEVICE_ID_WILDCARD, iptools.ip2long(hdhr_ip), hdhr_tuner) listener = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) listener.bind(('0.0.0.0', udp_relay_listen_port)) #listener.setblocking(False) #listener_fh=listener.fileno() localip = libhdhr.device_get_local_machine_addr(device) target = 'udp://%s:%d/' % (iptools.long2ip(localip), udp_relay_listen_port) libhdhr.device_set_tuner_target(device, target) dir_timestamp = time.strftime("%Y-%m-%d %H:%M:%S %Z") os.mkdir('/www/hdhr.adam.gs/scanner/%s' % dir_timestamp) req = urllib2.Request(
copy_protected = 't' else: copy_protected = 'f' return 'vchannel=%s:name=%s:auth=%s:cci=%s:cgms=%s:subscribed=%s:avaliable=%s:copy_protected=%s' % ( vstatus[2].vchannel, vstatus[2].name, vstatus[2].auth, vstatus[2].cci, vstatus[2].cgms, subscribed, available, copy_protected) hdhr_ip = '10.0.8.215' hdhr_tuner = 1 udp_relay_listen_port = 1236 libhdhr = LibHdhr() device = libhdhr.device_create(HDHOMERUN_DEVICE_ID_WILDCARD, iptools.ip2long(hdhr_ip), hdhr_tuner) listener = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) listener.bind(('0.0.0.0', udp_relay_listen_port)) #listener.setblocking(False) #listener_fh=listener.fileno() localip = libhdhr.device_get_local_machine_addr(device) target = 'udp://%s:%d/' % (iptools.long2ip(localip), udp_relay_listen_port) libhdhr.device_set_tuner_target(device, target) dir_timestamp = time.strftime("%Y-%m-%d %H:%M:%S %Z") os.mkdir('/www/hdhr.adam.gs/scanner/%s' % dir_timestamp) req = urllib2.Request(url='http://hdhr.adam.gs/lineup.xml') fh = urllib2.urlopen(req)
from hdhomerun import * hdhrs = [ #{'ip':'10.0.8.215','tuner':0}, {'ip':'10.0.8.215','tuner':1}, {'ip':'10.0.8.215','tuner':2}, ] local_port_base = 12340 local_port = local_port_base for hdhr in hdhrs: hdhr['local_port'] = local_port hdhr['busy'] = False hdhr['ip_long'] = iptools.ip2long(hdhr['ip']) local_port += 1 class ThreadingHTTPServer(ThreadingMixIn, BaseHTTPServer.HTTPServer): pass class HDHRHTTPHandler(BaseHTTPServer.BaseHTTPRequestHandler): def do_POST(self): length = int(self.headers.getheader('content-length')) xml = self.rfile.read(length) try: self.xml = ET.fromstring(xml) self.log_message("xml: %s" % self.xml) except: self.log_message("unable to parse XML")
copy_protected = True else: copy_protected = False print("vstatus:") print(" vchannel %s" % (vstatus[2].vchannel)) print(" name %s" % (vstatus[2].name)) print(" auth %s" % (vstatus[2].auth)) print(" cci %s" % (vstatus[2].cci)) print(" cgms %s" % (vstatus[2].cgms)) print(" subscribed %s" % (subscribed)) print(" available %s" % (available)) print(" copy_protected %s" % (copy_protected)) libhdhr = LibHdhr() device = libhdhr.device_create(HDHOMERUN_DEVICE_ID_WILDCARD, iptools.ip2long('10.0.8.211'), 0) print iptools.long2ip(libhdhr.device_get_local_machine_addr(device)) pp_vstatus(libhdhr, device) libhdhr.device_set_tuner_vchannel(device, '802') time.sleep(1) pp_vstatus(libhdhr, device) libhdhr.device_set_tuner_vchannel(device, '1216') time.sleep(1) pp_vstatus(libhdhr, device)
index = 1 for unit in units: if bytes < math.pow(1000, index): return '%.2f %s' % ((bytes / math.pow(1000, index - 1)), unit) index = index + 1 return False udp_relay_listen_port = 1234 udp_control_listen_port = 1235 hdhr_ip = '10.0.8.215' hdhr_tuner = 0 libhdhr = LibHdhr() device = libhdhr.device_create(HDHOMERUN_DEVICE_ID_WILDCARD, iptools.ip2long(hdhr_ip), hdhr_tuner) localip = libhdhr.device_get_local_machine_addr(device) target = 'udp://%s:%d/' % (iptools.long2ip(localip), udp_relay_listen_port) libhdhr.device_set_tuner_target(device, target) control = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) control.bind(('::0', udp_control_listen_port)) control.setblocking(False) control_fh = control.fileno() listener = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) listener.bind(('0.0.0.0', udp_relay_listen_port)) listener.setblocking(False) listener_fh = listener.fileno()
if vstatus[2].copy_protected == 1: copy_protected = True else: copy_protected = False print ("vstatus:") print (" vchannel %s" % (vstatus[2].vchannel)) print (" name %s" % (vstatus[2].name)) print (" auth %s" % (vstatus[2].auth)) print (" cci %s" % (vstatus[2].cci)) print (" cgms %s" % (vstatus[2].cgms)) print (" subscribed %s" % (subscribed)) print (" available %s" % (available)) print (" copy_protected %s" % (copy_protected)) libhdhr = LibHdhr() device = libhdhr.device_create(HDHOMERUN_DEVICE_ID_WILDCARD, iptools.ip2long("10.0.8.211"), 0) print iptools.long2ip(libhdhr.device_get_local_machine_addr(device)) pp_vstatus(libhdhr, device) libhdhr.device_set_tuner_vchannel(device, "802") time.sleep(1) pp_vstatus(libhdhr, device) libhdhr.device_set_tuner_vchannel(device, "1216") time.sleep(1) pp_vstatus(libhdhr, device)