Python Script to convert firewall rules

Kashif Rana kashifrana84 at gmail.com
Sun Dec 14 01:51:07 EST 2014


Hi Jason

Thank you very much. Appreciated ! But the first requirement was to convert
format1 to format2 as below:

set interface ethernet2/5 ip 10.17.10.1/24 (format 1)
set interfaces ge-0/0/0 unit 0 family inet address 10.17.10.1/24 (format 2)
(set, interface, ip) = (set, interfaces, family inet address)
But some values are variable and should ask the user to convert manually
like ethernet2/5 equal to ge-0/0/0 or ge-0/0/1 or ge-0/0/2
And some values keep as it is like 10.17.10.1/24

Thanks and Regards,

Kashif
On Sun, Dec 14, 2014 at 5:35 AM, Jason Friedman <jsf80238 at gmail.com> wrote:
>
> > 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, )))
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20141214/65e43192/attachment.html>


More information about the Python-list mailing list