<s> import argparse <EOL> from jinja2 import Environment <EOL> from jinja2 import FileSystemLoader <EOL> from ncclient import manager <EOL> import netaddr <EOL> import os . path <EOL> import re <EOL> import sys <EOL> HOST = '<STR_LIT>' <EOL> PORT = <NUM_LIT> <EOL> USER = '<STR_LIT:root>' <EOL> PASS = '<STR_LIT>' <EOL> BASE = '<STR_LIT>' <EOL> INT_ID = '<STR_LIT:3>' <EOL> FILE = '<STR_LIT>' <EOL> TEMPLATE = '<STR_LIT>' <EOL> def create_xml_config ( interface_template , xml_file , interface , int_id , vlan , ip , mask ) : <EOL> """<STR_LIT>""" <EOL> env = Environment ( loader = FileSystemLoader ( '<STR_LIT>' ) ) <EOL> template = env . get_template ( interface_template ) <EOL> rendered = template . render ( base = interface , interface_id = int_id , <EOL> vlan_id = vlan , ip = ip , subnet_mask = mask ) <EOL> with open ( xml_file , "<STR_LIT:wb>" ) as f : <EOL> f . write ( rendered ) <EOL> if os . path . isfile ( xml_file ) is not True : <EOL> print ( "<STR_LIT>" ) <EOL> sys . exit ( ) <EOL> def create_vlan ( host , port , user , password , interface , int_id , vlan , ip , mask , template , config ) : <EOL> """<STR_LIT>""" <EOL> intfc = re . compile ( r'<STR_LIT>' ) <EOL> m = intfc . match ( interface + int_id ) <EOL> if m is None : <EOL> print ( "<STR_LIT>" , BASE ) <EOL> sys . exit ( ) <EOL> create_xml_config ( template , config , interface , int_id , vlan , ip , mask ) <EOL> with manager . connect ( host = host , port = port , username = user , password = password , <EOL> hostkey_verify = False , device_params = { '<STR_LIT:name>' : '<STR_LIT:default>' } , <EOL> allow_agent = False , look_for_keys = False ) as m : <EOL> with open ( config ) as f : <EOL> try : <EOL> rpc_reply = m . edit_config ( target = '<STR_LIT>' , config = f . read ( ) ) <EOL> except Exception as e : <EOL> print ( "<STR_LIT>" ) <EOL> print ( e ) <EOL> sys . exit ( ) <EOL> if rpc_reply . ok is not True : <EOL> print ( "<STR_LIT>" ) <EOL> sys . exit ( ) <EOL> def main ( ) : <EOL> """<STR_LIT>""" <EOL> parser = argparse . ArgumentParser ( ) <EOL> parser . add_argument ( '<STR_LIT>' , help = "<STR_LIT>" , type = int ) <EOL> parser . add_argument ( '<STR_LIT>' , help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , default = TEMPLATE , help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT:-c>' , default = FILE , help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , default = BASE , help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , default = INT_ID , help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , default = USER , help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , default = PASS , help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , default = PORT , help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , default = HOST , help = "<STR_LIT>" ) <EOL> args = parser . parse_args ( ) <EOL> if args . vlan < <NUM_LIT:1> or args . vlan > <NUM_LIT> : <EOL> parser . print_usage ( ) <EOL> print ( "<STR_LIT>" % str ( args . vlan ) ) <EOL> sys . exit ( ) <EOL> try : <EOL> ip = netaddr . IPNetwork ( args . prefix ) <EOL> except netaddr . core . AddrFormatError as e : <EOL> parser . print_usage ( ) <EOL> print ( e ) <EOL> sys . exit ( ) <EOL> return create_vlan ( args . host , args . port , args . user , args . password , args . interface , <EOL> args . id , args . vlan , ip . ip , ip . netmask , args . template , args . config ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> sys . exit ( main ( ) ) </s>
