Guest User

Untitled

a guest
Nov 8th, 2017
1,160
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 console
  2. ## Overview: Useful for Telnet connections to Cisco Switches and other devices.
  3. ##           Customized to return data to PRTG via an EXE/Advanced sensor for a Citrix port 1494 ICA response code
  4. ## When creating a new sensor in PRTG for this, specify the remote server via IP address or hostname in the "Parameters" field
  5. ## If you are using this to monitor multiple computers, it's highly recommended to add a Mutex so they do not all simultaneously scan
  6.  
  7. param(
  8.     [string] $remoteHost = "",
  9.     [int] $port = 1494,
  10.     [string] $username = "",
  11.     [string] $password = "",
  12.     [string] $termlength = "term len 0", #Useful for older consoles that have line display limitations
  13.     [string] $enable = "en", #Useful for appliances like Cisco switches that have an 'enable' command mode
  14.     [string] $enablepassword = "",
  15.     [string] $command1 = "show interface", #You can add additional commands below here with additonal strings if you want
  16.     [int] $commandDelay = 1000
  17.    )
  18.  
  19. [string] $output = ""
  20.  
  21. ## Read output from a remote host
  22. function GetOutput
  23. {
  24.   ## Create a buffer to receive the response
  25.   $buffer = new-object System.Byte[] 1024
  26.   $encoding = new-object System.Text.AsciiEncoding
  27.  
  28.   $outputBuffer = ""
  29.   $foundMore = $false
  30.  
  31.   ## Read all the data available from the stream, writing it to the
  32.   ## output buffer when done.
  33.   do
  34.   {
  35.     ## Allow data to buffer for a bit
  36.     start-sleep -m 1000
  37.  
  38.     ## Read what data is available
  39.     $foundmore = $false
  40.     $stream.ReadTimeout = 1000
  41.  
  42.     do
  43.     {
  44.       try
  45.       {
  46.         $read = $stream.Read($buffer, 0, 1024)
  47.  
  48.         if($read -gt 0)
  49.         {
  50.           $foundmore = $true
  51.           $outputBuffer += ($encoding.GetString($buffer, 0, $read))
  52.         }
  53.       } catch { $foundMore = $false; $read = 0 }
  54.     } while($read -gt 0)
  55.   } while($foundmore)
  56.  
  57.   $outputBuffer
  58. }
  59.  
  60. function Main
  61. {
  62.   ## Open the socket, and connect to the computer on the specified port
  63.  
  64.   ## Line below disabled for automated operation
  65.   ## write-host "Connecting to $remoteHost on port $port"
  66.  
  67.   trap { Write-Error "Could not connect to remote computer: $_"; exit }
  68.   $socket = new-object System.Net.Sockets.TcpClient($remoteHost, $port)
  69.  
  70.   ## Line below disabled for automated operation
  71.   ## write-host "Connected. Press ^D followed by [ENTER] to exit.`n"
  72.  
  73.   $stream = $socket.GetStream()
  74.  
  75.   $writer = new-object System.IO.StreamWriter $stream
  76.  
  77.     ## Receive the output that has buffered so far
  78.     $SCRIPT:output += GetOutput
  79.  
  80.         $writer.WriteLine($username)
  81.         $writer.Flush()
  82.         Start-Sleep -m $commandDelay
  83.                 $writer.WriteLine($password)
  84.         $writer.Flush()
  85.         Start-Sleep -m $commandDelay
  86.                 $writer.WriteLine($termlength)
  87.         $writer.Flush()
  88.         Start-Sleep -m $commandDelay
  89.                 $writer.WriteLine($enable)
  90.         $writer.Flush()
  91.         Start-Sleep -m $commandDelay
  92.                 $writer.WriteLine($enablepassword)
  93.         $writer.Flush()
  94.         Start-Sleep -m $commandDelay
  95.                 $writer.WriteLine($command1) #Add additional entries below here for additional 'strings' you created above
  96.         $writer.Flush()
  97.         Start-Sleep -m $commandDelay
  98.         $SCRIPT:output += GetOutput
  99.                
  100.  
  101.  
  102.   ## Close the streams
  103.   $writer.Close()
  104.   $stream.Close()
  105.  
  106.   ## Test to see if string required is present in stream
  107.   $StringPresent = $output -Match 'ICA'
  108.  
  109.   ## Output results in XML-formatted response to host console
  110.   Write-Host "<prtg>"
  111.   Write-Host "<result>"
  112.   Write-Host "<channel>String Present</channel>"
  113.   If ($StringPresent) {Write-Host "<value>1</value>"}
  114.   Else {Write-Host "<value>0</value>"}
  115.   Write-Host "</result>"
  116.   Write-Host "</prtg>"
  117. }
  118. . Main
Add Comment
Please, Sign In to add comment