python - Extracting BIND parameters to build a JSON query -
i have file exported bind containing tsig values 500 domain names. need repurpose data json rest api query. bind data formatted so:
// secondary-example.com. key "2000000000000.key." { algorithm hmac-md5; secret "ahashedvalue="; }; zone "secondary-example.com." { type slave; file "sec/secondary-example.com."; allow-transfer { 1.1.1.1; 1.1.2.2; }; also-notify { 1.1.1.1; 2.2.2.2; }; masters { 1.2.3.4 key 2000000000000.key.; }; };
from need extract key, zone , secret. here's example api request.
{ "properties":{ "name":"secondary-example.com.", "accountname":"example", "type":"secondary" }, "secondarycreateinfo":{ "primarynameservers":{ "nameserveriplist":{ "nameserverip1":{ "ip":"1.2.3.4", "tsigkey":"2000000000000.key.", "tsigkeyvalue":"ahashedvalue=" } } } } }
i'm having difficulty crafting regular expression appropriate scenario. i'm looking construct json in python script , send request through postman.
i spent couple days reading on regex , figured out solution. so, each of "zones" began comment... e.g. "secondary-example.com"... , each set of bind info 17 lines long exactly. solution hackey , assumes data correct, managed work.
separate zones chunks of text.
zones = [] cur_zone = '' f = open(bind_file).readlines() line in f: if line[0:2] == '//': zones.append(cur_zone) cur_zone = '' else: cur_zone = cur_zone + line zones.pop(0) # drop first list item, it's empty
iterate through chunks , match needed parameters.
for z in zones: z_lines = z.splitlines() # regex patterns make required parameters key = re.findall('\"(.*)\"', z_lines[0])[0] secret = re.findall('\"(.*)\"', z_lines[2])[0] name = re.findall('\"(.*)\"', z_lines[5])[0] master = re.findall('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', z_lines[15])[0]
Comments
Post a Comment