-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip_to_db.py
56 lines (34 loc) · 1.19 KB
/
ip_to_db.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
#!/usr/bin/python
import pymongo
import datetime
import sys
import getopt
import argparse
from pymongo import MongoClient
#client connect string
client = MongoClient('mongodb://user:pass@mongo-host:port')
db = client.ip_list
def read_list(args):
#parses the portion of the variable we will print and then escapes the other text we will print with it
w = args.dc +"/"+ args.dc + ".list"
#opens the file and adds it to an array. Also drops the newline character. Return lines allows the list to be used later
with open(w) as f:
lines = f.read().splitlines()
f.close()
return lines
def add_ip(lines):
for ip in lines:
post = {"block": ip, "status": "unused"}
def main(args):
lines = read_list(args)
posts = args.dc +"_list"
post_id = posts.insert(add_ip(lines))
print post_id
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Add free IPs to database')
parser.add_argument("dc",choices=['iad', 'ord', 'dfw', 'lon', 'hkg', 'sjc', 'syd'], help='Enter the 3-letter code for one of the datacenters')
if len(sys.argv)==1:
parser.print_help()
sys.exit(1)
args = parser.parse_args()
main(args)