diff --git a/Renci.SshClient/Renci.SshClient/Connection.cs b/Renci.SshClient/Renci.SshClient/Connection.cs
deleted file mode 100644
index 5c6a5b2c..00000000
--- a/Renci.SshClient/Renci.SshClient/Connection.cs
+++ /dev/null
@@ -1,96 +0,0 @@
-namespace Renci.SshClient
-{
- public class Connection
- {
- private Session _session;
-
- public ConnectionInfo ConnectionInfo { get; private set; }
-
- private Shell _shell;
- public Shell Shell
- {
- get
- {
- if (this._shell == null)
- {
- this._shell = new Shell(this._session);
- }
- return this._shell;
- }
- }
-
- private Sftp _sftp;
-
- public Sftp Sftp
- {
- get
- {
- if (this._sftp == null)
- {
- this._sftp = new Sftp(this._session);
- }
- return this._sftp;
- }
- }
-
-
- public Connection(ConnectionInfo connectionInfo)
- {
- this.ConnectionInfo = connectionInfo;
- this._session = Session.CreateSession(this.ConnectionInfo);
- }
-
- public Connection(string host, int port, string username, string password)
- : this(new ConnectionInfo
- {
- Host = host,
- Port = port,
- Username = username,
- Password = password,
- })
- {
- }
-
- public Connection(string host, string username, string password)
- : this(new ConnectionInfo
- {
- Host = host,
- Username = username,
- Password = password,
- })
- {
- }
-
- public Connection(string host, int port, string username, PrivateKeyFile keyFile)
- : this(new ConnectionInfo
- {
- Host = host,
- Port = port,
- Username = username,
- KeyFile = keyFile,
- })
- {
- }
-
- public Connection(string host, string username, PrivateKeyFile keyFile)
- : this(new ConnectionInfo
- {
- Host = host,
- Username = username,
- KeyFile = keyFile,
- })
- {
- }
-
-
- public void Connect()
- {
- this._session.Connect();
- }
-
- public void Disconnect()
- {
- this._session.Disconnect();
- }
- }
-}
diff --git a/Renci.SshClient/Renci.SshClient/Renci.SshClient.csproj b/Renci.SshClient/Renci.SshClient/Renci.SshClient.csproj
index d1f4c1a1..fe443703 100644
--- a/Renci.SshClient/Renci.SshClient/Renci.SshClient.csproj
+++ b/Renci.SshClient/Renci.SshClient/Renci.SshClient.csproj
@@ -194,9 +194,9 @@
-
+
diff --git a/Renci.SshClient/Renci.SshClient/Session.cs b/Renci.SshClient/Renci.SshClient/Session.cs
index 85c75bdc..f2bc5316 100644
--- a/Renci.SshClient/Renci.SshClient/Session.cs
+++ b/Renci.SshClient/Renci.SshClient/Session.cs
@@ -143,7 +143,6 @@ namespace Renci.SshClient
this.ClientVersion = string.Format("SSH-2.0-Renci.SshClient.{0}", this.GetType().Assembly.GetName().Version);
this._sockeStream = new NetworkStream(socket);
- this.IsConnected = true;
}
private static IDictionary> _channels = new Dictionary>()
@@ -163,6 +162,12 @@ namespace Renci.SshClient
public void Connect()
{
+ // If connected dont connect again
+ if (this.IsConnected)
+ return;
+
+ this.IsConnected = true;
+
this.Write(Encoding.ASCII.GetBytes(string.Format("{0}\n", this.ClientVersion)));
// Register Transport response messages
diff --git a/Renci.SshClient/Renci.SshClient/Sftp.cs b/Renci.SshClient/Renci.SshClient/Sftp.cs
index b1827e7a..481e2401 100644
--- a/Renci.SshClient/Renci.SshClient/Sftp.cs
+++ b/Renci.SshClient/Renci.SshClient/Sftp.cs
@@ -1,70 +1,97 @@
-
-using System.Collections.Generic;
+using System.Collections.Generic;
using System.IO;
using Renci.SshClient.Channels;
using Renci.SshClient.Common;
namespace Renci.SshClient
{
- public class Sftp
+ public class Sftp : SshBase
{
- private Session _session;
-
private ChannelSftp _channel;
- internal Sftp(Session session)
+ private ChannelSftp Channel
{
- this._session = session;
-
- //this._channel = new ChannelSftp(this._session);
- this._channel = this._session.CreateChannel();
+ get
+ {
+ if (this._channel == null)
+ {
+ this.Session.Connect();
+ this._channel = this.Session.CreateChannel();
+ }
+ return this._channel;
+ }
}
+ public Sftp(ConnectionInfo connectionInfo)
+ : base(connectionInfo)
+ {
+ }
+
+ public Sftp(string host, int port, string username, string password)
+ : base(host, port, username, password)
+ {
+ }
+
+ public Sftp(string host, string username, string password)
+ : base(host, username, password)
+ {
+ }
+
+ public Sftp(string host, int port, string username, PrivateKeyFile keyFile)
+ : base(host, port, username, keyFile)
+ {
+ }
+
+ public Sftp(string host, string username, PrivateKeyFile keyFile)
+ : base(host, username, keyFile)
+ {
+ }
+
+
public IEnumerable ListDirectory(string path)
{
- return this._channel.ListDirectory(path);
+ return this.Channel.ListDirectory(path);
}
public void UploadFile(Stream source, string fileName)
{
- this._channel.UploadFile(source, fileName);
+ this.Channel.UploadFile(source, fileName);
}
public void UploadFile(string source, string fileName)
{
- this._channel.UploadFile(File.OpenRead(source), fileName);
+ this.Channel.UploadFile(File.OpenRead(source), fileName);
}
public void DownloadFile(string fileName, Stream destination)
{
- this._channel.DownloadFile(fileName, destination);
+ this.Channel.DownloadFile(fileName, destination);
}
public void DownloadFile(string fileName, string destination)
{
- var file = File.Create(destination);
- this._channel.DownloadFile(fileName, file);
+ this.Channel.DownloadFile(fileName, File.Create(destination));
}
public void RemoveFile(string fileName)
{
- this._channel.RemoveFile(fileName);
+ this.Channel.RemoveFile(fileName);
}
public void RenameFile(string oldFileName, string newFileName)
{
- this._channel.RenameFile(oldFileName, newFileName);
+ this.Channel.RenameFile(oldFileName, newFileName);
}
public void CreateDirectory(string directoryName)
{
- this._channel.CreateDirectory(directoryName);
+ this.Channel.CreateDirectory(directoryName);
}
public void RemoveDirectory(string directoryName)
{
- this._channel.RemoveDirectory(directoryName);
+ this.Channel.RemoveDirectory(directoryName);
}
}
}
diff --git a/Renci.SshClient/Renci.SshClient/Shell.cs b/Renci.SshClient/Renci.SshClient/Shell.cs
index 94db9541..0ada8627 100644
--- a/Renci.SshClient/Renci.SshClient/Shell.cs
+++ b/Renci.SshClient/Renci.SshClient/Shell.cs
@@ -5,13 +5,31 @@ using System.Text;
using Renci.SshClient.Channels;
namespace Renci.SshClient
{
- public class Shell
+ public class Shell : SshBase
{
- private Session _session;
-
- internal Shell(Session session)
+ public Shell(ConnectionInfo connectionInfo)
+ : base(connectionInfo)
+ {
+ }
+
+ public Shell(string host, int port, string username, string password)
+ : base(host, port, username, password)
+ {
+ }
+
+ public Shell(string host, string username, string password)
+ : base(host, username, password)
+ {
+ }
+
+ public Shell(string host, int port, string username, PrivateKeyFile keyFile)
+ : base(host, port, username, keyFile)
+ {
+ }
+
+ public Shell(string host, string username, PrivateKeyFile keyFile)
+ : base(host, username, keyFile)
{
- this._session = session;
}
public string Execute(string command)
@@ -30,6 +48,9 @@ namespace Renci.SshClient
public void Execute(string command, Stream output, Stream extended)
{
+ // Make sure session is connected
+ this.Session.Connect();
+
this.EndExecute(this.BeginExecute(command, output, extended, null, null));
}
@@ -40,7 +61,10 @@ namespace Renci.SshClient
public IAsyncResult BeginExecute(string command, Stream output, Stream extendedOutput, AsyncCallback callback, object state)
{
- var channel = this._session.CreateChannel();
+ // Make sure session is connected
+ this.Session.Connect();
+
+ var channel = this.Session.CreateChannel();
return channel.BeginExecute(command, output, extendedOutput, callback, state);
}
diff --git a/Renci.SshClient/Renci.SshClient/SshBase.cs b/Renci.SshClient/Renci.SshClient/SshBase.cs
new file mode 100644
index 00000000..3c3040c9
--- /dev/null
+++ b/Renci.SshClient/Renci.SshClient/SshBase.cs
@@ -0,0 +1,69 @@
+
+namespace Renci.SshClient
+{
+ public abstract class SshBase
+ {
+ public ConnectionInfo ConnectionInfo { get; private set; }
+
+ internal Session Session { get; private set; }
+
+ public SshBase(ConnectionInfo connectionInfo)
+ {
+ this.ConnectionInfo = connectionInfo;
+ this.Session = Session.CreateSession(this.ConnectionInfo);
+ }
+
+ public SshBase(string host, int port, string username, string password)
+ : this(new ConnectionInfo
+ {
+ Host = host,
+ Port = port,
+ Username = username,
+ Password = password,
+ })
+ {
+ }
+
+ public SshBase(string host, string username, string password)
+ : this(new ConnectionInfo
+ {
+ Host = host,
+ Username = username,
+ Password = password,
+ })
+ {
+ }
+
+ public SshBase(string host, int port, string username, PrivateKeyFile keyFile)
+ : this(new ConnectionInfo
+ {
+ Host = host,
+ Port = port,
+ Username = username,
+ KeyFile = keyFile,
+ })
+ {
+ }
+
+ public SshBase(string host, string username, PrivateKeyFile keyFile)
+ : this(new ConnectionInfo
+ {
+ Host = host,
+ Username = username,
+ KeyFile = keyFile,
+ })
+ {
+ }
+
+ public void Connect()
+ {
+ this.Session.Connect();
+ }
+
+ public void Disconnect()
+ {
+ this.Session.Disconnect();
+ }
+
+ }
+}