PowerShell: Telnetühendaja
From ICO wiki
<#
.SYNOPSIS
Skript, mis loob telnetyhenduse etteantud aadressiga. Seejärel küsitakse kasutajanimi ja parool ning asutakse k2skusid t2itma(hetkel on cisco ruuteri default conf)
.DESCRIPTION
Porti saab vabalt muuta. K2skudevaheline aeg on pandud kahe sekundi peale, ning k6iki k2ske ning nende vastuseid v2ljastatakse ekraanile
.Example
Skripti k2ivtamiseks tuleks lisada parameeter aadressiga
C:\PS>.\kodut88.ps1 192.168.1.1
.NOTES
Autor: Kristjan Rõõm
Versioon: 0.3
Muutmise kuup2ev 25.04.2012
.LINK
http://www.itcollege.ee
#>
#etteantud parameeter loetakse sisse stringina
param([String] $Hosta)
#m22rame vastava pordi
$port = 23
#kysime kasutajanime
$Usern = read-host "Sisestage kasutajanimi"
#kysime parooli
$pw = read-host "Sisestage parool" -AsSecureString
#muudame parooli tavatekstiks
$BasicString = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pw)
$Passw = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BasicString)
#funktsioon, mis loeb k2skudele j2rgnenud vastuseid
function readResponse {
while($stream.DataAvailable)
{
$read = $stream.Read($buffer, 0, 1024)
write-host -n -foregroundcolor cyan ($encoding.GetString($buffer, 0, $read))
""
}
}
#yhenduse parameetritega muutuja
$socket = new-object System.Net.Sockets.TcpClient($Hosta, $port)
if($socket -eq $null) { return; }
#loome yhenduse
$stream = $socket.GetStream()
$writer = new-object System.IO.StreamWriter($stream)
$buffer = new-object System.Byte[] 1024
$encoding = new-object System.Text.AsciiEncoding
#anname j2rjest vajalikud k2sud
#loetakse sisse k2sk
$command = $Usern
#antakse k2sk writerile
$writer.WriteLine($command)
#tyhjendatakse writer
$writer.Flush()
#oodatakse 2 sekundit
start-sleep -m 2000
#loetakse vastus
readResponse($stream)
$command = $Passw
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 2000
readResponse($stream)
$command = "enable"
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 2000
readResponse($stream)
$command = "configure terminal"
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 2000
readResponse($stream)
$command = "hostname Router1"
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 2000
readResponse($stream)
$command = "enable secret mingiparool"
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 2000
readResponse($stream)
$command = "no ip domain-lookup"
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 2000
readResponse($stream)
$command = 'banner motd "welcome R1"'
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 2000
readResponse($stream)
$command = "line console 0"
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 2000
readResponse($stream)
write-host -foregroundcolor DarkGreen $command
""
$command = "password mingiparool"
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 2000
readResponse($stream)
$command = "logging synchronous"
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 2000
readResponse($stream)
$command = "login"
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 2000
readResponse($stream)
$command = "line vty 0 4"
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 2000
readResponse($stream)
$command = "password mingiparool"
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 2000
readResponse($stream)
$command = "logging synchronous"
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 2000
readResponse($stream)
$command = "login"
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 2000
readResponse($stream)
$writer.Flush()
readResponse($stream)
# sulgeme striimi
$writer.Close()