Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #82492

Re: Python Script to convert firewall rules

References <b23872cc-7b5c-4e38-96c1-ac907b6df7f1@googlegroups.com> <mailman.16883.1418348704.18130.python-list@python.org> <e0d8dd24-2256-4b89-972e-c1ebe82f72a9@googlegroups.com> <mailman.16931.1418437882.18130.python-list@python.org> <c89c75f8-190d-4678-b575-b92db5c38983@googlegroups.com>
Date 2014-12-13 18:35 -0700
Subject Re: Python Script to convert firewall rules
From Jason Friedman <jsf80238@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.16939.1418520955.18130.python-list@python.org> (permalink)

Show all headers | View raw


> Thanks for the reply. I am learning python using CBT nuggets for python. But If you can refer me some good course, that should be practical then it would be great.
>
> For my requirement, if you can give me the best approach to start with or high level steps or give me some sample cod, I really appreciate that.
>
Good, some other sources for learning:
https://docs.python.org/3/tutorial/
http://learnpythonthehardway.org/

Here's some code to get you started  (version 3.4.0):

"""
convert

set interfaces ge-0/0/0 unit 0 family inet address 10.17.10.1/24

to

interfaces {
    ge-2/0/5 {
        unit 0 {
            family inet {
                address 10.17.10.1/24;
            }
        }
    }
}
"""

class interface():
    attribute_name_list = ("ge", "unit", "family", "address")
    def __init__(self, ge, unit, family, address):
        self.ge = ge
        self.unit = unit
        self.family = family
        self.address = address

def convert(interface_list, indent=4):
    indentation = 0
    return_list = list()
    return_list.append(" " * indentation + "interfaces {")
    for interface in interface_list:
        for attribute_name in interface.attribute_name_list:
            indentation += indent
            text = "%s %s {" % (attribute_name, getattr(interface,
attribute_name))
            return_list.append(" " * indentation + text)
        while indentation > indent:
            indentation -= indent
            return_list.append(" " * indentation + "}")
        indentation -= indent
    return_list.append("}")
    return "\n".join(return_list)

if __name__ == "__main__":
    interface1 = interface("0/0/0", "0", "inet", "10.17.10.1/24")
    interface2 = interface("2/0/5", "0", "inet", "11.18.10.1/24")
    print(convert((interface1, interface2, )))

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Python Script to convert firewall rules Kashif Rana <kashifrana84@gmail.com> - 2014-12-10 10:51 -0800
  Re: Python Script to convert firewall rules Jason Friedman <jsf80238@gmail.com> - 2014-12-11 18:44 -0700
    Re: Python Script to convert firewall rules Kashif Rana <kashifrana84@gmail.com> - 2014-12-11 23:54 -0800
      Re: Python Script to convert firewall rules Jason Friedman <jsf80238@gmail.com> - 2014-12-12 19:31 -0700
        Re: Python Script to convert firewall rules Kashif Rana <kashifrana84@gmail.com> - 2014-12-13 09:41 -0800
          Re: Python Script to convert firewall rules Jason Friedman <jsf80238@gmail.com> - 2014-12-13 18:35 -0700
          Re: Python Script to convert firewall rules Kashif Rana <kashifrana84@gmail.com> - 2014-12-14 10:51 +0400
          Re: Python Script to convert firewall rules Jason Friedman <jsf80238@gmail.com> - 2014-12-14 22:07 -0700

csiph-web