Guest User

Untitled

a guest
Apr 5th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 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. ## ./PowerShellTelnetRemoteSession.ps1 "127.0.0.1" "23" "admin" "password" "term len 0" "en" "enablepassword" "show interface"
  5.  
  6. param(
  7. [string] $remoteHost = "",
  8. [int] $port = 23,
  9. [string] $username = "",
  10. [string] $password = "",
  11. [string] $termlength = "term len 0", #Useful for older consoles that have line display limitations
  12. [string] $enable = "en", #Useful for appliances like Cisco switches that have an 'enable' command mode
  13. [string] $enablepassword = "",
  14. [string] $command1 = "show interface", #You can add additional commands below here with additonal strings if you want
  15. [int] $commandDelay = 1000
  16. )
  17.  
  18. [string] $output = ""
  19.  
  20. ## Read output from a remote host
  21. function GetOutput
  22. {
  23. ## Create a buffer to receive the response
  24. $buffer = new-object System.Byte[] 1024
  25. $encoding = new-object System.Text.AsciiEncoding
  26.  
  27. $outputBuffer = ""
  28. $foundMore = $false
  29.  
  30. ## Read all the data available from the stream, writing it to the
  31. ## output buffer when done.
  32. do
  33. {
  34. ## Allow data to buffer for a bit
  35. start-sleep -m 1000
  36.  
  37. ## Read what data is available
  38. $foundmore = $false
  39. $stream.ReadTimeout = 1000
  40.  
  41. do
  42. {
  43. try
  44. {
  45. $read = $stream.Read($buffer, 0, 1024)
  46.  
  47. if($read -gt 0)
  48. {
  49. $foundmore = $true
  50. $outputBuffer += ($encoding.GetString($buffer, 0, $read))
  51. }
  52. } catch { $foundMore = $false; $read = 0 }
  53. } while($read -gt 0)
  54. } while($foundmore)
  55.  
  56. $outputBuffer
  57. }
  58.  
  59. function Main
  60. {
  61. ## Open the socket, and connect to the computer on the specified port
  62.  
  63. write-host "Connecting to $remoteHost on port $port"
  64.  
  65. trap { Write-Error "Could not connect to remote computer: $_"; exit }
  66. $socket = new-object System.Net.Sockets.TcpClient($remoteHost, $port)
  67.  
  68. write-host "Connected. Press ^D followed by [ENTER] to exit.`n"
  69.  
  70. $stream = $socket.GetStream()
  71.  
  72. $writer = new-object System.IO.StreamWriter $stream
  73.  
  74. ## Receive the output that has buffered so far
  75. $SCRIPT:output += GetOutput
  76.  
  77. $writer.WriteLine($username)
  78. $writer.Flush()
  79. Start-Sleep -m $commandDelay
  80. $writer.WriteLine($password)
  81. $writer.Flush()
  82. Start-Sleep -m $commandDelay
  83. $writer.WriteLine($termlength)
  84. $writer.Flush()
  85. Start-Sleep -m $commandDelay
  86. $writer.WriteLine($enable)
  87. $writer.Flush()
  88. Start-Sleep -m $commandDelay
  89. $writer.WriteLine($enablepassword)
  90. $writer.Flush()
  91. Start-Sleep -m $commandDelay
  92. $writer.WriteLine($command1) #Add additional entries below here for additional 'strings' you created above
  93. $writer.Flush()
  94. Start-Sleep -m $commandDelay
  95. $SCRIPT:output += GetOutput
  96.  
  97.  
  98.  
  99. ## Close the streams
  100. $writer.Close()
  101. $stream.Close()
  102.  
  103. $output | Out-File ("C:\Users\msenturk\desktop\inhand\$remoteHost.txt") #Change this to suit your environment
  104. }
  105. . Main
Add Comment
Please, Sign In to add comment