|
| 1 | +import config |
| 2 | +import ipcalc |
| 3 | +import sqlite3 |
| 4 | +import sys |
| 5 | +import tempita |
| 6 | +from tempita import bunch |
| 7 | +import hashlib |
| 8 | + |
| 9 | +# Note: To add new variables, the generate function will need to |
| 10 | +# be modified as well |
| 11 | + |
| 12 | +password = "" |
| 13 | +enable = "" |
| 14 | +snmp_ro = "" |
| 15 | +snmp_salt = "" |
| 16 | +radius = "" |
| 17 | +# NOTE(bluecmd): 2950 doesn't support VLAN aware context, which means that |
| 18 | +# WhereAmI and dhmon needs v2. No reason to have v3 in that case. |
| 19 | +#snmp_user = "" |
| 20 | +#snmp_auth = "" |
| 21 | +#snmp_priv = "" |
| 22 | +wifi_vlanid = 851 |
| 23 | + |
| 24 | +# Enable this if we cannot set special option82 tags |
| 25 | +franken_net_switches = [ ] |
| 26 | + |
| 27 | +# If you have franken net, you need snmpv3 credentials to dist |
| 28 | +# NOTE: THERE IS NO NEED TO USE THIS IF is_franken_net == False |
| 29 | +snmpv3_username = '' |
| 30 | +snmpv3_auth = '' |
| 31 | +snmpv3_priv = '' |
| 32 | + |
| 33 | +models = { |
| 34 | + "WS-C2950T-24" : bunch(template="switchconfig/c2950t.cfg",eth=24), |
| 35 | + "WS-C2950G-24-EI" : bunch(template="switchconfig/c2950t.cfg",eth=24), |
| 36 | + "WS-C2950T-48-SI" : bunch(template="switchconfig/c2950t.cfg",eth=48), |
| 37 | + "WS-C2960G-48TC-L" : bunch(template="switchconfig/c2960g.cfg",eth=48), |
| 38 | + "WS-C2960X-48TS-L" : bunch(template="switchconfig/c2960x.cfg",eth=48), |
| 39 | +} |
| 40 | + |
| 41 | +wifi_switches = [ ] |
| 42 | + |
| 43 | +#wifi_switches = [ |
| 44 | +# "B01-A", "B05-A", "B10-A", "B16-A", "B22-C", "B26-C", "B31-C", "B37-C", |
| 45 | +# "C04-A", "C08-A", "C12-A", "C17-C", "C21-C", "C25-C", "C29-C", |
| 46 | +# "D03-A", "D09-B", "D19-B", "D23-A", "D29-A", "D34-C", "D40-C", "D44-C", "D48-C", "D56-C", "D62-C" |
| 47 | +#] |
| 48 | + |
| 49 | +# Files to be served as they are to all devices |
| 50 | +static_files = { |
| 51 | + "c2950.bin" : "ios/c2950-i6k2l2q4-mz.121-22.EA14.bin", |
| 52 | +} |
| 53 | + |
| 54 | +# =============================================================== |
| 55 | +# Do not change below this if you do not know what you're doing! |
| 56 | +# =============================================================== |
| 57 | + |
| 58 | +def generate(switch, model_id): |
| 59 | + model = config.models[model_id] |
| 60 | + |
| 61 | + mgmt, vlanid = parse_metadata(switch) |
| 62 | + if mgmt is None: |
| 63 | + raise Exception("The switch " + switch + " was not found in ipplan") |
| 64 | + |
| 65 | + cfg = tempita.Template(file(model.template).read()) |
| 66 | + return \ |
| 67 | + cfg.substitute( |
| 68 | + hostname=switch, |
| 69 | + model=model, |
| 70 | + mgmt_ip=mgmt['ip'], |
| 71 | + mgmt_mask=mgmt['mask'], |
| 72 | + mgmt_gw=mgmt['gw'], |
| 73 | + mgmt_vlanid=mgmt['vlanid'], |
| 74 | + vlanid=vlanid, |
| 75 | + wifi_switches=config.wifi_switches, |
| 76 | + wifi_vlanid=config.wifi_vlanid, |
| 77 | + password=config.password, |
| 78 | + enable=config.enable, |
| 79 | + radius=config.radius, |
| 80 | + snmp_ro=config.snmp_ro, |
| 81 | + snmp_rw=hashlib.sha1(config.snmp_salt + mgmt['ip']).hexdigest(), |
| 82 | +# snmp_user=config.snmp_user, |
| 83 | +# snmp_auth=config.snmp_auth, |
| 84 | +# snmp_priv=config.snmp_priv |
| 85 | + ) |
| 86 | + |
| 87 | +def parse_metadata(switch): |
| 88 | + sql = '''SELECT n_mgmt.ipv4_txt, h.ipv4_addr_txt, n_mgmt.ipv4_gateway_txt, |
| 89 | +n_mgmt.vlan, n.vlan FROM active_switch as s, network as n, host as h, |
| 90 | +network as n_mgmt WHERE s.switch_name LIKE ? AND n.node_id = s.node_id |
| 91 | +AND h.name = s.switch_name AND n_mgmt.node_id = h.network_id''' |
| 92 | + |
| 93 | + db = sqlite3.connect('/etc/ipplan.db') |
| 94 | + cursor = db.cursor() |
| 95 | + |
| 96 | + network_str, mgmt_ip, gateway, mgmt_vlan, vlan = cursor.execute( |
| 97 | + sql, ('%s%%' % switch.lower(),)).fetchone() |
| 98 | + |
| 99 | + network = ipcalc.Network(network_str) |
| 100 | + |
| 101 | + mgmt = {} |
| 102 | + mgmt['ip'] = mgmt_ip |
| 103 | + mgmt['mask'] = str(network.netmask()) |
| 104 | + mgmt['gw'] = str(network.host_first()) |
| 105 | + mgmt['vlanid'] = mgmt_vlan |
| 106 | + |
| 107 | + return mgmt, vlan |
| 108 | + |
| 109 | +if __name__ == '__main__': |
| 110 | + if len(sys.argv) == 3 and sys.argv[1] == 'dump-snmp-rw': |
| 111 | + mgmt, _ = parse_metadata(sys.argv[2]) |
| 112 | + print hashlib.sha1(config.snmp_salt + mgmt['ip']).hexdigest() |
| 113 | + else: |
| 114 | + print generate("D23-A", "WS-C2950T-24") |
0 commit comments