Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $tel_WaitTime = 1000
- #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
- #in other words, this function returns the output of the last command issued
- Function SeekOutput([string[]]$output, $command)
- {
- [string[]]$collector = $command
- $output | foreach-object{ #for each line of $output... (ASSUME the command WAS previously issued)
- $collector += $_ #...add the line to the $collector array
- if($_ -match $command){ #...but, if you happen to run across the $command in the $output again,
- $collector = $command #......that means it's not the LAST (most current) one issued. So clear the $collector and start over
- }
- }
- return $collector
- }
- #This function creates a new object (a "connection"), which opens a new telnet session and blackboxes the overhead telnet session management stuff
- #Each router (object)(see NewRouter()), will have a connection associated with it which should remain open. The router object shouldn't need to worry
- #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
- Function NewConnection($portnumber)
- {
- $o_connection = @{ 'port' = ""; 'socket' = $null; 'stream' = $null; 'writer' = $null; 'buffer' = $null; 'encoding' = $null }
- $getPort = { return $this.port }
- $setPort = { param($portnumber) $this.port = $portnumber }
- #writes an array of commands to the telnet server
- $write = {
- param ([string[]]$Commands)
- $r = $this
- foreach($Command in $Commands){
- $r.writer.WriteLine($Command)
- $r.writer.Flush()
- }
- }
- #reads from the telnet session
- $read = {
- $r = $this
- $r.stream.flush()
- While($r.stream.DataAvailable){
- $Read = $r.stream.Read($r.buffer, 0, 1024)
- $result += ($r.encoding.GetString($r.buffer, 0, $Read))
- }
- return $result.split("`n")
- }
- #Basically a combination of the read and the write method. There are 3 important things added here, though.
- #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)
- #2. It calls seekOutput(), which basically finds the last occurance of the command sent, and returns all output afterwards
- #3. It deals with the "--More--" sent back when the output is too long for "the terminal".
- $sendCommands = {
- param([string[]]$commands)
- $r = $this
- $commands |% { $r.write($commands) } #for each $command given, send it to the device
- Start-sleep -milliseconds $tel_WaitTime #wait a little bit, otherwise, you might not get ALL of the output from the device
- [string[]]$output = $r.read() #read the output produced by the command(s) sent ^ ^ ^
- 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
- $r.write(" ") #.....write a "space bar press" to the device to scroll through --More--
- Start-sleep -milliseconds $tel_WaitTime #.....damn it, now we need to wait for a little bit before reading again
- $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..)
- #......... that doesn't matter, because we're about to call SeekOutput, which grabs the output of the LAST command issued
- } #repeat if necessary (until there's no more --More--'s)
- [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
- #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...
- #...we can pretty much ignore it's output, and then use regex's on the significant output)
- return $rv
- }
- $connection = New-Object -TypeName PSObject -property $o_connection
- Add-Member -InputObject $connection -MemberType ScriptMethod -Name getPort -Value $getPort
- Add-Member -InputObject $connection -MemberType ScriptMethod -Name write -Value $write
- Add-Member -InputObject $connection -MemberType ScriptMethod -Name read -Value $read
- Add-Member -InputObject $connection -MemberType ScriptMethod -Name sendCommands -Value $sendCommands
- $connection.port = $portnumber
- $connection.socket = New-Object System.Net.Sockets.TcpClient("127.0.0.1", $connection.getPort())
- $connection.stream = $connection.socket.getStream()
- $connection.buffer = New-Object System.Byte[] 1024
- $connection.encoding = New-Object System.Text.AsciiEncoding
- $connection.writer = New-Object System.IO.StreamWriter($connection.stream)
- return $connection
- }
- Function NewRouter($number)
- {
- $o_router = @{ 'name' = $null; 'connection' = $null; 'number' = $null }
- #This is one of the main reasons I'm posting to Reddit
- #I'm not sure how to go about this a different way. The next 3 methods are really just wrappers for $this.connection.xxx()
- #I don't want to have to reference $myrouter.connection.write(). Instead, I want to simply do $myrouter.write()
- #I would assume that there's a much better way than this....
- $read = {
- return ($this.connection.read())
- }
- $write = {
- param ([string[]]$Commands)
- $this.connection.write($Commands)
- }
- $sendCommands = {
- param([string[]]$commands)
- [string[]]$rv = $this.connection.sendCommands($commands)
- return $rv
- }
- $r = New-Object -TypeName PSObject -property $o_router
- Add-Member -InputObject $r -MemberType ScriptMethod -Name write -Value $write
- Add-Member -InputObject $r -MemberType ScriptMethod -Name read -Value $read
- Add-Member -InputObject $r -MemberType ScriptMethod -Name sendCommands -Value $sendCommands
- $r.connection = NewConnection $number
- return $r
- }
- $a = NewRouter 2003 #create a new router. It's "connection" should connect to 127.0.0.1:2003
- $x = ($a.sendCommands(("show ip int brief")))
- $x | % { $_ }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement