Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## PowerShell: Script To Telnet To Remote Hosts And Run Commands Against Them With Output To A Variable ##
- [string] $voiceHost = "10.0.0.2"
- [int] $voicePort = 23
- [string] $routerHost = "10.0.0.1"
- [int] $routerPort = 23
- [string] $routerTelnetEnablePath = 'c:\telnetEnable.exe'
- [string] $routerMac = "EE99FFADABBB"
- [string] $voiceUsername = "admin"
- [string] $voicePassword = "admin"
- [int] $commandDelay = 500
- [string] $output = ""
- ## Read output from a remote host
- 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 750
- ## 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
- }
- function Main {
- ## Open the socket, and connect to the computer on the specified port
- write-host "Соединяемся с голосовым шлюзом $voiceHost на порту $voicePort."
- trap { Write-Error "Не могу соединиться: $_"; exit }
- $socket = new-object System.Net.Sockets.TcpClient($voiceHost, $voicePort)
- write-host "Соединение установлено."
- $stream = $socket.GetStream()
- $writer = new-object System.IO.StreamWriter $stream
- $writer.WriteLine($voiceUsername)
- $writer.Flush()
- Start-Sleep -m $commandDelay
- $writer.WriteLine($voicePassword)
- $writer.Flush()
- Start-Sleep -m $commandDelay
- $writer.WriteLine("cat /proc/kmsg &")
- $writer.Flush()
- Start-Sleep -m $commandDelay
- $writer.WriteLine("echo slic_rd 0 44> /proc/ks_cpld")
- $writer.Flush()
- Start-Sleep -m $commandDelay
- $SCRIPT:output = GetOutput
- if ($output.Contains("slic chip 0 reg 0x44 = 0x99")) {
- write-host "Трубка лежит, перезагружаем голосовой шлюз.`n"
- $writer.WriteLine("reboot")
- $writer.Flush()
- Start-Sleep -m $commandDelay
- $writer.Close()
- $stream.Close()
- $socket.Close()
- & $routerTelnetEnablePath $routerHost $routerMac 'Gearguy' 'Geardog'
- ## Open the socket, and connect to the computer on the specified port
- write-host "Соединяемся с маршрутизатором $routerHost на порту $routerPort."
- trap { Write-Error "Не могу соединиться: $_"; exit }
- $socket = new-object System.Net.Sockets.TcpClient($routerHost, $routerPort)
- write-host "Соединение установлено.`n"
- $stream = $socket.GetStream()
- $writer = new-object System.IO.StreamWriter $stream
- write-host "Перезагружаем маршрутизатор. Произойдет обрыв соединений с интернетом."
- $writer.WriteLine("reboot")
- $writer.Flush()
- Start-Sleep -m $commandDelay
- }
- ## Close the streams
- $writer.Close()
- $stream.Close()
- $socket.Close()
- write-host "`nКонец."
- }
- . Main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement