-
-
Save anonymous/8f6a008d1b86965bd2bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys, MySQLdb | |
host = sys.argv[1] | |
hostcountry = host.split('.')[1] | |
def gen_net_suffix(i): | |
"Generate IP" | |
base_prefix = "10.100" | |
y = (i % 64) * 4 + 1 | |
x = i /64 | |
base_net = base_prefix + "." + str(x) | |
ip1 = base_net + "." + str(y) | |
ip2 = base_net + "." + str(y+1) | |
return (ip1,ip2) | |
def print_routes(): | |
# find routes where host is the source | |
cur.execute("""SELECT tblhosts.hostname, tblroutes.src_ip, h2.hostname, tblroutes.dst_ip | |
FROM tblroutes | |
INNER JOIN tblhosts ON tblroutes.src = tblhosts.id | |
INNER JOIN tblhosts h2 ON tblroutes.dst = h2.id | |
WHERE tblhosts.hostname = %s""", | |
(host)) | |
partners = cur.fetchall() | |
for partner in partners: | |
print partner[0],partner[1],partner[2],partner[3] | |
# find routes where host is the destination | |
cur.execute("""SELECT tblhosts.hostname, tblroutes.src_ip, h2.hostname, tblroutes.dst_ip | |
FROM tblroutes | |
INNER JOIN tblhosts ON tblroutes.dst = tblhosts.id | |
INNER JOIN tblhosts h2 ON tblroutes.src = h2.id | |
WHERE tblhosts.hostname = %s""", | |
(host)) | |
partners = cur.fetchall() | |
for partner in partners: | |
print partner[0],partner[3],partner[2],partner[1] | |
conn = MySQLdb.connect ('localhost','routes','c7TQPaRREmZgkpCdZayz','routes') | |
cur = conn.cursor() | |
# Check if host exists, if not generate routes | |
cur.execute ("SELECT count(*) as count FROM tblhosts WHERE hostname = %s",host) | |
row = cur.fetchone() | |
if row[0] > 0: | |
print_routes() | |
else: | |
cur.execute("INSERT INTO tblhosts(hostname) values (%s)",host) | |
conn.commit() | |
cur.execute("SELECT id FROM tblhosts WHERE hostname = %s",host) | |
row = cur.fetchone() | |
hostid = row[0] | |
cur.execute("SELECT * from tblhosts WHERE hostname NOT LIKE %s",('%' + hostcountry + '%')) | |
partners = cur.fetchall() | |
cur.execute("SELECT count(*) as count FROM tblroutes") | |
result = cur.fetchone() | |
if result[0] is None: | |
n = 0 | |
else: | |
n = int(result[0]) | |
for partner in partners: | |
ips = gen_net_suffix(n) | |
ip1 = ips[0] | |
ip2 = ips[1] | |
n += 1 | |
cur.execute("INSERT INTO tblroutes(src,src_ip,dst,dst_ip) VALUES (%s,%s,%s,%s)",(hostid,ip1,partner[0],ip2)) | |
conn.commit() | |
print_routes() | |
cur.close() | |
conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment