public String Format(IHostPwEntry hostPwEntry)
        {
            PuttyOptions options = null;
            bool         success = PuttyOptionsParser.TryParse(hostPwEntry.AdditionalOptions, out options);

            StringBuilder sb = new StringBuilder(String.Format("\"{0}\" scp://{1}", ExecutablePath, hostPwEntry.GetUsername()));

            if (!success || (success && !options.HasKeyFile()))
            {
                sb.AppendFormat(":\"{0}\"", hostPwEntry.GetPassword());
            }

            sb.AppendFormat("@{0}", hostPwEntry.IPAddress);

            if (success && options.Port.HasValue)
            {
                sb.AppendFormat(":{0}", options.Port);
            }

            // Starting with version 5.6 a passphrase for the private key file can be provided.
            // See: https://winscp.net/eng/docs/faq_passphrase
            if (success && options.HasKeyFile())
            {
                sb.AppendFormat(" -privatekey=\"{0}\" -passphrase=\"{1}\"", options.KeyFilePath, hostPwEntry.GetPassword());
            }

            return(sb.ToString());
        }
Beispiel #2
0
        public void TryParse(String puttyOptionsString, PuttyOptions expectedPuttyOptions)
        {
            PuttyOptions puttyOptions;

            Assert.IsTrue(PuttyOptionsParser.TryParse(puttyOptionsString, out puttyOptions));
            Assert.AreEqual(expectedPuttyOptions, puttyOptions);
        }
Beispiel #3
0
        public void TryParse(String optionsString, String expectedSessionName, int?expectedPort)
        {
            PuttyOptions options;

            Assert.IsTrue(PuttyOptionsParser.TryParse(optionsString, out options));
            Assert.AreEqual(expectedSessionName, options.SessionName);
            Assert.AreEqual(expectedPort, options.Port);
        }
Beispiel #4
0
        public void ChangePassword(IHostPwEntry hostPwEntry, string newPassword)
        {
            PuttyOptions options = null;
            bool         success = PuttyOptionsParser.TryParse(hostPwEntry.AdditionalOptions, out options);

            var passwordChanger = success && options.Port.HasValue ? this.passwordChangerFactory.Create(options.Port.Value) :
                                  this.passwordChangerFactory.Create();

            passwordChanger.ChangePassword(hostPwEntry.IPAddress, hostPwEntry.GetUsername(), hostPwEntry.GetPassword(), newPassword);
        }
        public String Format(IHostPwEntry hostPwEntry)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("\"{0}\"", this.ExecutablePath);

            string ipAddress = null;
            string port      = null;

            if (hostPwEntry.IPAddress.Contains(":"))
            {
                ipAddress = hostPwEntry.IPAddress.Substring(0, hostPwEntry.IPAddress.IndexOf(':'));
                port      = hostPwEntry.IPAddress.Substring(hostPwEntry.IPAddress.IndexOf(':') + 1);
            }
            else
            {
                ipAddress = hostPwEntry.IPAddress;
            }

            PuttyOptions options = null;
            bool         success = PuttyOptionsParser.TryParse(hostPwEntry.AdditionalOptions, out options);

            if (success && options.HasKeyFile())
            {
                sb.AppendFormat(" -i \"{0}\"", options.KeyFilePath);
            }

            if (success && !String.IsNullOrEmpty(options.SessionName))
            {
                ICollection <String> sessionNames = this.PuttySessionFinder.Find(options.SessionName);

                if (sessionNames.Count > 0)
                {
                    sb.AppendFormat(" -load \"{0}\"", new List <String>(sessionNames)[0]);
                }
            }

            if (port != null)
            {
                sb.AppendFormat(" -P {0}", port);
            }

            if (port == null && success && options.Port.HasValue)
            {
                sb.AppendFormat(" -P {0}", options.Port);
            }

            if (hostPwEntry.ConnectionMethods.Contains(ConnectionMethodType.PuttySSH))
            {
                sb.Append(" -ssh");
            }
            else if (hostPwEntry.ConnectionMethods.Contains(ConnectionMethodType.PuttyTelnet))
            {
                sb.Append(" -telnet");
            }

            sb.AppendFormat(" {0}@{1}", hostPwEntry.GetUsername(), ipAddress);

            // Specifying the password via -pw switch only works with SSH protocol.
            // See: http://the.earth.li/~sgtatham/putty/0.65/htmldoc/Chapter3.html.
            if (hostPwEntry.ConnectionMethods.Contains(ConnectionMethodType.PuttySSH))
            {
                // Allow passwords with white-spaces.
                sb.AppendFormat(" -pw \"{0}\"", hostPwEntry.GetPassword());
            }

            return(sb.ToString());
        }
Beispiel #6
0
        public void TryParseReturnsFalse(String optionsString)
        {
            PuttyOptions options;

            Assert.IsFalse(PuttyOptionsParser.TryParse(optionsString, out options));
        }