Quantcast
Channel: SSH.NET Library
Viewing all 2955 articles
Browse latest View live

New Post: How to maintain a 'conversation' with an SSH session?


New Post: How to maintain a 'conversation' with an SSH session?

$
0
0
I was just able to solve my issue, and I posted the solution on StackOverflow.

New Post: How to maintain a 'conversation' with an SSH session?

$
0
0
@egagne, I see now that PowerShell is very similar. I, too, took notes from the same emails, and came up with a more .NET-ish solution. So it wouldn't be taken offline, I posted the whole thing (sample code, output, etc.) on my blog here.

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

$
0
0
Oleg,

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

$
0
0
I am trying to use the SSH.Net library to connect to a set of computers that have only have the Arcfour (128-bit) encryption enabled on them. The computers are running the OpenSSH software, I am using Visual Basic 2012. Is there something special that I have to do in my program to tell tell the software to use the Arcfour encryption to connect to the computers in question? If I add other choices for encryption to a test computer running the same setup of the computers that I need to connect to, I am able to connect fine and can send and receive commands. It is when I revert back to only Arcfour when my connection will timeout. The option to add or replace the type of encryption on the computers I need to connect to is not something that can be done. I have to use Arcfour. Putty is able to connect to the computers when they run only the Arcfour encryption.

Thank you for any help.

New Post: Why i am not getting complete output while executing time consuming commands

$
0
0
hi

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]

$
0
0
I'm experiencing timeout exception on Disconnect method of the SftpClient connected to an OpenSSH server.
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

$
0
0
Hello,
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

$
0
0
Hi Torakas, can you explain me how to connect to sql server using the library. and tell me about port forwarding.

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

$
0
0
Hi da_rinkes,

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

$
0
0
Hi,

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.CommandBase1.GetClone() at Csla.CommandBase1.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.CommandBase1.System.ICloneable.Clone() at Csla.DataPortal.Update[T](T obj) at Csla.Server.Hosts.Silverlight.SilverlightRequestProcessor.Update(SilverlightUpdateRequest request) }

New Post: Read Line

$
0
0
Is there a way to read only the first line (or first few lines) from a remote file, instead of downloading the entire file to Memory Stream (sftp.DownloadFile)?

ReadLines method doesn't seem to help here.

New Post: SftpClient.Exists always return true

$
0
0
Even if a file does not exist SftpClient.Exists will return true. I have double checked that the file does not exist and I have even tried SftpClient.DeleteFile afterwards, which throws a SftpPathNotFoundException.

The remote server is Bitvise SSH server running Windows 7.

New Post: SftpClient.Exists always return true

$
0
0
I have done some debugging and SftpSession.RequestRealPath doesn't return null as SftpClient.Exists expects, but a array of KeyValuePair<string, SftpFileAttributes> with 1 item where SftpFileAttributes are empty/has bogus values (userid=-1, groupid=-1, size=-1 etc)

Commented Issue: Process terminated due to unhandled exception in Finalize / Destructor [1514]

$
0
0
__My application encountered the following unhandled exception that caused the process to exit:__

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

$
0
0
I have also observed this issue using version 2013.4.7

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

$
0
0
Ok, I wasn't able to build the entire solution but could build the Renci.sshnet.dll

I'm testing it now...

Commented Issue: Remote File exists not working [1574]

$
0
0
I've tried to use sftp.Exists(String) do not work for files. Already tested for file and for directory. Directory work well, but files are not recognized.
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

$
0
0
I was hoping someone could give me an example of how to use these 2 commands and exactly what they mean? I'm relatively new to SSH but have a decent understanding... I believe.

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!

New Post: Renci.SshNet.Common.SshOperationTimeoutException

$
0
0
Any resolution to this? I am running into the exact same issue with code that looks almost identical. Any insight you have would be appreciated. Thanks!
Viewing all 2955 articles
Browse latest View live


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