network/nix/pkgs/switches/linksys-srw2048.nix

113 lines
3.2 KiB
Nix
Raw Normal View History

2021-11-03 01:07:44 +01:00
# https://www.crc.id.au/real-console-on-linksys-srw2024-switch/
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;
''
#! ${expect}/bin/expect -f
spawn ${inetutils}/bin/telnet ${config.site.net.mgmt.hosts4.${hostName}}
expect "Password:"
send "admin\t${hostConfig.password}\r"
# ^z
send "\x1A"
expect ">"
send "lcli\r"
expect "User Name:"
send "admin\r"
expect "Password:"
send "${hostConfig.password}\r"
expect "# "
send "configure\r"
expect "(config)# "
send "hostname ${hostName}\r"
expect "(config)# "
send "port jumbo-frame\r"
expect "(config)# "
2021-11-03 01:07:44 +01:00
send "management vlan 4094\r"
expect "(config)# "
send "vlan database\r"
expect "(config-vlan)# "
${concatMapStrings (net: ''
send "vlan ${toString config.site.net.${net}.vlan}\r"
expect "(config-vlan)#"
2021-11-07 02:22:24 +01:00
'') (sortNetsByVlan (builtins.attrNames config.site.net))}
2021-11-03 01:07:44 +01:00
send "exit\r"
expect "(config)#"
${concatMapStrings (net: ''
send "interface vlan ${toString config.site.net.${net}.vlan}\r"
expect "(config-if)#"
send "name ${net}\r"
expect "(config-if)#"
send "exit\r"
expect "(config)#"
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;
isBond = isTrunk && builtins.length linkConfig.ports > 1;
2021-11-07 02:22:24 +01:00
vlans = concatStringsSep "," (map toString (sort linkConfig.vlans));
2021-11-03 01:07:44 +01:00
ports = concatStringsSep "," linkConfig.ports;
in
if isTrunk && isBond
then ''
send "interface range ethernet ${ports}\r"
expect "(config-if)#"
send "switchport trunk allowed vlan remove all\r"
expect "(config-if)#"
send "channel-group ${linkConfig.group} mode auto\r"
expect "(config-if)#"
send "interface port-channel ${linkConfig.group}\r"
expect "(config-if)#"
send "exit\r"
send "interface port-channel ${linkConfig.group}\r"
expect "(config-if)#"
send "switchport mode trunk\r"
expect "(config-if)#"
2021-11-07 02:21:48 +01:00
send "switchport trunk allowed vlan add ${vlans}\r"
2021-11-03 01:07:44 +01:00
expect "(config-if)#"
send "exit\r"
expect "(config)#"
''
else if isTrunk
then concatMapStrings (port: ''
send "interface ethernet ${port}\r"
expect "(config-if)#"
send "no channel-group\r"
expect "(config-if)#"
send "switchport mode trunk\r"
expect "(config-if)#"
2021-11-07 02:21:48 +01:00
send "switchport trunk allowed vlan add ${vlans}\r"
2021-11-03 01:07:44 +01:00
expect "(config-if)#"
send "exit\r"
expect "(config)#"
'') linkConfig.ports
else concatMapStrings (port: ''
send "interface ethernet ${port}\r"
expect "(config-if)#"
send "no channel-group\r"
expect "(config-if)#"
send "switchport mode access\r"
expect "(config-if)#"
send "switchport access vlan ${toString netConfig.vlan}\r"
expect "(config-if)#"
send "exit\r"
expect "(config)#"
2021-11-07 02:22:24 +01:00
'') (sort linkConfig.ports)
) (sortBy (link: hostConfig.links.${link}.ports)
(builtins.attrNames hostConfig.links)
)}
2021-11-03 01:07:44 +01:00
''