Automate Get-Configuration Cisco from Powershell Scripts
###---------------------------------------###
### Author : Yingkamol Prukrattanakul-----###
###---MCP, MCSA, MCSE, MCT, MCST, SEC+----###
###---Email<yingkamol_7@hotmail.com>------###
## Blogger: https://yingkamol.blogspot.com ##
###---------------------------------------###
###//////////////..........\\\\\\\\\\\\\\\###
###/////////////////.....\\\\\\\\\\\\\\\\\###
##powershell for management SW##
param(
[int] $port = 23,
[string] $command1 = "terminal length 0",
[string] $command2 = "en",
[string] $command4 = "show run",
[int] $commandDelay = 1000
)
[string] $output = ""
function GetOutput
{
## Create a buffer to receive the response
$buffer = new-object System.Byte[] 1024
$encoding = new-object System.Text.AsciiEncoding
$outputBuffer = ""
$foundMore = $false
## Read all the data available from the stream, writing it to the
## output buffer when done.
do
{
## Allow data to buffer for a bit
start-sleep -m 1000
## Read what data is available
$foundmore = $false
$stream.ReadTimeout = 1000
do
{
try
{
$read = $stream.Read($buffer, 0, 1024)
if($read -gt 0)
{
$foundmore = $true
$outputBuffer += ($encoding.GetString($buffer, 0, $read))
}
} catch { $foundMore = $false; $read = 0 }
} while($read -gt 0)
} while($foundmore)
$outputBuffer
}
##HQ##
$remoteHost = Get-Content D:\Ying\PowerShell\mgmt_sw\sw-ip.txt
$username = Get-Content D:\Ying\PowerShell\mgmt_sw\user.txt
$password = Get-Content D:\Ying\PowerShell\mgmt_sw\password.txt
$password1 = Get-Content D:\Ying\PowerShell\mgmt_sw\password-en.txt
##Pak1##
$remoteHost1 = Get-Content D:\Ying\PowerShell\mgmt_sw\sw-ip1.txt
##Pak2##
$remoteHost2 = Get-Content D:\Ying\PowerShell\mgmt_sw\sw-ip2.txt
##Pak3##
$remoteHost3 = Get-Content D:\Ying\PowerShell\mgmt_sw\sw-ip3.txt
##Pak4##
$remoteHost4 = Get-Content D:\Ying\PowerShell\mgmt_sw\sw-ip4.txt
##Pak5##
$remoteHost5 = Get-Content D:\Ying\PowerShell\mgmt_sw\sw-ip5.txt
##Pak6##
$remoteHost6 = Get-Content D:\Ying\PowerShell\mgmt_sw\sw-ip6.txt
##Pak7##
$remoteHost7 = Get-Content D:\Ying\PowerShell\mgmt_sw\sw-ip7.txt
##Pak8##
$remoteHost8 = Get-Content D:\Ying\PowerShell\mgmt_sw\sw-ip8.txt
##Pak9##
$remoteHost9 = Get-Content D:\Ying\PowerShell\mgmt_sw\sw-ip9.txt
##Pak10##
$remoteHost10 = Get-Content D:\Ying\PowerShell\mgmt_sw\sw-ip10.txt
##Pak11##
$remoteHost11 = Get-Content D:\Ying\PowerShell\mgmt_sw\sw-ip11.txt
##Pak12##
$remoteHost12 = Get-Content D:\Ying\PowerShell\mgmt_sw\sw-ip12.txt
function HQ
{
ForEach ($remoteHosts in $remoteHost)
{
try
{
write-host "Connecting to $remoteHosts on port $port"
trap { Write-Error "Could not connect to remote computer: $_"; exit }
$socket = new-object System.Net.Sockets.TcpClient($remoteHosts, $port)
write-host "Connected. Press ^D followed by [ENTER] to exit.`n"
$stream = $socket.GetStream()
$writer = new-object System.IO.StreamWriter $stream
## Receive the output that has buffered so far
$writer.WriteLine($username)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($password)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command1)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command2)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($password1)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command4)
$writer.Flush()
Start-Sleep -m $commandDelay
$SCRIPT:output += GetOutput
## Close the streams
$writer.Close()
$stream.Close()
$output |Out-File "D:\Ying\PowerShell\mgmt_sw\HQ-$remoteHosts.txt"
}
catch
{
$Failedlogin = "System FailedUser:$remoteHosts"
echo $Failedlogin >
"D:\Ying\PowerShell\mgmt_sw\HQFailedlogin.txt"
}
}
}
function PAK1
{
ForEach ($remoteHosts in $remoteHost1)
{
try
{
write-host "Connecting to $remoteHosts on port $port"
trap { Write-Error "Could not connect to remote computer: $_"; exit }
$socket = new-object System.Net.Sockets.TcpClient($remoteHosts, $port)
write-host "Connected. Press ^D followed by [ENTER] to exit.`n"
$stream = $socket.GetStream()
$writer = new-object System.IO.StreamWriter $stream
## Receive the output that has buffered so far
$writer.WriteLine($username)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($password)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command1)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command2)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($password1)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command4)
$writer.Flush()
Start-Sleep -m $commandDelay
$SCRIPT:output += GetOutput
## Close the streams
$writer.Close()
$stream.Close()
$output |Out-File "D:\Ying\PowerShell\mgmt_sw\PAK1-$remoteHosts.txt"
}
catch
{
$Failedlogin = "System FailedUser:$remoteHosts"
echo $Failedlogin >
"D:\Ying\PowerShell\mgmt_sw\PAK1Failedlogin.txt"
}
}
}
function PAK2
{
ForEach ($remoteHosts in $remoteHost2)
{
try
{
write-host "Connecting to $remoteHosts on port $port"
trap { Write-Error "Could not connect to remote computer: $_"; exit }
$socket = new-object System.Net.Sockets.TcpClient($remoteHosts, $port)
write-host "Connected. Press ^D followed by [ENTER] to exit.`n"
$stream = $socket.GetStream()
$writer = new-object System.IO.StreamWriter $stream
## Receive the output that has buffered so far
$writer.WriteLine($username)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($password)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command1)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command2)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($password1)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command4)
$writer.Flush()
Start-Sleep -m $commandDelay
$SCRIPT:output += GetOutput
## Close the streams
$writer.Close()
$stream.Close()
$output |Out-File "D:\Ying\PowerShell\mgmt_sw\PAK2-$remoteHosts.txt"
}
catch
{
$Failedlogin = "System FailedUser:$remoteHosts"
echo $Failedlogin >
"D:\Ying\PowerShell\mgmt_sw\PAK2Failedlogin.txt"
}
}
}
function PAK3
{
ForEach ($remoteHosts in $remoteHost3)
{
try
{
write-host "Connecting to $remoteHosts on port $port"
trap { Write-Error "Could not connect to remote computer: $_"; exit }
$socket = new-object System.Net.Sockets.TcpClient($remoteHosts, $port)
write-host "Connected. Press ^D followed by [ENTER] to exit.`n"
$stream = $socket.GetStream()
$writer = new-object System.IO.StreamWriter $stream
## Receive the output that has buffered so far
$writer.WriteLine($username)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($password)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command1)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command2)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($password1)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command4)
$writer.Flush()
Start-Sleep -m $commandDelay
$SCRIPT:output += GetOutput
## Close the streams
$writer.Close()
$stream.Close()
$output |Out-File "D:\Ying\PowerShell\mgmt_sw\PAK3-$remoteHosts.txt"
}
catch
{
$Failedlogin = "System FailedUser:$remoteHosts"
echo $Failedlogin >
"D:\Ying\PowerShell\mgmt_sw\PAK3Failedlogin.txt"
}
}
}
function PAK4
{
ForEach ($remoteHosts in $remoteHost4)
{
try
{
write-host "Connecting to $remoteHosts on port $port"
trap { Write-Error "Could not connect to remote computer: $_"; exit }
$socket = new-object System.Net.Sockets.TcpClient($remoteHosts, $port)
write-host "Connected. Press ^D followed by [ENTER] to exit.`n"
$stream = $socket.GetStream()
$writer = new-object System.IO.StreamWriter $stream
## Receive the output that has buffered so far
$writer.WriteLine($username)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($password)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command1)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command2)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($password1)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command4)
$writer.Flush()
Start-Sleep -m $commandDelay
$SCRIPT:output += GetOutput
## Close the streams
$writer.Close()
$stream.Close()
$output |Out-File "D:\Ying\PowerShell\mgmt_sw\PAK4-$remoteHosts.txt"
}
catch
{
$Failedlogin = "System FailedUser:$remoteHosts"
echo $Failedlogin >
"D:\Ying\PowerShell\mgmt_sw\PAK4Failedlogin.txt"
}
}
}
function PAK5
{
ForEach ($remoteHosts in $remoteHost5)
{
try
{
write-host "Connecting to $remoteHosts on port $port"
trap { Write-Error "Could not connect to remote computer: $_"; exit }
$socket = new-object System.Net.Sockets.TcpClient($remoteHosts, $port)
write-host "Connected. Press ^D followed by [ENTER] to exit.`n"
$stream = $socket.GetStream()
$writer = new-object System.IO.StreamWriter $stream
## Receive the output that has buffered so far
$writer.WriteLine($username)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($password)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command1)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command2)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($password1)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command4)
$writer.Flush()
Start-Sleep -m $commandDelay
$SCRIPT:output += GetOutput
## Close the streams
$writer.Close()
$stream.Close()
$output |Out-File "D:\Ying\PowerShell\mgmt_sw\PAK1-$remoteHosts.txt"
}
catch
{
$Failedlogin = "System FailedUser:$remoteHosts"
echo $Failedlogin >
"D:\Ying\PowerShell\mgmt_sw\PAK5Failedlogin.txt"
}
}
}
function PAK6
{
ForEach ($remoteHosts in $remoteHost6)
{
try
{
write-host "Connecting to $remoteHosts on port $port"
trap { Write-Error "Could not connect to remote computer: $_"; exit }
$socket = new-object System.Net.Sockets.TcpClient($remoteHosts, $port)
write-host "Connected. Press ^D followed by [ENTER] to exit.`n"
$stream = $socket.GetStream()
$writer = new-object System.IO.StreamWriter $stream
## Receive the output that has buffered so far
$writer.WriteLine($username)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($password)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command1)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command2)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($password1)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command4)
$writer.Flush()
Start-Sleep -m $commandDelay
$SCRIPT:output += GetOutput
## Close the streams
$writer.Close()
$stream.Close()
$output |Out-File "D:\Ying\PowerShell\mgmt_sw\PAK6-$remoteHosts.txt"
}
catch
{
$Failedlogin = "System FailedUser:$remoteHosts"
echo $Failedlogin >
"D:\Ying\PowerShell\mgmt_sw\PAK6Failedlogin.txt"
}
}
}
function PAK7
{
ForEach ($remoteHosts in $remoteHost7)
{
try
{
write-host "Connecting to $remoteHosts on port $port"
trap { Write-Error "Could not connect to remote computer: $_"; exit }
$socket = new-object System.Net.Sockets.TcpClient($remoteHosts, $port)
write-host "Connected. Press ^D followed by [ENTER] to exit.`n"
$stream = $socket.GetStream()
$writer = new-object System.IO.StreamWriter $stream
## Receive the output that has buffered so far
$writer.WriteLine($username)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($password)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command1)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command2)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($password1)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command4)
$writer.Flush()
Start-Sleep -m $commandDelay
$SCRIPT:output += GetOutput
## Close the streams
$writer.Close()
$stream.Close()
$output |Out-File "D:\Ying\PowerShell\mgmt_sw\PAK7-$remoteHosts.txt"
}
catch
{
$Failedlogin = "System FailedUser:$remoteHosts"
echo $Failedlogin >
"D:\Ying\PowerShell\mgmt_sw\PAK7Failedlogin.txt"
}
}
}
function PAK8
{
ForEach ($remoteHosts in $remoteHost8)
{
try
{
write-host "Connecting to $remoteHosts on port $port"
trap { Write-Error "Could not connect to remote computer: $_"; exit }
$socket = new-object System.Net.Sockets.TcpClient($remoteHosts, $port)
write-host "Connected. Press ^D followed by [ENTER] to exit.`n"
$stream = $socket.GetStream()
$writer = new-object System.IO.StreamWriter $stream
## Receive the output that has buffered so far
$writer.WriteLine($username)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($password)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command1)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command2)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($password1)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command4)
$writer.Flush()
Start-Sleep -m $commandDelay
$SCRIPT:output += GetOutput
## Close the streams
$writer.Close()
$stream.Close()
$output |Out-File "D:\Ying\PowerShell\mgmt_sw\PAK8-$remoteHosts.txt"
}
catch
{
$Failedlogin = "System FailedUser:$remoteHosts"
echo $Failedlogin >
"D:\Ying\PowerShell\mgmt_sw\PAK8Failedlogin.txt"
}
}
}
function PAK9
{
ForEach ($remoteHosts in $remoteHost9)
{
try
{
write-host "Connecting to $remoteHosts on port $port"
trap { Write-Error "Could not connect to remote computer: $_"; exit }
$socket = new-object System.Net.Sockets.TcpClient($remoteHosts, $port)
write-host "Connected. Press ^D followed by [ENTER] to exit.`n"
$stream = $socket.GetStream()
$writer = new-object System.IO.StreamWriter $stream
## Receive the output that has buffered so far
$writer.WriteLine($username)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($password)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command1)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command2)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($password1)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command4)
$writer.Flush()
Start-Sleep -m $commandDelay
$SCRIPT:output += GetOutput
## Close the streams
$writer.Close()
$stream.Close()
$output |Out-File "D:\Ying\PowerShell\mgmt_sw\PAK9-$remoteHosts.txt"
}
catch
{
$Failedlogin = "System FailedUser:$remoteHosts"
echo $Failedlogin >
"D:\Ying\PowerShell\mgmt_sw\PAK9Failedlogin.txt"
}
}
}
function PAK10
{
ForEach ($remoteHosts in $remoteHost10)
{
try
{
write-host "Connecting to $remoteHosts on port $port"
trap { Write-Error "Could not connect to remote computer: $_"; exit }
$socket = new-object System.Net.Sockets.TcpClient($remoteHosts, $port)
write-host "Connected. Press ^D followed by [ENTER] to exit.`n"
$stream = $socket.GetStream()
$writer = new-object System.IO.StreamWriter $stream
## Receive the output that has buffered so far
$writer.WriteLine($username)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($password)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command1)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command2)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($password1)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command4)
$writer.Flush()
Start-Sleep -m $commandDelay
$SCRIPT:output += GetOutput
## Close the streams
$writer.Close()
$stream.Close()
$output |Out-File "D:\Ying\PowerShell\mgmt_sw\PAK10-$remoteHosts.txt"
}
catch
{
$Failedlogin = "System FailedUser:$remoteHosts"
echo $Failedlogin >
"D:\Ying\PowerShell\mgmt_sw\PAK10Failedlogin.txt"
}
}
}
function PAK11
{
ForEach ($remoteHosts in $remoteHost11)
{
try
{
write-host "Connecting to $remoteHosts on port $port"
trap { Write-Error "Could not connect to remote computer: $_"; exit }
$socket = new-object System.Net.Sockets.TcpClient($remoteHosts, $port)
write-host "Connected. Press ^D followed by [ENTER] to exit.`n"
$stream = $socket.GetStream()
$writer = new-object System.IO.StreamWriter $stream
## Receive the output that has buffered so far
$writer.WriteLine($username)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($password)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command1)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command2)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($password1)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command4)
$writer.Flush()
Start-Sleep -m $commandDelay
$SCRIPT:output += GetOutput
## Close the streams
$writer.Close()
$stream.Close()
$output |Out-File "D:\Ying\PowerShell\mgmt_sw\PAK11-$remoteHosts.txt"
}
catch
{
$Failedlogin = "System FailedUser:$remoteHosts"
echo $Failedlogin >
"D:\Ying\PowerShell\mgmt_sw\PAK11Failedlogin.txt"
}
}
}
function PAK12
{
ForEach ($remoteHosts in $remoteHost12)
{
try
{
write-host "Connecting to $remoteHosts on port $port"
trap { Write-Error "Could not connect to remote computer: $_"; exit }
$socket = new-object System.Net.Sockets.TcpClient($remoteHosts, $port)
write-host "Connected. Press ^D followed by [ENTER] to exit.`n"
$stream = $socket.GetStream()
$writer = new-object System.IO.StreamWriter $stream
## Receive the output that has buffered so far
$writer.WriteLine($username)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($password)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command1)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command2)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($password1)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command4)
$writer.Flush()
Start-Sleep -m $commandDelay
$SCRIPT:output += GetOutput
## Close the streams
$writer.Close()
$stream.Close()
$output |Out-File "D:\Ying\PowerShell\mgmt_sw\PAK12-$remoteHosts.txt"
}
catch
{
$Failedlogin = "System FailedUser:$remoteHosts"
echo $Failedlogin >
"D:\Ying\PowerShell\mgmt_sw\PAK12Failedlogin.txt"
}
}
}