[Tutor] Python Bind DNS Zone config

Nathan D'Elboux nathan.delboux at gmail.com
Wed May 3 23:11:19 EDT 2017


Thanks so much for your response. Ok so i now have the following code

new_domain = input("Enter domain you wish to blacklist: ")

print("""zone {} IN {
        type master;
        file "zones/1.2.3.4.zone";
        allow-transfer { none; };
        allow-query { test-test; };
};""".format(new_domain))

when i run the above code it prompts me to enter the domain so i do,
then the out put is a "Value error: Unexpected '{' in field name

So i comment out the .format statement and i get the exact output i
want but nothing in the {} statement

I need to either escape the opening { after the IN statement as the
.format(new_domain)) statement doesnt seem to be detecting i have the
{} ready to take the parameter new_domain?

Thanks for the link it certainly helped



On Wed, May 3, 2017 at 10:24 PM, boB Stepp <robertvstepp at gmail.com> wrote:
> Hello Nathan!
>
> On Wed, May 3, 2017 at 4:40 AM, Nathan D'Elboux
> <nathan.delboux at gmail.com> wrote:
>
>>
>> What i am trying to do is have the below syntax somehow stored into a block
>> of strings exactly in this syntax. I dont know what data type i should be
>> trying to store this as, a string obviously but how would i preserve the
>> syntax so it outputs in the identical way? The
>>
>> zone "." {
>> 6     type master;
>> 7     //type hint;
>> 8     file "/etc/bind/db.root.honeypot";
>> 9
>>
>>
>>
>>
>> };
>
> Do you know about triple quotes in Python?  They can be used when you
> wish to have a multi-line string.  You can use either single quotes:
>
> py3: my_string = '''Line one...
> ... Line two...
> ... Line three!'''
> py3: print(my_string)
> Line one...
> Line two...
> Line three!
>
> Or you could use three double quotes:
>
> py3: my_string = """Line one...
> ... Line two...
> ... Line three!"""
> py3: print(my_string)
> Line one...
> Line two...
> Line three!
>
>> In between the "." I will be asking a user for input to insert a domain in
>> plain ASCII and place it in that location of the zone file config and print
>> it to STD OUT on the terminal. Perhaps later another version of this i will
>> include writing directly to a file but for now i want to ensure the
>> inserting of string into specific position works and just print it out.
>>
>> So im thinking of something along the lines of
>>
>> User_input_Zone = str(input("Enter the domain you wish to block: ")
>
> Note:  The str() you use with input() is unnecessary.  input() always
> returns a string; no need to convert it to a string.  On the other
> hand if the input was an integer or a float, then you would need to
> use int() or float(), respectively.
>
>> Def zone_blacklist
>> New_zone = "zone "{}" {, user_input_zone
>> 6                  type master;
>> 7                  //type hint;
>> 8                  file "/etc/bind/db.root.honeypot";
>> 9 };"
>> Print(new_zone)
>>
>> What i was thinking of doing is having a place holder of {} in place of
>> where i want the domain to be inserted into but i dont know how to
>> structure the syntax of a zone file in a function within
>
> Have you looked into string formatting?  See:
>
> https://docs.python.org/3/tutorial/inputoutput.html
>
> in the Python Tutorial.  What you appear to be interested in falls a
> bit farther down the page.  And you can always google Python 3 string
> formatting.
>
> HTH!
>
> --
> boB


More information about the Tutor mailing list