-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented password changing for Linux hosts.
- Loading branch information
1 parent
8947e4a
commit 76dc967
Showing
16 changed files
with
163 additions
and
12 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
QuickConnectPlugin.Tests/PasswordChanger/LinuxPasswordChangerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System; | ||
using NUnit.Framework; | ||
using QuickConnectPlugin.PasswordChanger; | ||
|
||
namespace QuickConnectPlugin.Tests.PasswordChanger { | ||
|
||
[Ignore("IntegrationTest")] | ||
[TestFixture] | ||
public class LinuxPasswordChangerTests { | ||
|
||
[TestCase("opensuse13.1", "root", "12345678", Description = "openSUSE 13.1")] | ||
public void ChangePassword(String ipAddress, String username, String password) { | ||
var linuxPasswordChanger = new LinuxPasswordChanger(); | ||
Assert.DoesNotThrow(() => linuxPasswordChanger.ChangePassword(ipAddress, username, password, password)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
QuickConnectPlugin/PasswordChanger/LinuxPasswordChanger.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System; | ||
using System.IO; | ||
using Renci.SshNet; | ||
using Renci.SshNet.Common; | ||
|
||
namespace QuickConnectPlugin.PasswordChanger { | ||
|
||
public class LinuxPasswordChanger : IPasswordChanger { | ||
|
||
public void ChangePassword(string host, string username, string password, string newPassword) { | ||
KeyboardInteractiveAuthenticationMethod authMethod = new KeyboardInteractiveAuthenticationMethod(username); | ||
authMethod.AuthenticationPrompt += new EventHandler<AuthenticationPromptEventArgs>((sender, e) => authenticationPrompted(sender, e, password)); | ||
|
||
ConnectionInfo connectionInfo = new ConnectionInfo(host, username, authMethod); | ||
|
||
using (SshClient sshclient = new SshClient(connectionInfo)) { | ||
sshclient.Connect(); | ||
using (var shellStream = sshclient.CreateShellStream("xterm", 80, 24, 800, 600, 1024)) { | ||
using (var writer = new StreamWriter(shellStream) { AutoFlush = true }) { | ||
writer.WriteLine("sudo passwd"); | ||
processShellStream(shellStream, "New Password"); | ||
writer.WriteLine(newPassword); | ||
processShellStream(shellStream, "Reenter New Password"); | ||
writer.WriteLine(newPassword); | ||
processShellStream(shellStream, "Password changed."); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private void processShellStream(ShellStream shellStream, String expectedString) { | ||
var res = shellStream.Expect(expectedString, TimeSpan.FromSeconds(2)); | ||
if (res == null) { | ||
throw new Exception(String.Format("Error changing password. Expected '{0}' string from shell, but got null.", expectedString)); | ||
} | ||
} | ||
|
||
private void authenticationPrompted(object sender, AuthenticationPromptEventArgs e, String password) { | ||
foreach (AuthenticationPrompt prompt in e.Prompts) { | ||
if (prompt.Request.StartsWith("Password:", StringComparison.InvariantCultureIgnoreCase)) { | ||
prompt.Response = password; | ||
} | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
QuickConnectPlugin/PasswordChanger/LinuxPasswordChangerFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
|
||
namespace QuickConnectPlugin.PasswordChanger { | ||
|
||
public class LinuxPasswordChangerFactory : IPasswordChangerGenericFactory<IPasswordChanger> { | ||
|
||
public IPasswordChanger Create() { | ||
return new LinuxPasswordChanger(); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
The MIT License (MIT) | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
Binary file not shown.