Ejemplo n.º 1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SecureFileTransferClient"/> class.
 /// </summary>
 /// <param name="settings">The settings.</param>
 /// <param name="remoteHost">The remote host.</param>
 /// <param name="userName">Name of the user.</param>
 public SecureFileTransferClient(SecureCopySettings settings, string remoteHost, string userName)
 {
     if (settings == null)
         throw new ArgumentNullException("settings");
     if (string.IsNullOrEmpty(remoteHost))
         throw new ArgumentNullException("remoteHost");
     SecureCopySettings = (SecureCopySettings)settings.Clone();
     SecureCopySettings.Options = SecureCopySettingsOptions.ForceSftp;
     Credentials = new NetworkCredential(userName, (string)null);
     RemoteHost = remoteHost;
 }
Ejemplo n.º 2
0
        public static bool TryGet(SecureCopySettings settings, string remoteHost, string userName, string remoteFile, string localFile, out Exception ex)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }
            if (string.IsNullOrEmpty(remoteHost))
            {
                throw new ArgumentNullException("remoteHost");
            }
            if (string.IsNullOrEmpty(remoteFile))
            {
                throw new ArgumentNullException("remoteFile");
            }
            if (string.IsNullOrEmpty(localFile))
            {
                throw new ArgumentNullException("localFile");
            }
            //
            if (File.Exists(localFile))
            {
                File.Delete(localFile);
            }
            if (!string.IsNullOrEmpty(userName))
            {
                remoteHost = userName + "@" + remoteHost;
            }
            string executablePath;
            var    arguments = string.Format(GetArgumentsXABC, Get(settings, out executablePath), remoteHost, remoteFile, localFile);

            try
            {
                var launcher = new ProcessLauncher();
                launcher.Launch(settings.ProcessTimeoutInMilliseconds, executablePath, arguments, (w) =>
                {
                    // Respond N
                    w.WriteLine("n");
                    w.Flush();
                });
                ex = null;
                return(true);
            }
            catch (SecureCopyException e) { ex = e; return(false); }
        }
Ejemplo n.º 3
0
        //public static bool TryList<TItem>(SecureCopySettings settings, string remoteHost, string userId, string fileSpecification, out IEnumerable<TItem> items, out Exception ex)
        //{
        //    string itemsAsText;
        //    if (TryList(settings, remoteHost, userId, fileSpecification, out itemsAsText, out ex))
        //    {
        //        items = null;
        //        return true;
        //    };
        //    items = null;
        //    return false;
        //}
        public static bool TryList(SecureCopySettings settings, string remoteHost, string userName, string fileSpecification, out string items, out Exception ex)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }
            if (string.IsNullOrEmpty(remoteHost))
            {
                throw new ArgumentNullException("remoteHost");
            }
            if (string.IsNullOrEmpty(fileSpecification))
            {
                throw new ArgumentNullException("fileSpecification");
            }
            //
            if (!string.IsNullOrEmpty(userName))
            {
                remoteHost = userName + "@" + remoteHost;
            }
            string executablePath;
            var    arguments = string.Format(ListArgumentsXAB, Get(settings, out executablePath), remoteHost, fileSpecification);

            try
            {
                var launcher = new ProcessLauncher();
                launcher.Launch(settings.ProcessTimeoutInMilliseconds, executablePath, arguments, (w) =>
                {
                    // Respond N
                    w.WriteLine("n");
                    w.Flush();
                });
                items = launcher.OutputString;
                ex    = null;
                return(true);
            }
            catch (SecureCopyException e) { items = null; ex = e; return(false); }
        }
