예제 #1
0
 /// <summary>
 /// Logout
 /// </summary>
 /// <param name="shell"></param>
 /// <returns></returns>
 private static ISecureShell Logout(ISecureShell shell)
 {
     if (shell == null)
     {
         Console.WriteLine("Not logged in.");
     }
     else
     {
         shell.Dispose();
     }
     return(null);
 }
예제 #2
0
        /// <summary>
        /// Run client
        /// </summary>
        /// <param name="args">command-line arguments</param>
        public static async Task RunAsync(string[] args)
        {
            var          sshFactory = new SshShellFactory(LogEx.ConsoleOut());
            ISecureShell shell      = null;

            try {
                var run = true;
                do
                {
                    if (run)
                    {
                        Console.Write("> ");
                        args = CliOptions.ParseAsCommandLine(Console.ReadLine());
                    }
                    try {
                        if (args.Length < 1)
                        {
                            throw new ArgumentException("Need a command!");
                        }
                        var command = args[0].ToLowerInvariant();
                        var options = new CliOptions(args);
                        switch (command)
                        {
                        case "exit":
                            run = false;
                            break;

                        case "logout":
                            shell = Logout(shell);
                            break;

                        case "login":
                            if (shell != null)
                            {
                                throw new ArgumentException("Already logged in.");
                            }
                            shell = await LoginAsync(sshFactory, options);

                            break;

                        case "terminal":
                            await RunTerminalAsync(shell, options);

                            break;

                        case "exec":
                            await ExecuteCommandAsync(shell, options);

                            break;

                        case "download":
                            await DownloadAsync(shell, options);

                            break;

                        case "upload":
                            await UploadAsync(shell, options);

                            break;

                        case "folderup":
                            await UploadFolderAsync(shell, options);

                            break;

                        case "folderdown":
                            await DownloadFolderAsync(shell, options);

                            break;

                        case "-?":
                        case "-h":
                        case "--help":
                        case "help":
                            PrintHelp();
                            break;

                        default:
                            throw new ArgumentException($"Unknown command {command}.");
                        }
                    }
                    catch (ArgumentException e) {
                        Console.WriteLine(e.Message);
                        if (!run)
                        {
                            PrintHelp();
                            return;
                        }
                    }
                    catch (Exception e) {
                        Console.WriteLine("==================");
                        Console.WriteLine(e);
                        Console.WriteLine("==================");
                    }
                }while (run);
            }
            finally {
                shell?.Dispose();
            }
        }