本文整理汇总了C#中SshClient类的典型用法代码示例。如果您正苦于以下问题:C# SshClient类的具体用法?C# SshClient怎么用?C# SshClient使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SshClient类属于命名空间,在下文中一共展示了SshClient类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Test_Connect_Handle_HostKeyReceived
public void Test_Connect_Handle_HostKeyReceived()
{
var host = Resources.HOST;
var username = Resources.USERNAME;
var password = Resources.PASSWORD;
var hostKeyValidated = false;
#region Example SshClient Connect HostKeyReceived
using (var client = new SshClient(host, username, password))
{
client.HostKeyReceived += delegate(object sender, HostKeyEventArgs e)
{
hostKeyValidated = true;
if (e.FingerPrint.SequenceEqual(new byte[] { 0x00, 0x01, 0x02, 0x03 }))
{
e.CanTrust = true;
}
else
{
e.CanTrust = false;
}
};
client.Connect();
// Do something here
client.Disconnect();
}
#endregion
Assert.IsTrue(hostKeyValidated);
}
示例2: Test_KeyExchange_Rekeying
public void Test_KeyExchange_Rekeying()
{
var connectionInfo = new PasswordConnectionInfo(Resources.HOST, 22, Resources.USERNAME, Resources.PASSWORD);
using (var client = new SshClient(connectionInfo))
{
client.Connect();
// TODO: Add test to test re-keying
Assert.Inconclusive();
client.Disconnect();
}
}
示例3: BeginExecuteTest
public void BeginExecuteTest()
{
string expected = "123\n";
string result;
using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
{
#region Example SshCommand CreateCommand BeginExecute IsCompleted EndExecute
client.Connect();
var cmd = client.CreateCommand("sleep 15s;echo 123"); // Perform long running task
var asynch = cmd.BeginExecute();
while (!asynch.IsCompleted)
{
// Waiting for command to complete...
Thread.Sleep(2000);
}
result = cmd.EndExecute(asynch);
client.Disconnect();
#endregion
Assert.IsNotNull(asynch);
Assert.AreEqual(expected, result);
}
}
示例4: Test_Execute_Command_Asynchronously_With_Callback
public void Test_Execute_Command_Asynchronously_With_Callback()
{
using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
{
client.Connect();
var callbackCalled = false;
var cmd = client.CreateCommand("sleep 5s; echo 'test'");
var asyncResult = cmd.BeginExecute(new AsyncCallback((s) =>
{
callbackCalled = true;
}), null);
while (!asyncResult.IsCompleted)
{
Thread.Sleep(100);
}
cmd.EndExecute(asyncResult);
Assert.IsTrue(callbackCalled);
client.Disconnect();
}
}
示例5: Main
static void Main(string[] args)
{
// Setup Credentials and Server Information
ConnectionInfo ConnNfo = new ConnectionInfo("10.141.3.110",22,"root",
new AuthenticationMethod[]{
// Pasword based Authentication
new PasswordAuthenticationMethod("root","ismail"),
}
);
// Execute (SHELL) Commands
using (var sshclient = new SshClient(ConnNfo)){
sshclient.Connect();
// quick way to use ist, but not best practice - SshCommand is not Disposed, ExitStatus not checked...
Console.WriteLine("telnet localhost 6571");
Console.WriteLine("denemeeeeeee");
//Console.WriteLine(sshclient.CreateCommand("cd /tmp && ls -lah").Execute());
//Console.WriteLine(sshclient.CreateCommand("pwd").Execute());
//Console.WriteLine(sshclient.CreateCommand("cd /tmp/uploadtest && ls -lah").Execute());
sshclient.Disconnect();
}
Console.ReadKey();
}
示例6: Test_Execute_Command_Asynchronously_With_Callback_On_Different_Thread
public void Test_Execute_Command_Asynchronously_With_Callback_On_Different_Thread()
{
using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
{
client.Connect();
var currentThreadId = Thread.CurrentThread.ManagedThreadId;
int callbackThreadId = 0;
var cmd = client.CreateCommand("sleep 5s; echo 'test'");
var asyncResult = cmd.BeginExecute(new AsyncCallback((s) =>
{
callbackThreadId = Thread.CurrentThread.ManagedThreadId;
}), null);
while (!asyncResult.IsCompleted)
{
Thread.Sleep(100);
}
cmd.EndExecute(asyncResult);
Assert.AreNotEqual(currentThreadId, callbackThreadId);
client.Disconnect();
}
}
示例7: AddForwardedPortTest
public void AddForwardedPortTest()
{
ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value
SshClient target = new SshClient(connectionInfo); // TODO: Initialize to an appropriate value
ForwardedPort port = null; // TODO: Initialize to an appropriate value
target.AddForwardedPort(port);
Assert.Inconclusive("A method that does not return a value cannot be verified.");
}
示例8: Test_AddForwardedPort_Local_Hosts_Are_Empty
public void Test_AddForwardedPort_Local_Hosts_Are_Empty()
{
using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
{
client.Connect();
var port1 = client.AddForwardedPort<ForwardedPortLocal>(string.Empty, 8080, string.Empty, 80);
client.Disconnect();
}
}
示例9: Test_AddForwardedPort_Invalid_PortNumber
public void Test_AddForwardedPort_Invalid_PortNumber()
{
using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
{
client.Connect();
var port1 = client.AddForwardedPort<ForwardedPortRemote>("localhost", IPEndPoint.MaxPort+1, "www.renci.org", IPEndPoint.MaxPort+1);
client.Disconnect();
}
}
示例10: Test_Execute_SingleCommand_Without_Connecting
public void Test_Execute_SingleCommand_Without_Connecting()
{
using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
{
var result = ExecuteTestCommand(client);
Assert.IsTrue(result);
}
}
示例11: CleanCurrentFolder
public void CleanCurrentFolder()
{
using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
{
client.Connect();
client.RunCommand("rm -rf *");
client.Disconnect();
}
}
示例12: Test_AddForwardedPort_Remote_Hosts_Are_Null
public void Test_AddForwardedPort_Remote_Hosts_Are_Null()
{
using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
{
client.Connect();
var port1 = client.AddForwardedPort<ForwardedPortRemote>(null, 8080, null, 80);
client.Disconnect();
}
}
示例13: Test_EndExecute_Before_BeginExecute
public void Test_EndExecute_Before_BeginExecute()
{
using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
{
client.Connect();
var cmd = client.CreateCommand("ls -l");
cmd.EndExecute(null);
client.Disconnect();
}
}
示例14: CreateCommandTest1
public void CreateCommandTest1()
{
ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value
SshClient target = new SshClient(connectionInfo); // TODO: Initialize to an appropriate value
string commandText = string.Empty; // TODO: Initialize to an appropriate value
SshCommand expected = null; // TODO: Initialize to an appropriate value
SshCommand actual;
actual = target.CreateCommand(commandText);
Assert.AreEqual(expected, actual);
Assert.Inconclusive("Verify the correctness of this test method.");
}
示例15: Test_HMac_Sha1_Connection
public void Test_HMac_Sha1_Connection()
{
var connectionInfo = new PasswordConnectionInfo(Resources.HOST, 22, Resources.USERNAME, Resources.PASSWORD);
connectionInfo.HmacAlgorithms.Clear();
connectionInfo.HmacAlgorithms.Add("hmac-sha1", (key) => { return new HMac<SHA1Hash>(key.Take(20).ToArray()); });
using (var client = new SshClient(connectionInfo))
{
client.Connect();
client.Disconnect();
}
}