Ejemplo n.º 4
0
 private static string Get(SecureCopySettings settings, out string executablePath)
 {
     var options = settings.Options;
     var bits = (SecureCopySettingsOptions.ForceSshProtocol1 | SecureCopySettingsOptions.ForceSshProtocol2);
     if ((options & bits) == bits)
         throw new ArgumentException("settings", "ForceSshProtocol1 and ForceSshProtocol2 are mutualy exclusive");
     bits = (SecureCopySettingsOptions.ForceIPv4 | SecureCopySettingsOptions.ForceIPv6);
     if ((options & bits) == bits)
         throw new ArgumentException("settings", "ForceIPv4 and ForceIPv6 are mutualy exclusive");
     // build executablePath
     executablePath = EnsureEndsWith(settings.PuTtyPath, "\\") + "pscp.exe";
     if (!File.Exists(executablePath))
         throw new InvalidOperationException(string.Format("'pscp.exe' not found at '{0}'.", executablePath));
     // build arguments
     var b = new StringBuilder();
     if ((options & SecureCopySettingsOptions.PreserveFileAttributes) == SecureCopySettingsOptions.PreserveFileAttributes)
         b.Append("-p ");
     if ((options & SecureCopySettingsOptions.Recursively) == SecureCopySettingsOptions.Recursively)
         b.Append("-r ");
     if (!string.IsNullOrEmpty(settings.SessionName))
         b.AppendFormat("-load \"{0}\" ", settings.SessionName);
     if (settings.Port.HasValue)
         b.AppendFormat("-P {0} ", settings.Port);
     if (!string.IsNullOrEmpty(settings.UserName))
         b.AppendFormat("-l \"{0}\" ", settings.UserName);
     if (!string.IsNullOrEmpty(settings.Password))
         b.AppendFormat("-pw \"{0}\" ", settings.Password);
     if ((options & SecureCopySettingsOptions.ForceSshProtocol1) == SecureCopySettingsOptions.ForceSshProtocol1)
         b.Append("-1 ");
     if ((options & SecureCopySettingsOptions.ForceSshProtocol2) == SecureCopySettingsOptions.ForceSshProtocol2)
         b.Append("-2 ");
     if ((options & SecureCopySettingsOptions.ForceIPv4) == SecureCopySettingsOptions.ForceIPv4)
         b.Append("-4 ");
     if ((options & SecureCopySettingsOptions.ForceIPv6) == SecureCopySettingsOptions.ForceIPv6)
         b.Append("-6 ");
     if ((options & SecureCopySettingsOptions.EnableCompression) == SecureCopySettingsOptions.EnableCompression)
         b.Append("-C ");
     if (!string.IsNullOrEmpty(settings.PrivateKeyPath))
         b.AppendFormat("-i \"{0}\" ", settings.PrivateKeyPath);
     if ((options & SecureCopySettingsOptions.DisableAgent) == SecureCopySettingsOptions.DisableAgent)
         b.Append("-noagent ");
     if ((options & SecureCopySettingsOptions.EnableAgent) == SecureCopySettingsOptions.EnableAgent)
         b.Append("-agent ");
     if ((options & SecureCopySettingsOptions.DisableInteractivePrompts) == SecureCopySettingsOptions.DisableInteractivePrompts)
         b.Append("-batch ");
     if ((options & SecureCopySettingsOptions.Unsafe) == SecureCopySettingsOptions.Unsafe)
         b.Append("-unsafe ");
     if ((options & SecureCopySettingsOptions.ForceSftp) == SecureCopySettingsOptions.ForceSftp)
         b.Append("-sftp ");
     if ((options & SecureCopySettingsOptions.ForceScp) == SecureCopySettingsOptions.ForceScp)
         b.Append("-scp ");
     if (b.Length > 0)
         b.Length--;
     return b.ToString();
 }
