本文整理汇总了C#中SshClient.CreateCommand方法的典型用法代码示例。如果您正苦于以下问题:C# SshClient.CreateCommand方法的具体用法?C# SshClient.CreateCommand怎么用?C# SshClient.CreateCommand使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SshClient
的用法示例。
在下文中一共展示了SshClient.CreateCommand方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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();
}
}
示例2: 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);
}
}
示例3: 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();
}
}
示例4: 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.");
}
示例5: Test_Execute_Command_Asynchronously
public void Test_Execute_Command_Asynchronously()
{
using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
{
client.Connect();
var cmd = client.CreateCommand("sleep 5s; echo 'test'");
var asyncResult = cmd.BeginExecute(null, null);
while (!asyncResult.IsCompleted)
{
Thread.Sleep(100);
}
cmd.EndExecute(asyncResult);
Assert.IsTrue(cmd.Result == "test\n");
client.Disconnect();
}
}
示例6: Test_Execute_SingleCommand
public void Test_Execute_SingleCommand()
{
var host = Resources.HOST;
var username = Resources.USERNAME;
var password = Resources.PASSWORD;
using (var client = new SshClient(host, username, password))
{
#region Example SshCommand CreateCommand Execute
client.Connect();
var testValue = Guid.NewGuid().ToString();
var command = string.Format("echo {0}", testValue);
var cmd = client.CreateCommand(command);
var result = cmd.Execute();
result = result.Substring(0, result.Length - 1); // Remove \n character returned by command
client.Disconnect();
#endregion
Assert.IsTrue(result.Equals(testValue));
}
}
示例7: Test_PortForwarding_Remote
public void Test_PortForwarding_Remote()
{
// ******************************************************************
// ************* Tests are still in not finished ********************
// ******************************************************************
using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
{
client.Connect();
var port1 = new ForwardedPortRemote(8082, "www.renci.org", 80);
client.AddForwardedPort(port1);
port1.Exception += delegate(object sender, ExceptionEventArgs e)
{
Assert.Fail(e.Exception.ToString());
};
port1.Start();
var boundport = port1.BoundPort;
System.Threading.Tasks.Parallel.For(0, 5,
//new ParallelOptions
//{
// MaxDegreeOfParallelism = 1,
//},
(counter) =>
{
var cmd = client.CreateCommand(string.Format("wget -O- http://localhost:{0}", boundport));
var result = cmd.Execute();
var end = DateTime.Now;
Debug.WriteLine(string.Format("Length: {0}", result.Length));
}
);
Thread.Sleep(1000 * 100);
port1.Stop();
}
}
示例8: Test_Execute_Command_with_ExtendedOutput
public void Test_Execute_Command_with_ExtendedOutput()
{
using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
{
client.Connect();
var cmd = client.CreateCommand("echo 12345; echo 654321 >&2");
cmd.Execute();
//var extendedData = Encoding.ASCII.GetString(cmd.ExtendedOutputStream.ToArray());
var extendedData = new StreamReader(cmd.ExtendedOutputStream, Encoding.ASCII).ReadToEnd();
client.Disconnect();
Assert.AreEqual("12345\n", cmd.Result);
Assert.AreEqual("654321\n", extendedData);
}
}
示例9: Test_Execute_ExtendedOutputStream
public void Test_Execute_ExtendedOutputStream()
{
var host = Resources.HOST;
var username = Resources.USERNAME;
var password = Resources.PASSWORD;
using (var client = new SshClient(host, username, password))
{
#region Example SshCommand CreateCommand Execute ExtendedOutputStream
client.Connect();
var cmd = client.CreateCommand("echo 12345; echo 654321 >&2");
var result = cmd.Execute();
Console.Write(result);
var reader = new StreamReader(cmd.ExtendedOutputStream);
Console.WriteLine("DEBUG:");
Console.Write(reader.ReadToEnd());
client.Disconnect();
#endregion
Assert.Inconclusive();
}
}
示例10: 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();
}
}
示例11: ExecuteTestCommand
private static bool ExecuteTestCommand(SshClient s)
{
var testValue = Guid.NewGuid().ToString();
var command = string.Format("echo {0}", testValue);
var cmd = s.CreateCommand(command);
var result = cmd.Execute();
result = result.Substring(0, result.Length - 1); // Remove \n character returned by command
return result.Equals(testValue);
}
示例12: Test_Execute_Invalid_Command
public void Test_Execute_Invalid_Command()
{
using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
{
#region Example SshCommand CreateCommand Error
client.Connect();
var cmd = client.CreateCommand(";");
cmd.Execute();
if (!string.IsNullOrEmpty(cmd.Error))
{
Console.WriteLine(cmd.Error);
}
client.Disconnect();
#endregion
Assert.Inconclusive();
}
}
示例13: Test_Execute_Timeout
public void Test_Execute_Timeout()
{
using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
{
client.Connect();
var cmd = client.CreateCommand("sleep 10s");
cmd.CommandTimeout = TimeSpan.FromSeconds(5);
cmd.Execute();
client.Disconnect();
}
}
示例14: Test_Get_Result_Without_Execution
public void Test_Get_Result_Without_Execution()
{
using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
{
client.Connect();
var cmd = client.CreateCommand("ls -l");
Assert.IsTrue(string.IsNullOrEmpty(cmd.Result));
client.Disconnect();
}
}
示例15: Test_Execute_InvalidCommand_Then_Execute_ValidCommand
public void Test_Execute_InvalidCommand_Then_Execute_ValidCommand()
{
using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
{
client.Connect();
var cmd = client.CreateCommand(";");
cmd.Execute();
if (string.IsNullOrEmpty(cmd.Error))
{
Assert.Fail("Operation should fail");
}
Assert.IsTrue(cmd.ExitStatus > 0);
var result = ExecuteTestCommand(client);
client.Disconnect();
Assert.IsTrue(result);
}
}