Python - MySql query for ip address within cidr range -


ok need little bump here. have mysql database, within db, have table ip address stored inet_aton. have ip cidr ranges in table. such 192.168.0.0/24 or /8 etc. i'm using python query db see if single /32 ip address in db. i'm trying code this:

def ip_in_networks(ip, networks):      found_net_type = ''     found_template = ''       ip_as_int = unpack('!i', inet_aton(ip))[0]      (network, netmask, net_type, template) in networks:           if (ip_as_int & netmask) == network:              # are, awesome             found_net_type = net_type             found_template = template             break def fetch_networks(r_conn):     '''this returns list of networks [[network, netmask], ...]'''     nets = []     r_cur = r_conn.cursor()     sql = 'select n.network, n.netmask, l.lookup_value, l.template '     sql += 'from network n, lookup l '     sql += 'where n.network_type_id = l.id'     r_cur.execute(sql)     row in r_cur:         nets.append([row[0], row[1], row[2], row[3]])     return nets  if __name__ == '__main__':  ''' code removed'''    "connect db"   networks = fetch_networks(r_conn)   ip = sys.argv[1]    net_type, template = ip_in_networks(ip, networks)    if net_type:     '''if net_type means found ip address ini db, something, in case         log found message'' 

the error getting type error unpack non sequence.

i'm trying stick standard python 2.7 library , mysql.

i'm missing simple i'm sure. i'd change code if needed. have many ip ranges break them out ip address ip address , see if ip within range.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -