Hi there!
I've search everywhere, tried using expect, tried using other platforms like PowerShell, but I can't find a solution to this:
I can set up a connection with the Cisco router, create a shellstream and issue commands and receive the results up to the point where I issue the enable command. From that point all I write into the stream appears to be disappearing and there's no data available when I try to read from the stream.
I really hope someone can point me in the right direction!
Here's the test-code:
kind regards, Paul
I've search everywhere, tried using expect, tried using other platforms like PowerShell, but I can't find a solution to this:
I can set up a connection with the Cisco router, create a shellstream and issue commands and receive the results up to the point where I issue the enable command. From that point all I write into the stream appears to be disappearing and there's no data available when I try to read from the stream.
I really hope someone can point me in the right direction!
Here's the test-code:
Imports System.IO
Imports Renci.SshNet
Imports Renci.SshNet.Common
Imports System.Threading
Imports System.Text
Module test
Const ClientIP As String = "192.168.0.220"
Const ClientUsername As String = "paul"
Const ClientPassword As String = "apassword"
Const ClientEnablepassword As String = "enablepassword"
Dim ClientResponse As String
Public Sub Test()
Try
Dim sshOutput As New StringBuilder
Dim PasswordConnection = New PasswordAuthenticationMethod(ClientUsername, ClientPassword)
Dim KeyboardInteractive = New KeyboardInteractiveAuthenticationMethod(ClientUsername)
Dim ConnectionInfo = New ConnectionInfo(ClientIP, 22, ClientUsername, PasswordConnection, KeyboardInteractive)
AddHandler KeyboardInteractive.AuthenticationPrompt, AddressOf handleAuthentication
Dim sshClient = New SshClient(ConnectionInfo)
sshClient.ConnectionInfo.Timeout = TimeSpan.FromSeconds(10)
sshClient.ConnectionInfo.RetryAttempts = 3
sshClient.Connect()
If sshClient.IsConnected = False Then
Throw New Exception("connection failed")
Exit Sub
End If
Dim sshStream = sshClient.CreateShellStream("dumb", 80, 24, 800, 600, 1024)
Dim reader = New StreamReader(sshStream)
Dim writer = New StreamWriter(sshStream)
writer.AutoFlush = True
Thread.Sleep(1000)
reader.ReadToEnd()
sshStream.Write("terminal length 0" & vbCr)
Thread.Sleep(100)
sshOutput.Append(reader.ReadToEnd)
sshStream.Write("show users" & vbCr)
Thread.Sleep(100)
sshOutput.Append(reader.ReadToEnd)
sshStream.Write("enable" & vbCr)
Thread.Sleep(100)
sshOutput.Append(reader.ReadToEnd)
sshStream.Write(ClientEnablepassword & vbCr)
Thread.Sleep(100)
sshOutput.Append(reader.ReadToEnd)
sshStream.Write("show running-config" & vbCr)
Thread.Sleep(1000)
sshOutput.Append(reader.ReadToEnd)
Thread.Sleep(100)
sshStream.Write("exit" & vbCr)
sshOutput.Append(reader.ReadToEnd)
ClientResponse = sshOutput.ToString
sshStream.Close()
sshStream.Dispose()
sshClient.Disconnect()
sshClient.Dispose()
Catch ex As Exception
Debug.Print(ex.Message.ToString)
End Try
End Sub
Sub handleAuthentication(sender As Object, e As AuthenticationPromptEventArgs)
For Each prompt As AuthenticationPrompt In e.Prompts
If prompt.Request.Equals("Password: ", StringComparison.InvariantCultureIgnoreCase) Then
prompt.Response = ClientPassword
End If
Next
End Sub
End Module
and this is the final result in ClientResponse:terminal length 0
c3745>show users
Line User Host(s) Idle Location
0 con 0 idle 01:33:52
*162 vty 0 paul idle 00:00:00 192.168.0.102
Interface User Mode Idle Peer Address
c3745>enable
Password:
Any help will be greatly appreciated!kind regards, Paul