New Post: How to maintain a 'conversation' with an SSH session?
New Post: How to maintain a 'conversation' with an SSH session?
New Post: How to maintain a 'conversation' with an SSH session?
To preserve space here, I will say that I created for my tests several functions, a few of which are:
Function SendCommand(cmd As String, s As ShellStream) As String
Try
reader = New StreamReader(s)
writer = New StreamWriter(s)
writer.AutoFlush = True
writer.WriteLine(cmd)
While s.Length = 0
Thread.Sleep(500)
End While
Catch ex As Exception
Debug.WriteLine("SendCommand(" & cmd & ") caught exception: " & ex.ToString())
End Try
Return reader.ReadToEnd()
End Function
Function SendCommandW(cmd As String, s As ShellStream) As String
Try
reader = New StreamReader(s)
writer = New StreamWriter(s)
writer.AutoFlush = True
writer.Write(cmd)
While s.Length = 0
Thread.Sleep(500)
End While
Catch ex As Exception
Debug.WriteLine("SendCommandW(" & cmd & ") caught exception: " & ex.ToString())
End Try
Return reader.ReadToEnd()
End Function
On the blog I take the time to explain why there is more than one SendCommand(). You can probably tell from the above why-especially if you have had to work with Cisco IOS for any length of time. So all you so is create a ShellStream, and pass it to one of the functions. That function will return only the text from the command. This was the hard part. Once I got it to work, it seemed nothing I tried would clear out the buffers-making all previous input/output part of the current output!
I write in lots of languages, but since everyone here discusses in C# (and the source is in C#), I thought I'd help out the VB.NET folks.
This project, and the DLL/library it creates, are really great. Since I don't know what kind of development help is needed, right now all I can do is pass on some working code for others to get over the difficulties that plagued me for about 20 hours!
Thanks again, folks, and let me know if there is any way I can contribute.
pat
:)
New Post: CreateShell
I used your example to also create a solution:
Function SendCommand(cmd As String, s As ShellStream) As String
Try
reader = New StreamReader(s)
writer = New StreamWriter(s)
writer.AutoFlush = True
writer.WriteLine(cmd)
While s.Length = 0
Thread.Sleep(500)
End While
Catch ex As Exception
Debug.WriteLine("SendCommand(" & cmd & ") caught exception: " & ex.ToString())
End Try
Return reader.ReadToEnd()
End Function
Function SendCommandW(cmd As String, s As ShellStream) As String
Try
reader = New StreamReader(s)
writer = New StreamWriter(s)
writer.AutoFlush = True
writer.Write(cmd)
While s.Length = 0
Thread.Sleep(500)
End While
Catch ex As Exception
Debug.WriteLine("SendCommandW(" & cmd & ") caught exception: " & ex.ToString())
End Try
Return reader.ReadToEnd()
End Function
Function SendCommandCR(cmd As String, s As ShellStream) As String
Try
reader = New StreamReader(s)
writer = New StreamWriter(s)
writer.AutoFlush = True
writer.Write(cmd & vbCr)
While s.Length = 0
Thread.Sleep(500)
End While
Catch ex As Exception
Debug.WriteLine("SendCommandCR(" & cmd & ") caught exception: " & ex.ToString())
End Try
Return reader.ReadToEnd()
End Function
Function SendCommandLF(cmd As String, s As ShellStream) As String
Try
reader = New StreamReader(s)
writer = New StreamWriter(s)
writer.AutoFlush = True
writer.Write(cmd & vbLf)
While s.Length = 0
Thread.Sleep(500)
End While
Catch ex As Exception
Debug.WriteLine("SendCommandLF(" & cmd & ") caught exception: " & ex.ToString())
End Try
Return reader.ReadToEnd()
End Function
A little respect for the VB.NET folks!VS 2012 Ultimate
New Post: Renci.SshNet Encryption using Arcfour
Thank you for any help.
New Post: Why i am not getting complete output while executing time consuming commands
I am trying to execute some 20 commands of which some takes 5 seconds and some of them might take 5 min. So please let me know what is best approach to get the complete output
private void button1_Click(object sender, EventArgs e)
{
using (var client = new SshClient(txtIP.Text.ToString().Trim(), txtUser.Text.ToString().Trim(),txtPassword.Text.ToString().Trim()))
{
client.Connect();
using (var stream = client.CreateShellStream("dumb", 80, 24, 800, 600, 1024))
{
var reader = new StreamReader(stream);
var writer = new StreamWriter(stream);
writer.AutoFlush = true;
while (stream.Length == 0)
{
Thread.Sleep(500);
}
ReadStream(reader);
WriteStream(txtCommand.Text.ToString(), writer, stream);
ReadStream(reader);
}
client.Disconnect();
}
}
private void ReadStream(StreamReader reader)
{
string result="";
string line = reader.ReadLine();
while (line != null)
{
line = reader.ReadLine();
result = result + "\r\n" + line;
}
}
private void WriteStream(string cmd, StreamWriter writer, ShellStream stream)
{
writer.WriteLine(cmd);
while (stream.Length == 0)
{
Thread.Sleep(500);
}
}
Created Unassigned: SftpClient hangs on Diconnect() with certain servers [1814]
I've figured out that the server initiates shutdown of the subsystem (sftp-sever) on client's EOF message sending EOF and CLOSE in response right away.
However, if the client's EOF message is immediately followed by CLOSE one which is the case in SSH.NET, the timing issue may occur because of the race condition when the channel is being closed before the server actually sends its EOF and CLOSE messages already issued.
Since EOF message is optional, a workaround could be simple - not to send it at all.
New Post: Invoke-SshCommand fails to return to PowerShell
I have loaded up the SSH-Sessions module in PowerShell (Windows 2012), and I am able to establish a SSH Session between my Windows Server & ASA Firewall.
When typing:
Get-SshSession
I can see the resulting output showing the connection to my Firewall.
However, when I try to run:
Invoke-SshCommand -ComputerName xxxxx -Command "show checksum"
(any user can run this command with access to the firewall)
I never receive an output, nor am I returned back to PowerShell.
I can break out of PowerShell, re-establish a connection to the firewall, however when I use:
Enter-SshSession -ComputerName xxxxx
I am again not returned to a prompt, PowerShell again just sits there.
Am i missing something in my environment? I do not know where to start troubleshooting, as I am not receiving any errors.
Jens
New Post: Access MS SQL server over SSH.NET
I need to execute some queries in a server outside our net, i have ssh access and I can query with putty, but I need to query with a vb app.
how I configure sql management studio to use ssh?
New Post: Run scp command from remote server via SSHClient
I have a similar situation in which I use private keys. Any attempt to scp a file from Server1 to Server2 is met with permission denied (publickey).
Is there a way I may provide authentication?
Thanks,
Kelley
New Post: Renci.SshNet.SftpClient - Serialization Exception
I'm using Renci.SshNet in my library project, but when my class is executed the following exception occurs (pasted below):
Does anyone know how to "mark the Renci.SshNet.SftpClient as serializable"? The library is being called by a Silverlight app
Many thanks
Andy
{System.Runtime.Serialization.SerializationException: Type 'Renci.SshNet.SftpClient' in Assembly 'Renci.SshNet, Version=2013.4.7.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable. at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type) at System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context) at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo() at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Type objectType, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, SerializationBinder binder) at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo) at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders,
__BinaryWriter serWriter, Boolean fCheck) at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck) at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph) at Csla.Serialization.BinaryFormatterWrapper.Serialize(Stream serializationStream, Object graph) at Csla.Core.ObjectCloner.Clone(Object obj) at Csla.CommandBase
1.GetClone() at Csla.CommandBase
1.System.ICloneable.Clone() at Csla.DataPortal.Update[T](T obj) at Csla.Server.Hosts.Silverlight.SilverlightRequestProcessor.Update(SilverlightUpdateRequest request) }Csla.DataPortalException: {System.Runtime.Serialization.SerializationException: Type 'Renci.SshNet.SftpClient' in Assembly 'Renci.SshNet, Version=2013.4.7.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable. at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type) at System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context) at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo() at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Type objectType, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, SerializationBinder binder) at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo) at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Obj
ect graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck) at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck) at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph) at Csla.Serialization.BinaryFormatterWrapper.Serialize(Stream serializationStream, Object graph) at Csla.Core.ObjectCloner.Clone(Object obj) at Csla.CommandBase1.GetClone() at Csla.CommandBase
1.System.ICloneable.Clone() at Csla.DataPortal.Update[T](T obj) at Csla.Server.Hosts.Silverlight.SilverlightRequestProcessor.Update(SilverlightUpdateRequest request) }New Post: Read Line
ReadLines method doesn't seem to help here.
New Post: SftpClient.Exists always return true
The remote server is Bitvise SSH server running Windows 7.
New Post: SftpClient.Exists always return true
Commented Issue: Process terminated due to unhandled exception in Finalize / Destructor [1514]
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: Renci.SshNet.Common.SshConnectionException
Stack:
at Renci.SshNet.Session.SendMessage(Renci.SshNet.Messages.Message)
at Renci.SshNet.Channels.Channel.SendMessage(Renci.SshNet.Messages.Connection.ChannelCloseMessage)
at Renci.SshNet.Channels.Channel.Close(Boolean)
at Renci.SshNet.Channels.ChannelSession.Close(Boolean)
at Renci.SshNet.Channels.Channel.Dispose(Boolean)
at Renci.SshNet.Channels.ChannelSession.Dispose(Boolean)
at Renci.SshNet.Sftp.SubsystemSession.Dispose(Boolean)
at Renci.SshNet.Sftp.SftpSession.Dispose(Boolean)
at Renci.SshNet.Sftp.SubsystemSession.Finalize()
__This occurred just over 60 seconds _after_ the Dispose method on SftpClient was called which generated a very similar exception (handled and logged by my code):__
2013-02-16 15:41:32.9693 Error SftpFileTransfer Renci.SshNet.Common.SshConnectionException,Client not connected., at Renci.SshNet.Session.SendMessage(Message message)
at Renci.SshNet.Channels.Channel.SendMessage(ChannelCloseMessage message)
at Renci.SshNet.Channels.Channel.Close(Boolean wait)
at Renci.SshNet.Channels.ChannelSession.Close(Boolean wait)
at Renci.SshNet.Channels.Channel.Dispose(Boolean disposing)
at Renci.SshNet.Channels.ChannelSession.Dispose(Boolean disposing)
at Renci.SshNet.Sftp.SubsystemSession.Dispose(Boolean disposing)
at Renci.SshNet.Sftp.SftpSession.Dispose(Boolean disposing)
at Renci.SshNet.SftpClient.Dispose(Boolean disposing)
at Renci.SshNet.BaseClient.Dispose()
at BusinessObjects.SftpFileTransfer.DownloadFiles()
__It looks like ~SubsystemSession() is causing an attempt to send a message over the channel(!) due to the chaining of calls to the various Dispose() methods.__
Comments: ** Comment from web user: wvandoesburg **
Hi Oleg,
I also encountered this issue in version 2013.4.7.
When do you have a new release scheduled? This issues is really killing my application "literally".
Regards,
Willem
New Post: Bug in SftpClient dispose
When is a new release planned?
I was not able to compile from source directly from the svn repo using MSVS express 2012.
New Post: Bug in SftpClient dispose
I'm testing it now...
Commented Issue: Remote File exists not working [1574]
Comments: ** Comment from web user: oeriksen **
I've been looking at this and see that the Exists function was rewritten because it caused a subsequent Delete to fail on some system (file was not closed by the SSH server). The problem is that on some systems RequestRealPath doesn't throw an error. I would guess some (or all) Windows based servers, at least that the case for me. I agree on the current solution, it's much better than a file open and close, but wouldn't it be better to use RequestLStat instead of RequestRealPath? I can't imagine any systems not throwing an error if file or directory doesn't exist when executing that. Or is there any other reason not to use it?
New Post: Client KeepAliveInterval and SendKeepAlive
Such as, if I have a code block like this:
using(SshClient oClient = new SshClient(host,username,password)
{
oClient.Connect();
oClient.KeepAliveInterval = new TimeSpan(0, 0, 60);
SshCommand oCommand = oClient.CreateCommand(someCommandHere);
oClient.SendKeepAlive();
string result = oCommand.Execute();
}
oClient.KeepAliveInterval is doing what exactly? Is it telling the ssh client to send keep alive messages to the server every 60 seconds? Or is that what SendKeepAlive() does? Does SendKeepAlive() send a message once? Or does it send every 60 seconds.I'm trying to solve a connection timeout issue when transferring large files usually over 5GB in size. I've monitored the secure log on the server that the ssh.net client is connecting to, but from what Ive seen the client is the one disconnecting. Ive set the ClientAliveCountMax to 99999 on the Server to stop any disconnects but that doesnt appear to be stopping the timeouts.
I'd really appreciate a better explanation of what those 2 commands are doing so I can figure out whats happening. Thanks!