using System;
using System.Collections;
using SBSSHCommon;
using SBSSHKeyStorage;
using SBSimpleSftp;
using SBSftpCommon;
namespace ConsoleSftp
{
/// <summary>
/// Summary description for ConsoleSftpClient.
/// </summary>
class ConsoleSftpClient
{
static SBSimpleSftp.TElSimpleSFTPClient Client = null;
// command line parameters
const int OP_LIST = 1;
const int OP_DOWNLOAD = 2;
const int OP_UPLOAD = 3;
static long parOperation = 0;
static string parRemoteName = "/";
static string parLocalName;
static string parUsername;
static string parPassword;
static string parAddress;
static int parPort;
static bool ParseCommandLine(string[] args)
{
int i = 0;
string arg;
if (args.Length == 0)
{
System.Console.WriteLine("usage:");
System.Console.WriteLine("SFTPClient {-download|-upload} -addr[ess] <address> [-port <port>] -user[name] <user> -pass[word] <password> -remote <remote file name> -local <local file name>");
return false;
}
else
{
while (i < args.Length)
{
arg = args[i];
if ((String.Compare(arg, "-down", true) == 0) ||
(String.Compare(arg, "-download", true) == 0))
{
parOperation = OP_DOWNLOAD;
}
else
if ((String.Compare(arg, "-up", true) == 0) ||
(String.Compare(arg, "-upload", true) == 0))
{
parOperation = OP_UPLOAD;
}
else
if ((String.Compare(arg, "-list", true) == 0) ||
(String.Compare(arg, "-ls", true) == 0))
{
parOperation = OP_LIST;
}
else
if ((String.Compare(arg, "-addr", true) == 0) ||
(String.Compare(arg, "-address", true) == 0))
{
i++;
parAddress = args[i];
}
else
if (String.Compare(arg, "-port", true) == 0)
{
i++;
try
{
parPort = Int32.Parse(args[i]);
}
catch (Exception)
{
parPort = 22;
}
}
else
if ((String.Compare(arg, "-user", true) == 0) ||
(String.Compare(arg, "-username", true) == 0))
{
i++;
parUsername = args[i];
}
else
if ((String.Compare(arg, "-pass", true) == 0) ||
(String.Compare(arg, "-password", true) == 0))
{
i++;
parPassword = args[i];
}
else
if (String.Compare(arg, "-local", true) == 0)
{
i++;
parLocalName = args[i];
}
else
if (String.Compare(arg, "-remote", true) == 0)
{
i++;
parRemoteName = args[i];
}
i = i + 1;
}
return (parOperation != 0);
}
}
static void Download()
{
Client.DownloadFile(parRemoteName, parLocalName, TSBSFTPFileTransferMode.ftmOverwrite);
}
static void Upload()
{
Client.UploadFile(parLocalName, parRemoteName, TSBSFTPFileTransferMode.ftmOverwrite);
// now adjust the attributes of the uploaded file
TElSftpFileAttributes Attrs
= new TElSftpFileAttributes
();
Attrs.CTime = DateTime.Now;
Attrs.MTime = Attrs.CTime;
Attrs.ATime = Attrs.CTime;
Attrs.CATime = Attrs.CTime;
Attrs.IncludedAttributes = SBSftpCommon.__Global.saATime | SBSftpCommon.__Global.saMTime | SBSftpCommon.__Global.saCTime | SBSftpCommon.__Global.saCATime;
Client.SetAttributes(parRemoteName, Attrs);
}
static void List()
{
byte[] Handle;
ArrayList AListing
= new ArrayList
();
Handle = Client.OpenDirectory(parRemoteName);
Client.ReadDirectory(Handle, AListing);
if (AListing.Count > 0)
{
System.Console.WriteLine("Contents of " + parRemoteName + ":");
for (int i = 0; i < AListing.Count; i++)
{
System.Console.WriteLine(((TElSftpFileInfo)AListing[i]).Name);
}
}
else
{
System.Console.WriteLine("The specified directory is empty");
}
Client.CloseHandle(Handle);
}
static private void Client_OnKeyValidate(object Sender, TElSSHKey ServerKey, ref bool Validate)
{
System.Console.WriteLine("Server key received");
Validate = true; // NEVER do this. You MUST check the key validity somehow
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
SBUtils.Unit.SetLicenseKey("Your license key here");
if (!ParseCommandLine(args))
return;
Client
= new TElSimpleSFTPClient
();
Client.Address = parAddress;
Client.Port = parPort;
Client.Username = parUsername;
Client.Password = parPassword;
Client
.OnKeyValidate += new TSSHKeyValidateEvent
(Client_OnKeyValidate
);
try
{
try
{
Client.Open();
}
catch (Exception e)
{
System.Console.WriteLine("Connection failed due to exception: " + e.Message);
System.Console.WriteLine("If you have ensured that all connection parameters are correct and you still can't connect,");
System.Console.WriteLine("please contact EldoS support as described on http://www.eldos.com/sbb/support-tech.php");
System.Console.WriteLine("Remember to provide details about the error that happened.");
if (Client.ServerSoftwareName.Length > 0)
{
System.Console.WriteLine("Server software identified itself as: " + Client.ServerSoftwareName);
}
try
{
Client.Close(true);
}
catch
{
}
return;
}
try
{
switch (parOperation)
{
case OP_DOWNLOAD:
Download();
break;
case OP_UPLOAD:
Upload();
break;
case OP_LIST:
List();
break;
}
}
finally
{
Client.Close(false);
}
}
catch (Exception e)
{
System.Console.WriteLine("Exception: " + e.Message);
}
}
}
}