Guest User

Untitled

a guest
Apr 6th, 2016
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .SYNOPSIS
  3.     Simple script to connect to a server using Telnet, issue commands and
  4.     capture all of the output.
  5. .DESCRIPTION
  6.     I wrote this script to connect to my Extreme Network switches and download
  7.     their configuration.  I've also gotten it to work on Dell switches, though
  8.     I had to adjust the WaitTime up a little bit to get all of the output.
  9.    
  10.     Because it is expected that you will be wanting to attach to several different
  11.     devices to get their configurations I wrote this as a Function.  The intention
  12.     is that at the bottom of the script you will have a list of devices you want
  13.     to capture, and they may have different command and WaitTime requirements (as
  14.     well as different output files).  I put several examples the script below,
  15.     with full explanations in the Help Section.
  16. .PARAMETER Commands
  17.     An array with a single command in each element.  Can be input from a file--
  18.     using Get-Content--can come from the pipeline or be specified directly from
  19.     the parameter.
  20.    
  21.     Note about Commands: If you have a dollar sign in your password you MUST
  22.     escape it in order for it to work:
  23.         Example:  -Commands "admin","$ecurePa$$word"
  24.                   This will not work!
  25.         Example:  -Commands "admin","`$ecurePa`$`$word"
  26.                   This WILL work
  27. .PARAMETER RemoteHost
  28.     Name or IP address of the host you wish to connect to.
  29. .PARAMETER Port
  30.     Port number you need to attach to.
  31. .PARAMETER WaitTime
  32.     The amount of time the script waits in between issuing commands, in Milliseconds.
  33.     When you run this script you must take a look at the output file to make sure
  34.     you got all of the output you want.  If things are cut off then the script is
  35.     running too quickly and you have to slow it down by using a higher number.
  36.     1000 milliseconds is 1 second.
  37. .PARAMETER OutputPath
  38.     Full path and file name of where you want the script to save the output.
  39. .INPUTS
  40.     String input from Pipeline.
  41. .OUTPUTS
  42.     Text file at specified location.
  43. .EXAMPLE
  44.     Get-Telnet -RemoteHost "192.168.1.2" -OutputPath "\\server\share\hqswitches.txt"
  45.    
  46.     Will Telnet to 192.168.1.2 and execute the default commands, saving the output on
  47.     the server as hqswitches.txt
  48. .EXAMPLE
  49.     Get-Telnet -RemoteHost "10.10.10.2" -Commands "admin","password","terminal datadump","show run" -OutputPath "\\server\share\DellHQswitches.txt" -WaitTime 2000
  50.    
  51.     Will Telnet to a Dell switch at 10.10.10.2, login using admin/password.  Issues the
  52.     command terminal datadump (which tells Dell to not pause output) and than so the
  53.     full running configuration and save the output to the server as DellHQSwitches.txt.
  54.     The WaitTime had to be adjusted to up because the script was running too fast for the
  55.     switch.
  56. .EXAMPLE
  57.     Get-Telnet -RemoteHost "192.168.10.1" -Commands "admin","password","terminal pager 0","show run" -OutputPath "\\server\share\CiscoFirewall.txt"
  58.    
  59.     An example of how to connect to a Cisco ASA firewall and save the running config to
  60.     a file.
  61. .EXAMPLE
  62.     Get-Content "c:\scripts\commands.txt" | Get-Telnet -RemoteHost "192.168.10.1" -OutputPath "\\server\share\ciscoswitch.txt" -WaitTime 1500
  63.     Get-Telnet -Commands (Get-Content "c:\scripts\commands.txt") -RemoteHost "192.168.10.1" -OutputPath "\\server\share\ciscoswitch.txt" -WaitTime 1500
  64.    
  65.     Two examples of how to use Get-Content to pull a series of commands from a text file
  66.     and execute them.
  67. .NOTES
  68.     Author:            Martin Pugh
  69.     Twitter:           @thesurlyadm1n
  70.     Spiceworks:        Martin9700
  71.     Blog:              www.thesurlyadmin.com
  72.        
  73.     Changelog:
  74.        1.0             Initial Release
  75. .LINK
  76.     http://community.spiceworks.com/scripts/show/1887-get-telnet-telnet-to-a-device-and-issue-commands
  77. #>
  78. Function Get-Telnet
  79. {   Param (
  80.         [Parameter(ValueFromPipeline=$true)]
  81.         [String[]]$Commands = @("username","password","disable clipaging","sh config"),
  82.         [string]$RemoteHost = "HostnameOrIPAddress",
  83.         [string]$Port = "23",
  84.         [int]$WaitTime = 1000,
  85.         [string]$OutputPath = "\\server\share\switchbackup.txt"
  86.     )
  87.     #Attach to the remote device, setup streaming requirements
  88.     $Socket = New-Object System.Net.Sockets.TcpClient($RemoteHost, $Port)
  89.     If ($Socket)
  90.     {   $Stream = $Socket.GetStream()
  91.         $Writer = New-Object System.IO.StreamWriter($Stream)
  92.         $Buffer = New-Object System.Byte[] 1024
  93.         $Encoding = New-Object System.Text.AsciiEncoding
  94.  
  95.         #Now start issuing the commands
  96.         ForEach ($Command in $Commands)
  97.         {   $Writer.WriteLine($Command)
  98.             $Writer.Flush()
  99.             Start-Sleep -Milliseconds $WaitTime
  100.         }
  101.         #All commands issued, but since the last command is usually going to be
  102.         #the longest let's wait a little longer for it to finish
  103.         Start-Sleep -Milliseconds ($WaitTime * 4)
  104.         $Result = ""
  105.         #Save all the results
  106.         While($Stream.DataAvailable)
  107.         {   $Read = $Stream.Read($Buffer, 0, 1024)
  108.             $Result += ($Encoding.GetString($Buffer, 0, $Read))
  109.         }
  110.     }
  111.     Else    
  112.     {   $Result = "Unable to connect to host: $($RemoteHost):$Port"
  113.     }
  114.     #Done, now save the results to a file
  115.     $Result | Out-File $OutputPath
  116. }
  117.  
  118. ##Edit below
  119. Get-Telnet -RemoteHost "192.168.1.2" -Port 23  -Commands "username","password","disable clipaging","sh config" -OutputPath "C:\Logfile.txt"
Advertisement
Add Comment
Please, Sign In to add comment