Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!us.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.013 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'python.': 0.02; 'interfaces': 0.04; 'subject:Python': 0.06; '"""': 0.07; 'great.': 0.07; '"__main__":': 0.09; '__name__': 0.09; 'cc:addr:python- list': 0.11; 'python': 0.11; 'def': 0.12; 'address)': 0.16; 'address):': 0.16; 'family,': 0.16; 'indent': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'email addr:gmail.com>': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; '>': 0.26; 'appreciated': 0.26; 'skip:" 20': 0.27; 'values': 0.27; 'header :In-Reply-To:1': 0.27; 'am,': 0.29; 'dec': 0.30; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; 'reply.': 0.31; 'that.': 0.31; '"': 0.31; 'indentation': 0.31; 'class': 0.32; 'interface': 0.32; 'text': 0.33; 'url:python': 0.33; 'skip:& 30': 0.33; 'sources': 0.33; 'skip:_ 10': 0.34; 'convert': 0.35; 'equal': 0.35; 'requirement': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; '14,': 0.36; 'interface,': 0.36; 'much.': 0.36; 'thanks': 0.36; 'url:org': 0.36; 'should': 0.36; 'unit': 0.37; 'level': 0.37; 'skip:& 10': 0.38; 'thank': 0.38; 'jason': 0.38; 'skip:& 20': 0.39; 'skip:p 20': 0.39; 'manually': 0.60; 'url:3': 0.61; 'first': 0.61; 'address': 0.63; 'high': 0.63; 'refer': 0.63; 'to:addr:gmail.com': 0.65; 'sample': 0.67; 'url:11': 0.68; 'url:17': 0.68; 'url:18': 0.68; 'below:': 0.68; 'skip:r 30': 0.69; 'family': 0.73; 'url:self': 0.84; 'good,': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=iU6Z0EIZ6GGTDVRlcachHVRdMoizxRKbH4MyRlSe2J4=; b=H9BkPm8fIu6XFUOXWWB1UZHG2FNDbdo8CHe4vtpfJjY4wOZ7zTcsA3rZSOoPDGZTL/ mQ4aJAMH3OpU2zZ4mYdzo8WHeEQTa309qqk800NrXnbNliWlUJX1E2atvOdd5w+vFs7r zkwKnPq4D3Qg5SEVJMnV7TATrS8GDF13WgIp1SCJeqh6hwRcfliYFJFP/tSAyMdATs7D bQQCKCSIHvQrutA9pj9tFV3+Qy1B1uRlU9FmTVHadYmGD9KOEROEElc6lATEWPyDKLXG hUmPb5YpAOUA7H78EHDmhKj81hXymG4NOV2m26hfnC05h2jiWAd+Zxi3yukiUug8T49d oQqA== MIME-Version: 1.0 X-Received: by 10.107.13.205 with SMTP id 196mr15168822ion.15.1418539867457; Sat, 13 Dec 2014 22:51:07 -0800 (PST) In-Reply-To: References: Date: Sun, 14 Dec 2014 10:51:07 +0400 Subject: Re: Python Script to convert firewall rules From: Kashif Rana To: Jason Friedman Content-Type: multipart/alternative; boundary=001a113feec4e67ac3050a278cc0 Cc: python-list X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 154 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1418539876 news.xs4all.nl 2831 [2001:888:2000:d::a6]:46665 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:82493 --001a113feec4e67ac3050a278cc0 Content-Type: text/plain; charset=UTF-8 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 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, ))) > --001a113feec4e67ac3050a278cc0 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Hi Jason
=C2=A0
Thank you very much. Appreciated ! But the first requirement was to co= nvert format1 to format2 as below:
=C2=A0
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) =3D (set, interfaces, family inet address)
But some values are variable and should ask the user to convert manual= ly 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
=C2=A0
Thanks and Regards,
=C2=A0
Kashif
On Sun, Dec 14, 2014 at 5:35 AM, Jason Friedman = <jsf80238@gmail.com> wrote:=20
> Thanks for the reply. I am learn= ing python using CBT nuggets for python. But If you can refer me some good = course, that should be practical then it would be great.
>
> Fo= r my requirement, if you can give me the best approach to start with or hig= h 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 star= ted=C2=A0 (version 3.4.0):

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

to

interfaces {
=C2=A0 =C2=A0 ge-2/0/5 {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 u= nit 0 {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 family inet {
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 address 10.17.10.1/24;
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 }
=C2=A0 =C2=A0 =C2=A0 =C2=A0 }
=C2=A0 = =C2=A0 }
}
"""

class interface():
=C2= =A0 =C2=A0 attribute_name_list =3D ("ge", "unit", "= ;family", "address")
=C2=A0 =C2=A0 def __init__(self, ge,= unit, family, address):
=C2=A0 =C2=A0 =C2=A0 =C2=A0 self.ge =3D ge
=C2=A0 =C2=A0 =C2=A0 =C2= =A0 self.unit =3D unit
=C2=A0 =C2=A0 =C2=A0 =C2=A0 self.family =3D famil= y
=C2=A0 =C2=A0 =C2=A0 =C2=A0 self.address =3D address

def conver= t(interface_list, indent=3D4):
=C2=A0 =C2=A0 indentation =3D 0
=C2=A0= =C2=A0 return_list =3D list()
=C2=A0 =C2=A0 return_list.append(" &= quot; * indentation + "interfaces {")
=C2=A0 =C2=A0 for interf= ace in interface_list:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 for attribute_name in= interface.attribute_name_list:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 indentation +=3D indent
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 te= xt =3D "%s %s {" % (attribute_name, getattr(interface,
attribu= te_name))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return_list.append(&= quot; " * indentation + text)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 while ind= entation > indent:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 indentat= ion -=3D indent
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return_list.ap= pend(" " * indentation + "}")
=C2=A0 =C2=A0 =C2=A0 = =C2=A0 indentation -=3D indent
=C2=A0 =C2=A0 return_list.append("}&= quot;)
=C2=A0 =C2=A0 return "\n".join(return_list)

if _= _name__ =3D=3D "__main__":
=C2=A0 =C2=A0 interface1 =3D interf= ace("0/0/0", "0", "inet", "10.17.10.1/24")
=C2=A0 = =C2=A0 interface2 =3D interface("2/0/5", "0", "ine= t", "11.18.10.= 1/24")
=C2=A0 =C2=A0 print(convert((interface1, interface2, )))=
--001a113feec4e67ac3050a278cc0--