示例#1
0
        public static void RunExample()
        {
            SshConnectionInfo input = Util.GetInput();
            try
            {
                SshExec exec = new SshExec(input.Host, input.User);
                if(input.Pass != null) exec.Password = input.Pass;
                if(input.IdentityFile != null) exec.AddIdentityFile( input.IdentityFile );

                Console.Write("Connecting...");
                exec.Connect();
                Console.WriteLine("OK");
                while(true)
                {
                    Console.Write("Enter a command to execute ['Enter' to cancel]: ");
                    string command = Console.ReadLine();
                    if(command=="")break;
                    string output = exec.RunCommand(command);
                    Console.WriteLine(output);
                }
                Console.Write("Disconnecting...");
                exec.Close();
                Console.WriteLine("OK");
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
示例#2
0
 public bool Test()
 {
     bool result = false;
     SshExec exec = new SshExec(host, username);
    // SshShell exec = new SshShell(host, username);
     try
     {
         exec.Password = password;
         if (!string.IsNullOrEmpty(pkFile))
         {
             exec.AddIdentityFile(pkFile);
         }
         exec.Connect();
         string output = exec.RunCommand(commandText);
         Console.WriteLine(output);
         result = true;
     }
     catch (Exception)
     {
         result = false;
     }
     finally
     {
         try
         {
             if (exec != null && exec.Connected)
             {
                 exec.Close();
             }
         }
         catch
         { 
         
         }  
     }
     return result;
 }