network/salt/switches/3com-4200G.expect
webzwo0i c2bfd17143 Jeder access Port ist default in vlan 1, welches wir auch als mgmt
belassen haben. Falls ein Port als mgmt konfiguriert werden soll, muss
das eventuell vorhandene vlan gelöscht werden. Explizit vlan 1 zu
setzen geht nicht.
2017-03-02 02:32:36 +01:00

121 lines
2.9 KiB
Plaintext

{# http://h20628.www2.hp.com/km-ext/kmcsdirect/emr_na-c02586144-1.pdf #}
{%- import_yaml "netmasks.yaml" as netmasks -%}
#!/usr/bin/expect -f
spawn telnet {{ pillar['hosts-inet']['mgmt'][hostname] }}
expect "Password:"
send "{{ switch['password'] }}\r"
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]"
send "set authentication password simple {{ switch['password'] }}\r"
expect "ui-vty0-4]"
send "quit\r"
expect "{{ hostname }}]"
send "local-user admin\r"
expect -- "-luser-admin]"
send "password simple {{ switch['password'] }}\r"
expect -- "-luser-admin]"
send "quit\r"
expect "{{ hostname }}]"
{%- for name, vlan in pillar['vlans'].items() %}
send "vlan {{ vlan }}\r"
expect -- "-vlan{{ vlan }}]"
send "name {{ name }}\r"
expect -- "-vlan{{ vlan }}]"
{%- if name == 'mgmt' %}
{# Actually only used for mgmt_vlan, switches are not routers #}
send "interface Vlan-interface {{ vlan }}\r"
expect "]"
{%- set net_hosts = pillar['hosts-inet'].get(name) %}
{%- set ipaddr = net_hosts and net_hosts.get(hostname) %}
{%- if ipaddr %}
send "ip address {{ ipaddr }} {{ netmasks[pillar['subnets-inet'][name].split('/')[1]] }}\r"
expect "]"
{%- endif %}
{%- endif %}
send "quit\r"
expect "{{ hostname }}]"
{%- endfor %}
{%- for name, conf in switch['ports'].items() %}
{%- if conf['mode'] == 'trunk' or conf['mode'] == 'bond' %}
{%- for port in conf['ports'] %}
send "interface {{ port }}\r"
expect "]"
send "undo stp edged-port\r"
expect "]"
{%- if conf['mode'] == 'bond' %}
send "lacp enable\r"
{%- else %}
send "undo lacp enable\r"
{%- endif %}
expect "]"
{%- if conf.get('vlans') %}
send "port link-type trunk\r"
expect "]"
# Set dummy default vlan
send "port trunk pvid vlan 4094\r"
expect "]"
# Deconfigure all but mgmt vlan
send "undo port trunk permit vlan 2 to 4094\r"
expect "]"
{%- for vlan_name in conf['vlans'] %}
send "port trunk permit vlan {{ pillar['vlans'][vlan_name] }}\r"
expect "]"
{%- endfor %}
{%- else %}
send "port link-type access\r"
expect "]"
send "port access vlan {{ pillar['vlans'][conf['access']] }}\r"
expect "]"
{%- endif %}
send "quit\r"
expect "{{ hostname }}]"
{%- endfor %}
{%- elif conf['mode'] == 'access' %}
{%- for port in conf['ports'] %}
send "interface {{ port }}\r"
expect "]"
send "undo port link-aggregation group\r"
expect "]"
send "port link-type access\r"
expect "]"
{%- if name == 'mgmt' %}
send "undo port access vlan\r"
expect "]"
{%- else %}
send "port access vlan {{ pillar['vlans'][name] }}\r"
expect "]"
{%- endif %}
send "quit\r"
expect "{{ hostname }}]"
{%- endfor %}
{%- endif %}
{%- 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"