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

116 lines
2.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 #}
{%- 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 }}]"
{%- 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-29 02:24:13 +01:00
send "name {{ name }}\r"
2016-11-13 03:19:54 +01:00
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 %}
{%- set group = 0 %}
{%- for name, conf in switch['ports'].items() %}
{%- if conf['mode'] == 'trunk' or conf['mode'] == 'bond' %}
2016-11-13 03:19:54 +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 "]"
{%- if conf['mode'] == 'bond' %}
2016-11-13 03:19:54 +01:00
send "lacp enable\r"
{%- else %}
send "undo lacp enable\r"
{%- endif %}
2016-11-13 03:19:54 +01:00
expect "]"
2016-11-29 02:24:13 +01:00
{%- if conf['mode'] == 'bond' %}
{%- set group = group + 1 %}
2016-11-13 03:19:54 +01:00
send "port link-aggregation group {{ group }}\r"
2016-11-29 02:24:13 +01:00
{%- else %}
send "undo port link-aggregation group\r"
{%- endif %}
2016-11-13 03:19:54 +01:00
expect "]"
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 "]"
{%- 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 "]"
{%- endfor %}
send "quit\r"
expect "{{ hostname }}]"
{%- endfor %}
{%- 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 "]"
send "port access vlan {{ pillar['vlans'][name] }}\r"
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"