Advertisement
Guest User

Untitled

a guest
Dec 20th, 2014
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.52 KB | None | 0 0
  1. $tel_WaitTime = 1000
  2.  
  3.  
  4. #finds the last occurance of $command, in the string array $output, then returns every array element (string) in $output AFTER the last occurance of command
  5. #in other words, this function returns the output of the last command issued
  6. Function SeekOutput([string[]]$output, $command)
  7. {
  8. [string[]]$collector = $command
  9. $output | foreach-object{ #for each line of $output... (ASSUME the command WAS previously issued)
  10. $collector += $_ #...add the line to the $collector array
  11. if($_ -match $command){ #...but, if you happen to run across the $command in the $output again,
  12. $collector = $command #......that means it's not the LAST (most current) one issued. So clear the $collector and start over
  13. }
  14. }
  15. return $collector
  16.  
  17. }
  18.  
  19. #This function creates a new object (a "connection"), which opens a new telnet session and blackboxes the overhead telnet session management stuff
  20. #Each router (object)(see NewRouter()), will have a connection associated with it which should remain open. The router object shouldn't need to worry
  21. #about opening and closing the connection. The router object should only have to worry about providing the port number, and sending writes and reads to the connection
  22. Function NewConnection($portnumber)
  23. {
  24. $o_connection = @{ 'port' = ""; 'socket' = $null; 'stream' = $null; 'writer' = $null; 'buffer' = $null; 'encoding' = $null }
  25. $getPort = { return $this.port }
  26. $setPort = { param($portnumber) $this.port = $portnumber }
  27.  
  28. #writes an array of commands to the telnet server
  29. $write = {
  30. param ([string[]]$Commands)
  31. $r = $this
  32. foreach($Command in $Commands){
  33. $r.writer.WriteLine($Command)
  34. $r.writer.Flush()
  35. }
  36. }
  37.  
  38. #reads from the telnet session
  39. $read = {
  40. $r = $this
  41. $r.stream.flush()
  42. While($r.stream.DataAvailable){
  43. $Read = $r.stream.Read($r.buffer, 0, 1024)
  44. $result += ($r.encoding.GetString($r.buffer, 0, $Read))
  45. }
  46. return $result.split("`n")
  47.  
  48. }
  49.  
  50. #Basically a combination of the read and the write method. There are 3 important things added here, though.
  51. #1. There is a wait time added between the write and the read (to make sure the end device has time to process the commands written to it)
  52. #2. It calls seekOutput(), which basically finds the last occurance of the command sent, and returns all output afterwards
  53. #3. It deals with the "--More--" sent back when the output is too long for "the terminal".
  54. $sendCommands = {
  55. param([string[]]$commands)
  56. $r = $this
  57. $commands |% { $r.write($commands) } #for each $command given, send it to the device
  58. Start-sleep -milliseconds $tel_WaitTime #wait a little bit, otherwise, you might not get ALL of the output from the device
  59. [string[]]$output = $r.read() #read the output produced by the command(s) sent ^ ^ ^
  60. while($output[($output.length-1)] -match "--More--"){ #If the output is too long, then "--More--" will be sent back as the last line. Now we need to deal with this
  61. $r.write(" ") #.....write a "space bar press" to the device to scroll through --More--
  62. Start-sleep -milliseconds $tel_WaitTime #.....damn it, now we need to wait for a little bit before reading again
  63. $output += $r.read() #.....read from the device again, and add on the output (actually, i THINK this will just re-read everything and append it to what has already been read. oh well..)
  64. #......... that doesn't matter, because we're about to call SeekOutput, which grabs the output of the LAST command issued
  65. } #repeat if necessary (until there's no more --More--'s)
  66.  
  67. [string[]]$rv = SeekOutput $output $commands[0] #Find the output of the sent command (note: if more than one $command is sent at a time, this will return the OUTPUT from the FIRST command AND all subsequent commands. It's recommended to only send one "significant" command at a time using this function if you're wanting the output--otherwise, use write() instead
  68. #The functionality of being able to send multiple commands is provided for convenience. For example, it's a good idea to send "end" (to ensure you're in EXEC mode) before any "show" command is issued. "end" doesn't produce any significant output...
  69. #...we can pretty much ignore it's output, and then use regex's on the significant output)
  70. return $rv
  71. }
  72.  
  73.  
  74. $connection = New-Object -TypeName PSObject -property $o_connection
  75. Add-Member -InputObject $connection -MemberType ScriptMethod -Name getPort -Value $getPort
  76. Add-Member -InputObject $connection -MemberType ScriptMethod -Name write -Value $write
  77. Add-Member -InputObject $connection -MemberType ScriptMethod -Name read -Value $read
  78. Add-Member -InputObject $connection -MemberType ScriptMethod -Name sendCommands -Value $sendCommands
  79.  
  80.  
  81. $connection.port = $portnumber
  82. $connection.socket = New-Object System.Net.Sockets.TcpClient("127.0.0.1", $connection.getPort())
  83. $connection.stream = $connection.socket.getStream()
  84. $connection.buffer = New-Object System.Byte[] 1024
  85. $connection.encoding = New-Object System.Text.AsciiEncoding
  86. $connection.writer = New-Object System.IO.StreamWriter($connection.stream)
  87.  
  88. return $connection
  89. }
  90.  
  91.  
  92. Function NewRouter($number)
  93. {
  94. $o_router = @{ 'name' = $null; 'connection' = $null; 'number' = $null }
  95.  
  96. #This is one of the main reasons I'm posting to Reddit
  97. #I'm not sure how to go about this a different way. The next 3 methods are really just wrappers for $this.connection.xxx()
  98. #I don't want to have to reference $myrouter.connection.write(). Instead, I want to simply do $myrouter.write()
  99. #I would assume that there's a much better way than this....
  100. $read = {
  101. return ($this.connection.read())
  102. }
  103. $write = {
  104. param ([string[]]$Commands)
  105. $this.connection.write($Commands)
  106. }
  107. $sendCommands = {
  108. param([string[]]$commands)
  109. [string[]]$rv = $this.connection.sendCommands($commands)
  110. return $rv
  111. }
  112.  
  113. $r = New-Object -TypeName PSObject -property $o_router
  114. Add-Member -InputObject $r -MemberType ScriptMethod -Name write -Value $write
  115. Add-Member -InputObject $r -MemberType ScriptMethod -Name read -Value $read
  116. Add-Member -InputObject $r -MemberType ScriptMethod -Name sendCommands -Value $sendCommands
  117. $r.connection = NewConnection $number
  118.  
  119. return $r
  120. }
  121.  
  122.  
  123.  
  124. $a = NewRouter 2003 #create a new router. It's "connection" should connect to 127.0.0.1:2003
  125.  
  126.  
  127.  
  128. $x = ($a.sendCommands(("show ip int brief")))
  129. $x | % { $_ }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement