The follow script is something I wrote to automate the VLAN configuration of our Dell 3448 Switches via telnet. We are currently implementing VLANs for the very first time and I wanted a way to easily deploy the VLAN changes as quickly as possible.
param (
[string]$SwitchIP = $env:computername,
[string]$SwitchUser = "",
[string]$SwitchPassword = "",
[string]$InputFile = "",
[int]$Port = 23
)
$Data = Import-Csv -Path $InputFile -Header 'Port', 'Tagged', 'UntaggedOut', 'UntaggedIn'
# All this does is connect and enter the username and password
$TelnetScript = @"
set oShell = CreateObject("WScript.Shell")`r`n
oShell.run("telnet")`r`n
WScript.Sleep 1000`r`n
oShell.SendKeys("open $SwitchIP $Port")`r`n
WScript.Sleep 1000`r`n
oShell.SendKeys("{Enter}")`r`n
WScript.Sleep 1000`r`n
oShell.SendKeys("{Enter}")`r`n
WScript.Sleep 1000`r`n
oShell.SendKeys("$SwitchUser")
WScript.Sleep 1000`r`n
oShell.SendKeys("{Enter}")`r`n
WScript.Sleep 1000`r`n
oShell.SendKeys("$SwitchPassword")
WScript.Sleep 1000`r`n
oShell.SendKeys("{Enter}")`r`n
WScript.Sleep 1000`r`n
oShell.SendKeys("configure")`r`n
WScript.Sleep 1000`r`n
oShell.SendKeys("{Enter}")`r`n
WScript.Sleep 1000`r`n
"@
$Data | Foreach-Object {
# This gets the correct port and sets it to general mode
$Object = $_
$TelnetScript += 'oShell.SendKeys("interface ethernet ' + $Object.Port + '")'+"`r`n";
$TelnetScript += "WScript.Sleep 1000`r`n";
$TelnetScript += 'oShell.SendKeys("{Enter}")'+"`r`n";
$TelnetScript += 'WScript.Sleep 1000'+"`r`n";
$TelnetScript += 'oShell.SendKeys("switchport mode general")'+"`r`n";
$TelnetScript += "WScript.Sleep 1000`r`n";
$TelnetScript += 'oShell.SendKeys("{Enter}")'+"`r`n";
$TelnetScript += "WScript.Sleep 1000`r`n";
# Sets the allowed tagged VLANs
$Object.Tagged.Split(',') | Foreach-Object {
$TelnetScript += 'oShell.SendKeys("switchport general allowed vlan add ' + $_ + ' tagged")'+"`r`n";
$TelnetScript += "WScript.Sleep 1000`r`n";
$TelnetScript += 'oShell.SendKeys("{Enter}")'+"`r`n";
$TelnetScript += "WScript.Sleep 1000`r`n";
}
# Sets the outgoing untagged vlan
$TelnetScript += 'oShell.SendKeys("switchport general allowed vlan add ' + $Object.UntaggedOut + ' untagged")'+"`r`n";
$TelnetScript += "WScript.Sleep 1000`r`n";
$TelnetScript += 'oShell.SendKeys("{Enter}")'+"`r`n";
$TelnetScript += "WScript.Sleep 1000`r`n";
# Sets the incoming untagged vlan
$TelnetScript += 'oShell.SendKeys("switchport general pvid ' + $Object.UntaggedIn + '")'+"`r`n";
$TelnetScript += "WScript.Sleep 1000`r`n";
$TelnetScript += 'oShell.SendKeys("{Enter}")'+"`r`n";
$TelnetScript += "WScript.Sleep 1000`r`n";
# Exit interface command so we can do a diffrent one
$TelnetScript += 'oShell.SendKeys("exit")'+"`r`n";
$TelnetScript += "WScript.Sleep 1000`r`n";
$TelnetScript += 'oShell.SendKeys("{Enter}")'+"`r`n";
$TelnetScript += "WScript.Sleep 1000`r`n";
}
$TelnetScript | Out-File -FilePath .\RunTelnet.vbs -Encoding ASCII;
& .\RunTelnet.vbs
Your CSV File must be in the following format (note that there is NO HEADER!)
<port> | <tagged> | <untaggedOut> | <untaggedIn> |
1/e1 | 200,400,500 | 400 | 400 |
1/e5 | 400,500 | 500 | 500 |
You must also have the following parameters defined when running the script:
-SwitchIP
IP of the Switch
-SwitchUser
Username for the admin
-SwitchPassword
Password for admin user
-InputFile
The CSV File you created
-Port
(option) port of the telnet interface