Ejemplo n.º 5
0
 //public static bool TryList<TItem>(SecureCopySettings settings, string remoteHost, string userId, string fileSpecification, out IEnumerable<TItem> items, out Exception ex)
 //{
 //    string itemsAsText;
 //    if (TryList(settings, remoteHost, userId, fileSpecification, out itemsAsText, out ex))
 //    {
 //        items = null;
 //        return true;
 //    };
 //    items = null;
 //    return false;
 //}
 /// <summary>
 /// Tries the list.
 /// </summary>
 /// <param name="settings">The settings.</param>
 /// <param name="remoteHost">The remote host.</param>
 /// <param name="userName">Name of the user.</param>
 /// <param name="fileSpecification">The file specification.</param>
 /// <param name="items">The items.</param>
 /// <param name="ex">The ex.</param>
 /// <returns></returns>
 public static bool TryList(SecureCopySettings settings, string remoteHost, string userName, string fileSpecification, out string items, out Exception ex)
 {
     if (settings == null)
         throw new ArgumentNullException("settings");
     if (string.IsNullOrEmpty(remoteHost))
         throw new ArgumentNullException("remoteHost");
     if (string.IsNullOrEmpty(fileSpecification))
         throw new ArgumentNullException("fileSpecification");
     //
     if (!string.IsNullOrEmpty(userName))
         remoteHost = userName + "@" + remoteHost;
     string executablePath;
     var arguments = string.Format(ListArgumentsXAB, Get(settings, out executablePath), remoteHost, fileSpecification);
     try
     {
         var launcher = new ProcessLauncher();
         launcher.Launch(settings.ProcessTimeoutInMilliseconds, executablePath, arguments, (w) =>
         {
             // Respond N
             w.WriteLine("n");
             w.Flush();
         });
         items = launcher.OutputString;
         ex = null;
         return true;
     }
     catch (SecureCopyException e) { items = null; ex = e; return false; }
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Tries the put.
 /// </summary>
 /// <param name="settings">The settings.</param>
 /// <param name="remoteHost">The remote host.</param>
 /// <param name="userName">Name of the user.</param>
 /// <param name="localFiles">The local files.</param>
 /// <param name="remoteFile">The remote file.</param>
 /// <param name="ex">The ex.</param>
 /// <returns></returns>
 public static bool TryPut(SecureCopySettings settings, string remoteHost, string userName, string[] localFiles, string remoteFile, out Exception ex)
 {
     if (settings == null)
         throw new ArgumentNullException("settings");
     if (string.IsNullOrEmpty(remoteHost))
         throw new ArgumentNullException("remoteHost");
     if ((localFiles == null) || (localFiles.Length == 0))
         throw new ArgumentNullException("localFiles");
     if (string.IsNullOrEmpty(remoteFile))
         throw new ArgumentNullException("remoteFile");
     //
     if (!string.IsNullOrEmpty(userName))
         remoteHost = userName + "@" + remoteHost;
     string executablePath;
     var arguments = string.Format(PutArgumentsXABC, Get(settings, out executablePath), string.Join(" ", localFiles), remoteHost, remoteFile);
     try
     {
         var launcher = new ProcessLauncher();
         launcher.Launch(settings.ProcessPutTimeoutInMilliseconds, executablePath, arguments, (w) =>
         {
             // Respond N
             w.WriteLine("n");
             w.Flush();
         });
         ex = null;
         return true;
     }
     catch (SecureCopyException e) { ex = e; return false; }
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Tries the put.
 /// </summary>
 /// <param name="settings">The settings.</param>
 /// <param name="remoteHost">The remote host.</param>
 /// <param name="userName">Name of the user.</param>
 /// <param name="localFiles">The local files.</param>
 /// <param name="remoteFile">The remote file.</param>
 /// <param name="ex">The ex.</param>
 /// <returns></returns>
 public static bool TryPut(SecureCopySettings settings, string remoteHost, string userName, string localFiles, string remoteFile, out Exception ex) { return TryPut(settings, remoteHost, userName, new[] { localFiles }, remoteFile, out ex); }
Ejemplo n.º 8
0
        private static string Get(SecureCopySettings settings, out string executablePath)
        {
            var options = settings.Options;
            var bits    = (SecureCopySettingsOptions.ForceSshProtocol1 | SecureCopySettingsOptions.ForceSshProtocol2);

            if ((options & bits) == bits)
            {
                throw new ArgumentException("settings", "ForceSshProtocol1 and ForceSshProtocol2 are mutualy exclusive");
            }
            bits = (SecureCopySettingsOptions.ForceIPv4 | SecureCopySettingsOptions.ForceIPv6);
            if ((options & bits) == bits)
            {
                throw new ArgumentException("settings", "ForceIPv4 and ForceIPv6 are mutualy exclusive");
            }
            // build executablePath
            executablePath = EnsureEndsWith(settings.PuTtyPath, "\\") + "pscp.exe";
            if (!File.Exists(executablePath))
            {
                throw new InvalidOperationException(string.Format("'pscp.exe' not found at '{0}'.", executablePath));
            }
            // build arguments
            var b = new StringBuilder();

            if ((options & SecureCopySettingsOptions.PreserveFileAttributes) == SecureCopySettingsOptions.PreserveFileAttributes)
            {
                b.Append("-p ");
            }
            if ((options & SecureCopySettingsOptions.Recursively) == SecureCopySettingsOptions.Recursively)
            {
                b.Append("-r ");
            }
            if (!string.IsNullOrEmpty(settings.SessionName))
            {
                b.AppendFormat("-load \"{0}\" ", settings.SessionName);
            }
            if (settings.Port.HasValue)
            {
                b.AppendFormat("-P {0} ", settings.Port);
            }
            if (!string.IsNullOrEmpty(settings.UserName))
            {
                b.AppendFormat("-l \"{0}\" ", settings.UserName);
            }
            if (!string.IsNullOrEmpty(settings.Password))
            {
                b.AppendFormat("-pw \"{0}\" ", settings.Password);
            }
            if ((options & SecureCopySettingsOptions.ForceSshProtocol1) == SecureCopySettingsOptions.ForceSshProtocol1)
            {
                b.Append("-1 ");
            }
            if ((options & SecureCopySettingsOptions.ForceSshProtocol2) == SecureCopySettingsOptions.ForceSshProtocol2)
            {
                b.Append("-2 ");
            }
            if ((options & SecureCopySettingsOptions.ForceIPv4) == SecureCopySettingsOptions.ForceIPv4)
            {
                b.Append("-4 ");
            }
            if ((options & SecureCopySettingsOptions.ForceIPv6) == SecureCopySettingsOptions.ForceIPv6)
            {
                b.Append("-6 ");
            }
            if ((options & SecureCopySettingsOptions.EnableCompression) == SecureCopySettingsOptions.EnableCompression)
            {
                b.Append("-C ");
            }
            if (!string.IsNullOrEmpty(settings.PrivateKeyPath))
            {
                b.AppendFormat("-i \"{0}\" ", settings.PrivateKeyPath);
            }
            if ((options & SecureCopySettingsOptions.DisableAgent) == SecureCopySettingsOptions.DisableAgent)
            {
                b.Append("-noagent ");
            }
            if ((options & SecureCopySettingsOptions.EnableAgent) == SecureCopySettingsOptions.EnableAgent)
            {
                b.Append("-agent ");
            }
            if ((options & SecureCopySettingsOptions.DisableInteractivePrompts) == SecureCopySettingsOptions.DisableInteractivePrompts)
            {
                b.Append("-batch ");
            }
            if ((options & SecureCopySettingsOptions.Unsafe) == SecureCopySettingsOptions.Unsafe)
            {
                b.Append("-unsafe ");
            }
            if ((options & SecureCopySettingsOptions.ForceSftp) == SecureCopySettingsOptions.ForceSftp)
            {
                b.Append("-sftp ");
            }
            if ((options & SecureCopySettingsOptions.ForceScp) == SecureCopySettingsOptions.ForceScp)
            {
                b.Append("-scp ");
            }
            if (b.Length > 0)
            {
                b.Length--;
            }
            return(b.ToString());
        }
Ejemplo n.º 9
0
 public static bool TryPut(SecureCopySettings settings, string remoteHost, string userName, string localFiles, string remoteFile, out Exception ex)
 {
     return(TryPut(settings, remoteHost, userName, new[] { localFiles }, remoteFile, out ex));
 }