Quantcast
Channel: sshnet Discussions Rss Feed
Viewing all articles
Browse latest Browse all 1729

New Post: on cisco router no response after send Enable

$
0
0
All right Pat, da_rinkes,

Perhaps you wonder what I'm working at. I have a system, in use for some 10 years now to, by which people can perform settings on switches/routers.
Up to now I used external programs to do the actual communication to the switches. I want to have more control and not to depend on these programs.

I've created a class hierarchy switchSession and switchSshSession to handle sending a script containing commands and expected results to a switch/router.

Perhaps it's of some use to somebody else.

Base class switchSession
    ' Class switchSession is the base class for switchSshSession.
    '
    ' A switch session is the execution of a (text) script. The script contains alternating WAIT and SEND lines:
    '   WAIT "expected response"
    '   SEND "command"
    ' This format of SEND and WAIT commands has been in use for a long time and is constructed by the front-end
    ' application. The classes in this file implement the execution of such scripts.
    '
    ' example:
    '
    '192.168.88.238
    'Wait "Username:"
    'SEND "******@******\m"
    'WAIT "Password:"
    'SEND "*********\m"
    'wait ">"
    'SEND "enable\m"
    'WAIT "Password:"
    'SEND "********\m"
    'wait "#"
    'send "terminal length 0\m"
    'wait "#"
    'send "show mac-address-table vlan 1\m"
    'wait "#"
    'send "exit\m"

    Class switchSession
        ' base class switchSession
        Protected ClientIP As String = Nothing          ' ip address
        Protected ClientEndPort As IPEndPoint = Nothing
        Protected ClientResponseWait As Integer = 0     ' wait time (ms) to wait for response after sending a command
        Protected ClientResponseRetries As Integer = 0  ' nr of allowed retries when ScriptWait value is not (yet) received
        Protected ClientResponse As String = ""         ' responses received from the switch
        Protected ClientUsername As String = ""         ' username
        Protected ClientPassword As String = ""         ' password
        Protected ClientPort As Integer = 0             ' port (23: telnet, 22: ssh)
        Protected ClientDisplayscript As String = ""    ' password and username less version of the scriptText
        Protected sessionIOindex As Integer             ' index in send and wait
        Protected ScriptSend() As String                ' commands to send
        Protected ScriptWait() As String                ' expected responses
        Protected ClientValid As Boolean = False
        Protected sessionStatus As String = ""
        Public Sub New(scriptText As String, ResponseWait As Integer, ResponseRetries As Integer)
            ' Main purpose of base class switchSession New constructor is to convert the scriptText with SEND and WAIT lines
            ' into ScriptSend and ScriptWait string array's.
            ' Along with building ScriptSend (from SEND lines) and ScriptWait (from WAIT lines) ClientDisplayscript is filled,
            ' which will contain all SEND en WAIT lines but with username and password replaced by "********" (to allow an
            ' username and password less version of the script to present to the user)

            Dim qSend As New Queue(Of String)
            Dim qWait As New Queue(Of String)
            Try
                sessionStatus = "parse scripttext"
                scriptText = Regex.Replace(scriptText, "Wait " & Chr(34), "", RegexOptions.IgnoreCase)  ' remove all "wait"'s
                scriptText = Regex.Replace(scriptText, "Send " & Chr(34), "", RegexOptions.IgnoreCase)  ' remove all "send"'s
                scriptText = scriptText.Replace("\m" & Chr(34), "")                                     ' remove all command-end strings: \m"
                scriptText = scriptText.Replace("\M" & Chr(34), "")
                scriptText = scriptText.Replace(Chr(34), "")                                            ' remove all "

                For Each aIOstring As String In scriptText.Split(Environment.NewLine)
                    If sessionIOindex = 0 Then ClientIP = aIOstring.Trim
                    If sessionIOindex = 2 Then ClientUsername = aIOstring.Trim
                    If sessionIOindex = 4 Then ClientPassword = aIOstring.Trim
                    If sessionIOindex Mod 2 = 0 Or sessionIOindex = 0 Then
                        qSend.Enqueue(aIOstring.Trim)               ' even = send "..."
                        ClientDisplayscript += aIOstring & Environment.NewLine
                    Else
                        qWait.Enqueue(aIOstring.ToLower.Trim)       ' odd = wait "..."
                        If sessionIOindex <> 4 And sessionIOindex <> 8 Then
                            ClientDisplayscript += aIOstring & Environment.NewLine
                        Else
                            ClientDisplayscript += "********" & Environment.NewLine
                        End If
                    End If
                    sessionIOindex += 1
                Next
                sessionStatus = sessionIOindex & " lines in scripttext, " & qSend.Count & " script commands " & qWait.Count & " expected prompts"
                ReDim ScriptSend(qSend.Count - 1)
                ReDim ScriptWait(qWait.Count - 1)
                Dim i As Integer = 0
                While qSend.Count > 0
                    ScriptSend(i) = qSend.Dequeue
                    i += 1
                End While
                i = 0
                While qWait.Count > 0
                    ScriptWait(i) = qWait.Dequeue
                    i += 1
                End While

                qSend.Clear()
                qWait.Clear()

                ClientResponseWait = ResponseWait
                ClientResponseRetries = ResponseRetries

                ClientValid = True
                sessionIOindex = 0

            Catch ex As Exception
                ClientValid = False
                sessionStatus += Environment.NewLine & ex.Message
            End Try

        End Sub
        Public Function ExecuteScript() As Boolean
            Return False
        End Function

        Public ReadOnly Property ScriptDisplay As String
            Get
                Return ClientDisplayscript
            End Get
        End Property
        Public ReadOnly Property ScripLog As String
            Get
                Return ClientResponse
            End Get
        End Property
        Public ReadOnly Property isValid() As Boolean
            Get
                Return ClientValid
            End Get
        End Property
        Public ReadOnly Property status() As String
            Get
                Return sessionStatus
            End Get
        End Property
        Public Sub Dispose()
            ClientDisplayscript = ""
            ClientResponse = ""
            Array.Clear(ScriptSend, 0, ScriptSend.Length)
            Array.Clear(ScriptWait, 0, ScriptWait.Length)
            ClientIP = ""
            ClientUsername = ""
            ClientPassword = ""
        End Sub
    End Class
continued below...

Viewing all articles
Browse latest Browse all 1729

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>