Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.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.004 X-Spam-Evidence: '*H*': 0.99; '*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; 'family,': 0.16; 'indent': 0.16; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'skip:" 20': 0.27; 'header:In-Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'reply.': 0.31; 'that.': 0.31; 'indentation': 0.31; 'class': 0.32; 'interface': 0.32; 'text': 0.33; 'url:python': 0.33; 'sources': 0.33; 'skip:_ 10': 0.34; 'convert': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'thanks': 0.36; 'url:org': 0.36; 'should': 0.36; 'unit': 0.37; 'level': 0.37; 'skip:p 20': 0.39; 'url:3': 0.61; 'address': 0.63; 'high': 0.63; 'refer': 0.63; 'to:addr:gmail.com': 0.65; 'sample': 0.67; 'family': 0.73; '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=U0S/l3UIyQhBtBXc32nu38jGQSHZbPasF/awu+kAq1Y=; b=aUWW2xEpq7xHGHWNv5SWFY85+1cPnuLcDCUABnJ2FbqDE0vEG7mPd+r+7vuiSk6aIU keESTJZ7zNjxuE//9LoA3zq/4oZD9Nt0qlT3hVSRIYV7uyV/0nSz+hegnuO4bPsPk1qy SzPJfMOMKHC68/6t/95AIesFB2BRVw1Oq77xBsaXxbMZ9XRHqY74W1xF10BTQ5bre70s 0vmoqXVuk4Q27LXVHmMco1cKzmEqigrWHlFcBy5J7Q3iDbXXJH+Ll4Fji5MOIfSP08xH 3vUsomANoteeneNQaqoU5T2AzC/EHN5lZRWQkyT8gweKXEWpdkx2RBIvRUPlZcDtdWoF wmwA== MIME-Version: 1.0 X-Received: by 10.194.6.7 with SMTP id w7mr39103260wjw.25.1418520946364; Sat, 13 Dec 2014 17:35:46 -0800 (PST) In-Reply-To: References: Date: Sat, 13 Dec 2014 18:35:46 -0700 Subject: Re: Python Script to convert firewall rules From: Jason Friedman To: Kashif Rana Content-Type: text/plain; charset=UTF-8 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: 57 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1418520955 news.xs4all.nl 2829 [2001:888:2000:d::a6]:60508 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:82492 > 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, )))