network/salt/switches/3com-4200G.expect

152 lines
3.8 KiB
Plaintext
Raw Normal View History

2016-11-13 03:19:54 +01:00
{# http://h20628.www2.hp.com/km-ext/kmcsdirect/emr_na-c02586144-1.pdf #}
{%- macro increment(dct, key, inc=1) -%}
{% if dct.update({key: dct[key] + inc}) %} {% endif %}
{%- endmacro -%}
2016-11-13 03:19:54 +01:00
{%- import_yaml "netmasks.yaml" as netmasks -%}
#!/usr/bin/expect -f
spawn telnet {{ pillar['hosts-inet']['mgmt'][hostname] }}
expect "Password:"
2016-11-16 01:17:28 +01:00
send "{{ switch['password'] }}\r"
2016-11-13 03:19:54 +01:00
expect ">"
send "system-view\r"
expect "]"
send "sysname {{ hostname }}\r"
expect "]"
send "user-interface vty 0 4\r"
expect "ui-vty0-4]"
send "screen-length 0\r"
expect "ui-vty0-4]"
send "user privilege level 3\r"
expect "ui-vty0-4]"
2016-11-16 01:17:28 +01:00
send "set authentication password simple {{ switch['password'] }}\r"
2016-11-13 03:19:54 +01:00
expect "ui-vty0-4]"
send "quit\r"
expect "{{ hostname }}]"
send "local-user admin\r"
expect -- "-luser-admin]"
2016-11-16 01:17:28 +01:00
send "password simple {{ switch['password'] }}\r"
2016-11-13 03:19:54 +01:00
expect -- "-luser-admin]"
send "quit\r"
expect "{{ hostname }}]"
2019-08-15 23:39:45 +02:00
{# Enable logging #}
send "info-center enable\r"
expect "]"
send "info-center loghost {{logging}} channel loghost facility local6\r"
expect "]"
send "info-center source default channel loghost log level informational\r"
expect "]"
2016-11-13 03:19:54 +01:00
{%- for name, vlan in pillar['vlans'].items() %}
send "vlan {{ vlan }}\r"
expect -- "-vlan{{ vlan }}]"
2016-11-29 17:36:11 +01:00
send "name {{ name }}\r"
2016-11-13 03:19:54 +01:00
expect -- "-vlan{{ vlan }}]"
2016-11-29 02:24:13 +01:00
{%- if name == 'mgmt' %}
2016-11-13 03:19:54 +01:00
{# Actually only used for mgmt_vlan, switches are not routers #}
send "interface Vlan-interface {{ vlan }}\r"
expect "]"
2016-11-14 21:49:38 +01:00
{%- set net_hosts = pillar['hosts-inet'].get(name) %}
{%- set ipaddr = net_hosts and net_hosts.get(hostname) %}
{%- if ipaddr %}
2016-11-13 03:19:54 +01:00
send "ip address {{ ipaddr }} {{ netmasks[pillar['subnets-inet'][name].split('/')[1]] }}\r"
expect "]"
2016-11-14 21:49:38 +01:00
{%- endif %}
2016-11-29 02:24:13 +01:00
{%- endif %}
2016-11-13 03:19:54 +01:00
send "quit\r"
expect "{{ hostname }}]"
{%- endfor %}
{%- for name, conf in switch['ports'].items() %}
2016-12-09 02:52:38 +01:00
{%- if conf['mode'] == 'trunk' or conf['mode'] == 'bond' %}
{%- if conf['mode'] == 'bond' %}
send "link-aggregation group {{ conf['group'] }} mode static\r"
expect {
"This aggregation will be modified to static mode. Continue ?" {
send "Y\r"
}
"]" {}
}
send "link-aggregation group {{ conf['group'] }} description {{name}}\r"
expect "]"
{%- endif %}
2016-12-12 22:34:31 +01:00
{%- for port in conf['ports'] %}
2016-11-16 01:27:41 +01:00
send "interface {{ port }}\r"
2016-11-13 03:19:54 +01:00
expect "]"
send "undo stp edged-port\r"
expect "]"
2016-12-12 22:34:31 +01:00
{%- if conf['mode'] == 'bond' %}
send "lacp enable\r"
expect "]"
send "undo port link-aggregation group\r"
expect "]"
send "port link-aggregation group {{ conf['group'] }}\r"
2016-12-12 22:34:31 +01:00
{%- else %}
send "undo lacp enable\r"
2016-12-12 22:34:31 +01:00
{%- endif %}
2016-11-13 03:19:54 +01:00
expect "]"
send "jumboframe enable\r"
expect "]"
2016-12-12 22:34:31 +01:00
{%- if conf.get('vlans') %}
2016-11-13 03:19:54 +01:00
send "port link-type trunk\r"
expect "]"
2016-11-16 01:17:28 +01:00
# Set dummy default vlan
2016-11-14 23:44:13 +01:00
send "port trunk pvid vlan 4094\r"
expect "]"
# Deconfigure all but mgmt vlan
send "undo port trunk permit vlan 2 to 4094\r"
expect "]"
2016-12-12 22:34:31 +01:00
{%- for vlan_name in conf['vlans'] %}
2016-11-13 03:19:54 +01:00
send "port trunk permit vlan {{ pillar['vlans'][vlan_name] }}\r"
expect "]"
2016-12-12 22:34:31 +01:00
{%- endfor %}
{%- else %}
send "port link-type access\r"
expect "]"
send "port access vlan {{ pillar['vlans'][conf['access']] }}\r"
expect "]"
{%- endif %}
2016-11-13 03:19:54 +01:00
send "quit\r"
expect "{{ hostname }}]"
2016-12-12 22:34:31 +01:00
{%- endfor %}
2016-11-13 03:19:54 +01:00
2016-12-12 22:34:31 +01:00
{%- elif conf['mode'] == 'access' %}
{%- for port in conf['ports'] %}
2016-11-16 01:27:41 +01:00
send "interface {{ port }}\r"
2016-11-13 03:19:54 +01:00
expect "]"
send "undo port link-aggregation group\r"
expect "]"
2016-11-13 03:19:54 +01:00
send "port link-type access\r"
expect "]"
{%- if name == 'mgmt' %}
send "undo port access vlan\r"
expect "]"
{%- else %}
2016-11-13 03:19:54 +01:00
send "port access vlan {{ pillar['vlans'][name] }}\r"
2016-12-12 22:34:31 +01:00
expect "]"
{%- endif %}
2016-11-13 03:19:54 +01:00
send "quit\r"
expect "{{ hostname }}]"
2016-12-12 22:34:31 +01:00
{%- endfor %}
2016-11-13 03:19:54 +01:00
2016-12-12 22:34:31 +01:00
{%- endif %}
2016-11-13 03:19:54 +01:00
{%- endfor %}
send "save main\r"
expect "Y/N]"
send "YES\r"
expect "press the enter key):"
send "\r"
expect "]"
send "quit\r"
expect ">"
send "quit\r"