Advertisement
Guest User

Untitled

a guest
Aug 25th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. ## PowerShell: Script To Telnet To Remote Hosts And Run Commands Against Them With Output To A File ##
  2. ## Overview: Useful for Telnet connections to Cisco Switches and other devices. Can add additional command strings
  3. ## Usage Examples: Add your environment specific details into the parameters below, or include when calling the script:
  4. ## ./Auth-Telnet.ps1 "127.0.0.1" "23" "admin" "password"
  5.  
  6. param(
  7. [string] $remoteHost = "10.64.80.15",
  8. [int] $port = 23,
  9. [string] $username = "****",
  10. [string] $password = "****",
  11. [int] $commandDelay = 1000
  12. )
  13.  
  14. [string] $output = ""
  15.  
  16. ## Read output from a remote host
  17. function GetOutput
  18. {
  19. ## Create a buffer to receive the response
  20. $buffer = new-object System.Byte[] 1024
  21. $encoding = new-object System.Text.AsciiEncoding
  22.  
  23. $outputBuffer = ""
  24. $foundMore = $false
  25.  
  26. ## Read all the data available from the stream, writing it to the
  27. ## output buffer when done.
  28. do {
  29. ## Allow data to buffer for a bit
  30. start-sleep -m $commandDelay
  31.  
  32. ## Read what data is available
  33. $foundMore = $false
  34. $stream.ReadTimeout = $commandDelay
  35.  
  36. do {
  37. try {
  38. $read = $stream.Read($buffer, 0, 1024)
  39.  
  40. if ($read -gt 0) {
  41. $foundMore = $true
  42. $outputBuffer += ($encoding.GetString($buffer, 0, $read))
  43. }
  44. } catch { $foundMore = $false; $read = 0 }
  45. } while ($read -gt 0)
  46. } while ($foundMore)
  47.  
  48. $outputBuffer
  49. }
  50.  
  51. function Main
  52. {
  53. do {
  54. ## Open the socket, and connect to the computer on the specified port
  55. Write-Host "Connecting to $remoteHost on port $port"
  56.  
  57. trap { Write-Error "Could not connect to remote computer: $_"; exit }
  58. $socket = New-Object System.Net.Sockets.TcpClient($remoteHost, $port)
  59.  
  60. # Write-Host "Connected. Press ^D followed by [ENTER] to exit.`n"
  61. Write-Host "Connected.`n"
  62.  
  63. $stream = $socket.GetStream()
  64. $writer = New-Object System.IO.StreamWriter $stream
  65.  
  66. ## Receive the output that has buffered so far
  67. while (-not (GetOutput -match "Username:")) { sleep 1 }
  68. $writer.WriteLine($username)
  69. $writer.Flush()
  70.  
  71. while (-not (GetOutput -match "Password:")) { sleep 1 }
  72. $writer.WriteLine($password)
  73. $writer.Flush()
  74.  
  75. $SCRIPT:output += GetOutput
  76.  
  77. ## Close the streams
  78. $writer.Close()
  79. $stream.Close()
  80.  
  81. $output
  82. $output | Out-File ".\$remoteHost.txt" #Change this to suit your environment
  83. } until ($SCRIPT:output -match "Authentication Successful")
  84. }
  85.  
  86. Main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement