Advertisement
Guest User

PowerShell: Telnet To Remote Hosts And Run Commands

a guest
Jan 3rd, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ## PowerShell: Script To Telnet To Remote Hosts And Run Commands Against Them With Output To A Variable ##
  2.  
  3. [string] $voiceHost = "10.0.0.2"
  4. [int]    $voicePort = 23
  5. [string] $routerHost = "10.0.0.1"
  6. [int]    $routerPort = 23
  7. [string] $routerTelnetEnablePath = 'c:\telnetEnable.exe'
  8. [string] $routerMac = "EE99FFADABBB"
  9. [string] $voiceUsername = "admin"
  10. [string] $voicePassword = "admin"
  11. [int]    $commandDelay = 500
  12. [string] $output = ""
  13.  
  14. ## Read output from a remote host
  15. function GetOutput {
  16.     ## Create a buffer to receive the response
  17.     $buffer = new-object System.Byte[] 1024
  18.     $encoding = new-object System.Text.AsciiEncoding
  19.     $outputBuffer = ""
  20.     $foundMore = $false
  21.     ## Read all the data available from the stream, writing it to the
  22.     ## output buffer when done.
  23.     do {
  24.         ## Allow data to buffer for a bit
  25.         start-sleep -m 750
  26.         ## Read what data is available
  27.         $foundmore = $false
  28.         $stream.ReadTimeout = 1000
  29.         do {
  30.             try {
  31.                 $read = $stream.Read($buffer, 0, 1024)
  32.                 if ($read -gt 0) {
  33.                     $foundmore = $true
  34.                     $outputBuffer += ($encoding.GetString($buffer, 0, $read))
  35.                 }
  36.             } catch {
  37.                 $foundMore = $false; $read = 0
  38.             }
  39.         } while ($read -gt 0)
  40.     } while ($foundmore)
  41.     $outputBuffer
  42. }
  43.  
  44. function Main {
  45.     ## Open the socket, and connect to the computer on the specified port
  46.     write-host "Соединяемся с голосовым шлюзом $voiceHost на порту $voicePort."
  47.     trap { Write-Error "Не могу соединиться: $_"; exit }
  48.     $socket = new-object System.Net.Sockets.TcpClient($voiceHost, $voicePort)
  49.     write-host "Соединение установлено."
  50.     $stream = $socket.GetStream()
  51.     $writer = new-object System.IO.StreamWriter $stream
  52.    
  53.     $writer.WriteLine($voiceUsername)
  54.     $writer.Flush()
  55.     Start-Sleep -m $commandDelay
  56.     $writer.WriteLine($voicePassword)
  57.     $writer.Flush()
  58.     Start-Sleep -m $commandDelay
  59.     $writer.WriteLine("cat /proc/kmsg &")
  60.     $writer.Flush()
  61.     Start-Sleep -m $commandDelay
  62.     $writer.WriteLine("echo slic_rd 0 44> /proc/ks_cpld")
  63.     $writer.Flush()
  64.     Start-Sleep -m $commandDelay
  65.     $SCRIPT:output = GetOutput
  66.     if ($output.Contains("slic chip 0 reg 0x44 = 0x99")) {
  67.         write-host "Трубка лежит, перезагружаем голосовой шлюз.`n"
  68.         $writer.WriteLine("reboot")
  69.         $writer.Flush()
  70.         Start-Sleep -m $commandDelay
  71.         $writer.Close()
  72.         $stream.Close()
  73.         $socket.Close()
  74.        
  75.         & $routerTelnetEnablePath $routerHost $routerMac 'Gearguy' 'Geardog'
  76.        
  77.         ## Open the socket, and connect to the computer on the specified port
  78.         write-host "Соединяемся с маршрутизатором $routerHost на порту $routerPort."
  79.         trap { Write-Error "Не могу соединиться: $_"; exit }
  80.         $socket = new-object System.Net.Sockets.TcpClient($routerHost, $routerPort)
  81.         write-host "Соединение установлено.`n"
  82.         $stream = $socket.GetStream()
  83.         $writer = new-object System.IO.StreamWriter $stream
  84.        
  85.         write-host "Перезагружаем маршрутизатор. Произойдет обрыв соединений с интернетом."
  86.         $writer.WriteLine("reboot")
  87.         $writer.Flush()
  88.         Start-Sleep -m $commandDelay
  89.     }
  90.     ## Close the streams
  91.     $writer.Close()
  92.     $stream.Close()
  93.     $socket.Close()
  94.     write-host "`nКонец."
  95. }
  96. . Main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement