New Post: Using ShellStream and readline gives me double prompts
New Post: Connecting via sshclient
Created Unassigned: download xlsx stream using renci [1880]
Commented Unassigned: download xlsx stream using renci [1880]
Comments: ** Comment from web user: beula **
sftpClient.DownloadFile(filePath, Stream);
byte[] decryptedArray = decryptedStream.ToArray();
var table = (Encoding.Default.GetString(decryptedArray, 0, decryptedArray.Length - 1)).Split(new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
which returns encrypted text
Commented Unassigned: Closing forwarded ports does not work [1867]
I use PuTTy with the following settings for port forwarding which works fine:
putty.exe -ssh user@hostname -L 5555:localhost:5555 -L 5554:localhost:5554 -N -pw password
Now I want to do this from C# and therefore I'd like to use the SSH.NET library. So I use the following to forward the ports:
```
public void ForwardPortLocally(uint portNumber)
{
try
{
var p = new ForwardedPortLocal("localhost", portNumber, "localhost", portNumber);
client.AddForwardedPort(p);
p.Start();
}
catch (Exception ex)
{
throw new Exception(String.Format("ERROR: Port forwarding failed: {0}", ex.Message));
}
}
```
I know I need to clean up after my self an therefore I use the following code:
```
public void Dispose()
{
var toDispose = new List<ForwardedPortLocal>();
foreach (var forwardedPort in client.ForwardedPorts.Where(forwardedPort => forwardedPort.IsStarted))
{
forwardedPort.Stop();
toDispose.Add((ForwardedPortLocal)forwardedPort);
}
foreach (var port in toDispose)
{
client.RemoveForwardedPort(port);
port.Dispose();
}
if (client.IsConnected)
{
client.Disconnect();
}
client.Dispose();
}
```
This is where the problem begins. I am not able to close my ports properly. While setting up the forwarded ports is working fine, the attempt to close it always fails.
Once I have setup the forwarded ports and closed the application I am not able to open them anymore because they are still in use and I get the exception:
> "Only one usage of each socket address (protocol/network address/port) is normally permitted"
I have used sysinternals' tcpview to see whether the ports stay open and they do but the process is "non-existent". I have to logg off and on again from Windows to be able to re-open the port.
Am I doing something wrong? Have I found a bug in the library?
Regards
P.S.: I also experienced that the method i use to get rid of the forwarded ports takes seconds to run which is very long and seems a bit odd to me.
Comments: ** Comment from web user: e5490626 **
Hi,
I have the same problem with Forwadingports. The first opening port forwadin run properly. When disconnect and try to forwading with a new SSH server, the new one doesn't run, because the old connection is still "open".
I do the following:
1.- Open
```
Imports Renci.SshNet
Dim client As SshClient
Dim port As ForwardedPortLocal
If (contador.Use_server_ssh And RadioButton1Remoto.Checked) Then
ToolStripStatusLabel1.Text = "SQL Server string conection..."
CadenaDEconexion = "data source =127.0.0.1,14444; xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
ToolStripStatusLabel1.Text = "Connecting SSH..."
SSHConect(client, port, contador.ssh_server_ip, contador.ssh_server_port, contador.ssh_DDBB_Ipforwading)
ToolStripStatusLabel1.Text = "Conexion SSH OK..."
End If
```
Where:
contador.ssh_server_ip= ip remota del servidor SSH (por ejemplo 94.168.1.23)
contador.ssh_server_port = puerto del servidor SSH 1443
contador.ssh_DDBB_Ipforwading = ip local del equipo remoto 192.168.1.3
and
```
Public Function SSHConect(ByRef _client As SshClient, ByRef _port As ForwardedPortLocal, _SSHIPserver As String, _SSHPortServer As Integer, _SSHForwardIP As String, _SSHForwardPort As Integer) As Boolean
If (Not IsNothing(_client)) Then
If (_client.IsConnected) Then
'_port.Stop()
'_client.RemoveForwardedPort(_port)
_client.Disconnect()
End If
End If
SSHConect = False
_client = New SshClient(_SSHIPserver, _SSHPortServer, "userSSH", "passSSH")
_client.KeepAliveInterval = New TimeSpan(0, 0, 30)
_client.Connect()
_client.SendKeepAlive()
If _client.IsConnected Then
Try
_port = New ForwardedPortLocal("127.0.0.1", _SSHForwardPort, _SSHForwardIP, _SSHForwardPort)
_client.AddForwardedPort(_port)
_port.Start()
If (_port.IsStarted) Then
SSHConect = True
Else
SSHConect = False
End If
Catch ex As Exception
MessageBox.Show(ex.ToString())
SSHConect = False
End Try
End If
End Function
```
2 .- Just here, all run OK, I can connect to remote database and read data.
3 .- Now, I try to disconnect and delete the forwading rule:
```
If (Not IsNothing(client)) Then SSHDisconnect(client, port)
Donde SSHDisconnect :
Public Sub SSHDisconnect(ByRef _client As SshClient, ByRef _port As ForwardedPortLocal)
_port.Stop()
_client.Disconnect()
_client.RemoveForwardedPort(_port)
_port.Dispose()
_client.Dispose()
End Sub
```
Now, if I try to forwading to a new SSH server, seems like the socket is in an unknow state. The connection with the new rule return successful, but really open the forwading with the previous address, really I think the old rule is all time running, because if the old server SSH and the new rule are over the same address run properly.
Some idea?
Commented Unassigned: Connection through proxy [1869]
I have an issue through a Symantec Web Gateway proxy server HTTP . The connection is made through to the proxy server but the authentication isn't passed and the connection fails. Is there something I can do to allow this, like a setting. Or is this just an inherent issue with the Proxy? Has anyone else had this experience?
Thanks
Comments: ** Comment from web user: xboixed **
Check the issue [1601](https://sshnet.codeplex.com/workitem/1601) and its resolution.
New Post: An established connection was aborted
In my code when I use KeepAliveInterval connection loss occurs if the application is finished with a signature of exception "An established connection was aborted by the software on the host computer" on SendKeepAlive.
Me.StatusTextBoxSSH.AppendText("Conexão SSH Estabelecida..." & Chr(13) & Chr(10))
Me.StatusTextBoxSSH.AppendText("Iniciando Tunelamento SSH..." & Chr(13) & Chr(10))
Me.Refresh()
f_intRandonPort = f_rdnRandonLoud.Next(30000, 60000) ' Gera Porta do servidor randômica
f_strTunnelRemotoServer = CStr(f_intRandonPort)
f_sshPortForwardRemote = New SshNet.ForwardedPortRemote(g_clsIni.Loopback, CUInt(f_strTunnelRemotoServer), g_clsIni.Loopback, CUInt(f_strTunnelRemoto))
f_sshTunnel.AddForwardedPort(f_sshPortForwardRemote)
f_sshPortForwardRemote.Start()
f_sshTunnel.SendKeepAlive()
If f_sshPortForwardRemote.IsStarted Then
Me.StatusTextBoxSSH.AppendText("Tunelamento SSH Iniciado" & Chr(13) & Chr(10) & _
"Porta do Tunel " & f_intRandonPort & Chr(13) & Chr(10) & _
"Portal local " & f_strTunnelRemoto & Chr(13) & Chr(10))
Me.Refresh()
Else
Debugging the source code of the library and my code together Renci exception occurs in BaseClient.cs. If the connection is lost with the still active session an error occurs during the execution of SendKeepAlive./// <summary>
/// Sends keep-alive message to the server.
/// </summary>
public void SendKeepAlive()
{
try
{
if (this.Session == null)
return;
if (!this.Session.IsConnected)
return;
this.Session.SendKeepAlive();
}
}Marcelo
Created Unassigned: SSHNet and Visual basic- login [1882]
The problem I'm currently running into is the actual login part of this. here's some examples of what I've got going on.
The Interface: http://puu.sh/6FQ5J.png
```
Public Class ggsmain
Public Sub login_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles login.Click
'Create the objects needed to make the connection
Dim connInfo As New Renci.SshNet.PasswordConnectionInfo(hostname.Text.ToString, usrname.Text.ToString, password.Text.ToString)
Dim sshClient As New Renci.SshNet.SshClient(connInfo)
Try
Using sshClient
'connect to the server
sshClient.Connect()
End Using
Catch ex As Exception
cmds.Visible = True
End Try
End Sub
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim connInfo As New Renci.SshNet.PasswordConnectionInfo(hostname.Text.ToString, usrname.Text.ToString, password.Text.ToString)
Dim sshClient As New Renci.SshNet.SshClient(connInfo)
Using sshClient
Dim cmd As Renci.SshNet.SshCommand
cmd = sshClient.RunCommand("htop")
output.Text = cmd.Result
End Using
End Sub
End Class
```
and finally, the error output:
With port appended to hostname:
```
A first chance exception of type 'System.ArgumentException' occurred in Renci.SshNet.dll
A first chance exception of type 'Renci.SshNet.Common.SshConnectionException' occurred in Renci.SshNet.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'Renci.SshNet.Common.SshConnectionException' occurred in Renci.SshNet.dll
A first chance exception of type 'System.ArgumentException' occurred in Renci.SshNet.dll
A first chance exception of type 'System.ArgumentException' occurred in Renci.SshNet.dll
(the more detailed error output) http://puu.sh/6FQbm.png
```
and without the port appended:
```
http://puu.sh/6FQfR.png(it doesn't show the output window for some reason, like the other)
```
Note that I can connect to the host with the information I used in my app in PuTTY and send commands normally.
Considering my at best intermediate level of proficiency in VB, I should probably not be attempting this, however I learn fast, and am Self taught in VB- never took classes for it. I literally installed it and played around with it until I was able to build multimedia applications(music player using wmp object and playlist support)
I feel like if I can get a little insight with this issue, I would be able to pick it up. Once I get the application to connect, and hold said connection open, I can then begin to add more commands to send to ssh host, and print them to the big black box(label) on the form
Thanks for your time, and I hope someone is able to help me. I'll be happy to provide more information if necessary
Commented Unassigned: lsnrctl: command not found issue [1874]
I am facing issue while trying to run a command on a remote server using renci.dll
The error is as below
bash: lsnrctl: command not found
and command used as -
lsnrctl status | egrep -i 'security'
Whereas if i run the same command through ssh (i.e remotely connecting through putty) , I am able to get the command output.
thanks
Comments: ** Comment from web user: gege21 **
Hi,
hope it helps [https://sshnet.codeplex.com/discussions/474366
](https://sshnet.codeplex.com/discussions/474366)
New Post: PortForwading Bug
I detected a problem when using PortForwading.
The first time, the tunneling is working properly, but after closing, the forwading rule is still working,
' RENCI
Dim client As SshClient
Dim port As ForwardedPortLocal
Public Sub SSHDisconnect(ByRef _client As SshClient, ByRef _port As ForwardedPortLocal)
_port.Stop()
_client.Disconnect()
_client.RemoveForwardedPort(_port)
End Sub
I use this sequence for open: Public Function SSHConect(ByRef _client As SshClient, ByRef _port As ForwardedPortLocal, _SSHIPserver As String, _SSHPortServer As Integer, _SSHForwardIP As String, _SSHForwardPort As Integer) As Boolean
If (Not IsNothing(_client)) Then
If (_client.IsConnected) Then
'_port.Stop()
'_client.RemoveForwardedPort(_port)
_client.Disconnect()
End If
End If
SSHConect = False
_client = New SshClient(_SSHIPserver, _SSHPortServer, "userssh", "1111aA")
_client.KeepAliveInterval = New TimeSpan(0, 0, 30)
_client.Connect()
_client.SendKeepAlive()
If _client.IsConnected Then
Try
_port = New ForwardedPortLocal("127.0.0.1", _SSHForwardPort, _SSHForwardIP, _SSHForwardPort)
_client.AddForwardedPort(_port)
_port.Start()
If (_port.IsStarted) Then
SSHConect = True
Else
SSHConect = False
End If
Catch ex As Exception
MessageBox.Show(ex.ToString())
SSHConect = False
End Try
End If
End Function
When connection is established first time is possible to SELECT from SQLServer database without problems. _port = New ForwardedPortLocal("127.0.0.1", 14444, _SSHForwardIP, 1433)
and _port.IsStarted is True
After close connection and open to another SSH server, the first portforwading rule is still running, and the next rule doesn't works.
This is the state for second time with connecting to a new ssh server ip, connected state is true.

Any idea how to repair??
New Post: PortForwading Bug
Local Port Forwardings open and close properly.
Your Pics are too small to see anything.
New Comment on "Documentation"
New Post: SUDO with ScpClient?
Created Unassigned: Incorrect handling of SocketException in Session.ReadSocket() [1885]
```
catch (SocketException exp)
{
if (exp.SocketErrorCode == SocketError.ConnectionAborted)
{
buffer = new byte[length];
this.Disconnect();
return;
}
... // Code removed for brevity
else
throw; // any serious error occurred
}
```
It would seem that the code tries to figure out if "our side" did the disconnect, and then it would send a disconnect message to server, and be done. The problem is that this exception also signals if all sides of the socket is closed. If you use TcpView (sys internals tool) to forcibly close the connection, Disconnect() will be called, and the thread terminates, leaving pending operations such as RunCommand() hanging.
My fix solves *my* particular problem, but I am not sure what impact it has on the rest of the software. This is what I added:
```
catch (SocketException exp)
{
if (this._socket.Connected && // Check if we're still connected
exp.SocketErrorCode == SocketError.ConnectionAborted)
{
buffer = new byte[length];
this.Disconnect();
return;
}
else if (exp.SocketErrorCode == SocketError.WouldBlock ||
exp.SocketErrorCode == SocketError.IOPending ||
exp.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
{
// socket buffer is probably empty, wait and try again
Thread.Sleep(30);
}
else
throw; // any serious error occurred
}
```
The logic is that "If we're not connected anymore via the socket, then something bad happened, and we want outstanding requests to know". The thread of execution will reach the rethrow, which in turn will activate the RaiseError() method further up the stack.
New Post: PortForwading Bug
yes, really small.....I upload the pictures to another site.
http://subefotos.com/ver/?013a03f6bd067f31fe7632f954b9bb45o.jpg
https://drive.google.com/file/d/0BzK7a0IKeyr4dHdXUEpRYm8yV2M/edit?usp=sharing

Could be a problem with init sequence, because it create a new instance each time in the SSHConnect function?¿
_client = New SshClient(_SSHIPserver, _SSHPortServer, "userssh", "11111111")
I will try to collect more data
New Post: Use SSH.NET for regular FTP
i tried, but it doesn't seem to work.
i set the port to 21 instead of 22, but it just times out.
New Post: SSH.net server string is null by sftp.connect()
First of all I want to say that so far I have no experience with the ssh.net Framework.
I want to connect to the server but the following problem (server string is null at line sftp.Connect();) occured and I'm not able to solve it.
Maybe someone can tell me what it could be or what i should check.
Thanks in advance
using (var sftp = new SftpClient("127.0.0.1", 990, "admin", "admin"))
{
string wd=sftp.WorkingDirectory;
string ci = sftp.ConnectionInfo.ToString();
if(!sftp.IsConnected)
sftp.Connect();
string uploadedFileName = @"C:\Users\gat\Desktop\test\MyFile.txt";
string remoteFileName = "test2.txt";
using (var file = File.OpenRead(uploadedFileName))
{
sftp.UploadFile(file, remoteFileName);
}
sftp.Disconnect();
}
}
New Post: SSH.net server string is null by sftp.connect()
SFTP => FTP over SSH
FTPS => FTP with SSL/TLS
FTPS is not supported by this library, since it has nothing to do with SSH.