public void TryParse(String puttyOptionsString, PuttyOptions expectedPuttyOptions) { PuttyOptions puttyOptions; Assert.IsTrue(PuttyOptions.TryParse(puttyOptionsString, out puttyOptions)); Assert.AreEqual(expectedPuttyOptions, puttyOptions); }
public String Format(IHostPwEntry hostPwEntry) { PuttyOptions options = null; bool success = PuttyOptions.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()); }
public void TryParse(String optionsString, String expectedSessionName, int?expectedPort) { PuttyOptions options; Assert.IsTrue(PuttyOptions.TryParse(optionsString, out options)); Assert.AreEqual(expectedSessionName, options.SessionName); Assert.AreEqual(expectedPort, options.Port); }
public void ChangePassword(IHostPwEntry hostPwEntry, string newPassword) { PuttyOptions options = null; bool success = PuttyOptions.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 void TryParseReturnsFalse(String optionsString) { PuttyOptions options; Assert.IsFalse(PuttyOptions.TryParse(optionsString, out options)); }
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 = PuttyOptions.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 (!success || (success && !options.CommandContainsPort)) { 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 (this.AppendPassword && hostPwEntry.ConnectionMethods.Contains(ConnectionMethodType.PuttySSH)) { // Allow passwords with white-spaces. if (!success || (success && !options.CommandContains("-pw "))) { var password = hostPwEntry.GetPassword(); if (password.Contains(@"""")) { sb.AppendFormat(" -pw \"{0}\"", password.Replace(@"""", @"\""")); } else { sb.AppendFormat(" -pw \"{0}\"", password); } } } if (success && options.HasCommand()) { sb.AppendFormat(" {0}", options.Command); } return(sb.ToString()); }