2021-11-03 01:07:44 +01:00
|
|
|
# http://static.tp-link.com/res/down/doc/TL-SG3210(UN)_V2.0_CLI_.pdf
|
2021-11-07 02:22:24 +01:00
|
|
|
{ self, pkgs, hostName, config, hostConfig
|
|
|
|
, sort, sortBy, sortNetsByVlan
|
|
|
|
, ... }:
|
2021-11-03 01:07:44 +01:00
|
|
|
with pkgs;
|
|
|
|
with lib;
|
|
|
|
''
|
|
|
|
#! ${pkgs.expect}/bin/expect -f
|
|
|
|
|
|
|
|
spawn ${pkgs.inetutils}/bin/telnet ${config.site.net.mgmt.hosts4.${hostName}}
|
|
|
|
expect "Password:"
|
|
|
|
send "${hostConfig.password}\r"
|
|
|
|
expect ">"
|
|
|
|
send "\r"
|
|
|
|
expect ">"
|
|
|
|
send "enable\r"
|
|
|
|
expect "Password:"
|
|
|
|
send "${hostConfig.password}\r"
|
|
|
|
expect "#"
|
|
|
|
send "configure\r"
|
|
|
|
expect "(config)#"
|
|
|
|
|
|
|
|
send "enable secret 0 ${hostConfig.password}\r"
|
|
|
|
expect "(config)#"
|
|
|
|
#send "enable password 0 ${hostConfig.password}\r"
|
|
|
|
#expect "(config)#"
|
|
|
|
send "service password-encryption\r"
|
|
|
|
expect "(config)#"
|
|
|
|
send "user name admin privilege admin secret 0 ${hostConfig.password}\r"
|
|
|
|
expect "(config)#"
|
|
|
|
|
|
|
|
send "hostname \"${hostName}\"\r"
|
|
|
|
expect "(config)#"
|
|
|
|
send "location \"${hostConfig.location}\"\r"
|
|
|
|
expect "(config)#"
|
|
|
|
|
|
|
|
send "logging host index 1 ${config.site.net.mgmt.hosts4.logging} 6\r"
|
|
|
|
expect "(config)#"
|
|
|
|
|
|
|
|
send "ip management-vlan ${toString config.site.net.mgmt.vlan}\r"
|
|
|
|
expect "(config)#"
|
|
|
|
send "ip ssh server\r"
|
|
|
|
expect "(config)#"
|
|
|
|
send "telnet enable\r"
|
|
|
|
expect "(config)#"
|
|
|
|
send "line vty 0 15\r"
|
|
|
|
expect "(config-line)#"
|
|
|
|
send "password 0 ${hostConfig.password}\r"
|
|
|
|
expect "(config-line)#"
|
|
|
|
send "exit\r"
|
|
|
|
expect "(config)#"
|
|
|
|
|
|
|
|
${concatMapStrings (net:
|
|
|
|
let
|
|
|
|
netConfig = config.site.net.${net};
|
|
|
|
vlan = toString netConfig.vlan;
|
|
|
|
inherit (config.site.net.${net}) hosts4;
|
|
|
|
hostAddr4 = hosts4.${hostName};
|
|
|
|
prefixLength = elemAt (
|
|
|
|
builtins.split "/" netConfig.subnet4
|
|
|
|
) 2;
|
|
|
|
netmask = self.lib.netmasks.${prefixLength};
|
2022-01-19 17:18:00 +01:00
|
|
|
in
|
|
|
|
if netConfig.vlan != null
|
|
|
|
then ''
|
|
|
|
${optionalString (net != "mgmt") ''
|
|
|
|
send "vlan ${vlan}\r"
|
|
|
|
expect "(config-vlan)#"
|
|
|
|
send "name \"${net}\"\r"
|
|
|
|
expect "(config-vlan)#"
|
|
|
|
send "exit\r"
|
|
|
|
expect "(config)#"
|
|
|
|
''}
|
2021-11-03 01:07:44 +01:00
|
|
|
|
2022-01-19 17:18:00 +01:00
|
|
|
send "interface vlan ${vlan}\r"
|
2021-11-03 01:07:44 +01:00
|
|
|
expect "(config-if)#"
|
2022-01-19 17:18:00 +01:00
|
|
|
${optionalString (hosts4 ? ${hostName}) ''
|
|
|
|
# Actually only used for mgmt_vlan, switches are not routers
|
|
|
|
send "ip address ${hostAddr4} ${netmask}\r"
|
|
|
|
expect "(config-if)#"
|
|
|
|
''}
|
|
|
|
send "exit\r"
|
|
|
|
expect "(config)#"
|
|
|
|
''
|
|
|
|
else ""
|
2021-11-07 02:22:24 +01:00
|
|
|
) (sortNetsByVlan (builtins.attrNames config.site.net))}
|
2021-11-03 01:07:44 +01:00
|
|
|
|
|
|
|
${concatMapStrings (name:
|
|
|
|
let
|
|
|
|
linkConfig = hostConfig.links.${name};
|
|
|
|
isAccess = config.site.net ? ${name};
|
|
|
|
netConfig = config.site.net.${name};
|
|
|
|
isTrunk = !isAccess;
|
2021-11-07 02:21:48 +01:00
|
|
|
port0 = builtins.head linkConfig.ports;
|
|
|
|
isBond =
|
|
|
|
builtins.length linkConfig.ports > 1
|
|
|
|
||
|
|
|
|
hasInfix "-" port0;
|
2021-11-07 02:22:24 +01:00
|
|
|
vlans = concatStringsSep "," (map toString (sort linkConfig.vlans));
|
2021-11-07 02:21:48 +01:00
|
|
|
ports = concatMapStringsSep "," (port:
|
|
|
|
"1/0/${port}"
|
|
|
|
) linkConfig.ports;
|
2021-11-03 01:07:44 +01:00
|
|
|
in
|
|
|
|
if isTrunk
|
|
|
|
then ''
|
2021-11-07 02:21:48 +01:00
|
|
|
send "interface range gigabitEthernet ${ports}\r"
|
2021-11-03 01:07:44 +01:00
|
|
|
expect "(config-if-range)#"
|
|
|
|
send "switchport mode trunk\r"
|
|
|
|
expect "(config-if-range)#"
|
2021-11-07 02:21:48 +01:00
|
|
|
${if isBond
|
2021-11-03 01:07:44 +01:00
|
|
|
then ''
|
|
|
|
send "channel-group ${linkConfig.group} mode active\r"
|
|
|
|
expect "(config-if-range)#"
|
|
|
|
#send "port-channel load-balance src-dst-ip\r"
|
|
|
|
#expect "(config-if-range)#"
|
|
|
|
'' else ''
|
|
|
|
send "no channel-group\r"
|
|
|
|
expect "(config-if-range)#"
|
|
|
|
''}
|
2021-11-07 02:21:48 +01:00
|
|
|
send "switchport trunk allowed vlan ${vlans}\r"
|
2021-11-03 01:07:44 +01:00
|
|
|
expect "(config-if-range)#"
|
|
|
|
send "exit\r"
|
|
|
|
expect "(config)#"
|
|
|
|
'' else ''
|
|
|
|
send "interface range gigabitEthernet 1/0/${ports}\r"
|
|
|
|
expect "(config-if-range)#"
|
|
|
|
send "switchport mode access\r"
|
|
|
|
expect "(config-if-range)#"
|
|
|
|
send "switchport access vlan ${toString netConfig.vlan}\r"
|
|
|
|
expect "(config-if-range)#"
|
|
|
|
send "exit\r"
|
|
|
|
expect "(config)#"
|
|
|
|
''
|
2021-11-07 02:22:24 +01:00
|
|
|
) (sortBy (link: hostConfig.links.${link}.ports)
|
|
|
|
(builtins.attrNames hostConfig.links)
|
|
|
|
)}
|
2021-11-03 01:07:44 +01:00
|
|
|
|
|
|
|
send "exit\r"
|
|
|
|
expect "#"
|
|
|
|
send "copy running-config startup-config\r"
|
|
|
|
expect "#"
|
|
|
|
send "exit\r"
|
|
|
|
expect ">"
|
|
|
|
send "exit\r"
|
|
|
|
''
|