Beispiel #1
0
        /// <summary>
        /// Bind shell to terminal/console
        /// </summary>
        /// <returns></returns>
        public static async Task BindAsync(this ISecureShell shell,
                                           bool forceVTerm, CancellationToken ct)
        {
            var win     = IsWindowsAnniversaryEdition(forceVTerm);
            var inMode  = ~0u;
            var outMode = ~0u;

            if (IsWindowsAnniversaryEdition(forceVTerm))
            {
                EnableVtermOnWindows10AnniversaryEdition(
                    out inMode, out outMode);
            }
            var ctrlC = Console.TreatControlCAsInput;

            Console.TreatControlCAsInput = true;
            using (var input = Console.OpenStandardInput())
                using (var output = Console.OpenStandardOutput()) {
                    await shell.BindAsync(input, output,
                                          Console.WindowWidth, Console.WindowHeight,
                                          Console.BufferWidth, Console.BufferHeight, ct);
                }
            Console.TreatControlCAsInput = ctrlC;
            if (inMode != ~0u && outMode != ~0u)
            {
                RestoreConsoleModes(inMode, outMode);
            }
        }
 /// <summary>
 /// Download with default timeout of 1 minute
 /// </summary>
 /// <param name="shell"></param>
 /// <param name="destBuff"></param>
 /// <param name="maxCount"></param>
 /// <param name="fileName"></param>
 /// <param name="fromPath"></param>
 /// <param name="isUserHomeBased"></param>
 /// <returns></returns>
 public static Task <int> DownloadAsync(this ISecureShell shell,
                                        byte[] destBuff, int maxCount, string fileName,
                                        string fromPath, bool isUserHomeBased)
 {
     return(shell.DownloadAsync(destBuff, maxCount, fileName, fromPath,
                                isUserHomeBased, TimeSpan.FromMinutes(1)));
 }
Beispiel #3
0
        /// <summary>
        /// Upload folder
        /// </summary>
        /// <param name="shell"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        private static async Task UploadFolderAsync(ISecureShell shell,
                                                    CliOptions options)
        {
            var to   = options.GetValue <string>("-t", "--to");
            var from = options.GetValue <string>("-p", "--path");

            await UploadFolderAsync(shell, options, to, from);
        }
Beispiel #4
0
        /// <summary>
        /// Run command
        /// </summary>
        /// <param name="shell"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        private static async Task ExecuteCommandAsync(ISecureShell shell,
                                                      CliOptions options)
        {
            var str = await shell.ExecuteCommandAsync(
                options.GetValue <string>("-c", "--command"));

            PrintResult(options, str);
        }
 /// <summary>
 /// Execute with timeout
 /// </summary>
 /// <param name="shell"></param>
 /// <param name="commandToExecute"></param>
 /// <param name="timeout"></param>
 /// <returns></returns>
 public static Task <string> ExecuteCommandAsync(this ISecureShell shell,
                                                 string commandToExecute, TimeSpan timeout)
 {
     using (var cts = new CancellationTokenSource(timeout)) {
         return(shell.ExecuteCommandAsync(commandToExecute,
                                          cts.Token));
     }
 }
Beispiel #6
0
 /// <summary>
 /// Download folder
 /// </summary>
 /// <param name="shell"></param>
 /// <param name="options"></param>
 /// <returns></returns>
 private static async Task DownloadFolderAsync(ISecureShell shell,
                                               CliOptions options)
 {
     var from = options.GetValue <string>("-f", "--from");
     var path = options.GetValue <string>("-p", "--path");
     await shell.DownloadFolderAsync(path, from,
                                     options.GetValueOrDefault("-h", "--home", true));
 }
 /// <summary>
 /// Download with timeout
 /// </summary>
 /// <param name="shell"></param>
 /// <param name="fileName"></param>
 /// <param name="fromPath"></param>
 /// <param name="isUserHomeBased"></param>
 /// <param name="timeout"></param>
 /// <returns></returns>
 public static Task <string> DownloadAsync(this ISecureShell shell,
                                           string fileName, string fromPath, bool isUserHomeBased,
                                           TimeSpan timeout)
 {
     using (var cts = new CancellationTokenSource(timeout)) {
         return(shell.DownloadAsync(fileName, fromPath, isUserHomeBased,
                                    cts.Token));
     }
 }
 /// <summary>
 /// Upload with timeout and default permissions
 /// </summary>
 /// <param name="shell"></param>
 /// <param name="data"></param>
 /// <param name="fileName"></param>
 /// <param name="toPath"></param>
 /// <param name="isUserHomeBased"></param>
 /// <param name="timeout"></param>
 /// <returns></returns>
 public static Task UploadAsync(this ISecureShell shell, byte[] data,
                                string fileName, string toPath, bool isUserHomeBased,
                                TimeSpan timeout)
 {
     using (var cts = new CancellationTokenSource(timeout)) {
         return(shell.UploadAsync(data, fileName, toPath, isUserHomeBased, null,
                                  cts.Token));
     }
 }
 /// <summary>
 /// Download with timeout
 /// </summary>
 /// <param name="shell"></param>
 /// <param name="destBuff"></param>
 /// <param name="maxCount"></param>
 /// <param name="fileName"></param>
 /// <param name="fromPath"></param>
 /// <param name="isUserHomeBased"></param>
 /// <param name="timeout"></param>
 /// <returns></returns>
 public static Task <int> DownloadAsync(this ISecureShell shell,
                                        byte[] destBuff, int maxCount, string fileName,
                                        string fromPath, bool isUserHomeBased, TimeSpan timeout)
 {
     using (var cts = new CancellationTokenSource(timeout)) {
         return(shell.DownloadAsync(destBuff, maxCount, fileName, fromPath,
                                    isUserHomeBased, cts.Token));
     }
 }
Beispiel #10
0
        /// <summary>
        /// Upload file
        /// </summary>
        /// <param name="shell"></param>
        /// <param name="options"></param>
        /// <param name="to"></param>
        /// <param name="from"></param>
        /// <param name="file"></param>
        /// <returns></returns>
        private static async Task UploadFileAsync(ISecureShell shell,
                                                  CliOptions options, string to, string from, string file)
        {
            var buffer = await File.ReadAllBytesAsync(Path.Combine(from, file));

            await shell.UploadAsync(buffer, file, to,
                                    options.GetValueOrDefault("-h", "--home", true),
                                    options.GetValueOrDefault <string>("-m", "--mode", null));
        }
Beispiel #11
0
        /// <summary>
        /// Download file
        /// </summary>
        /// <param name="shell"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        private static async Task DownloadAsync(ISecureShell shell,
                                                CliOptions options)
        {
            var file = options.GetValue <string>("-f", "--file");
            var from = options.GetValue <string>("-p", "--path");
            var str  = await shell.DownloadAsync(file, from,
                                                 options.GetValueOrDefault("-h", "--home", true));

            PrintResult(options, str);
        }
 /// <summary>
 /// Shell into simulation
 /// </summary>
 /// <param name="shell"></param>
 /// <param name="options"></param>
 /// <returns></returns>
 private static async Task RunTerminalAsync(ISecureShell shell,
                                            CliOptions options)
 {
     if (shell == null)
     {
         throw new ArgumentException("Must login first");
     }
     var cts = new CancellationTokenSource(
         options.GetValueOrDefault("-t", "--timeout", -1));
     await shell.BindAsync(cts.Token);
 }
Beispiel #13
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);
 }
Beispiel #14
0
        /// <summary>
        /// Upload folder
        /// </summary>
        /// <param name="shell"></param>
        /// <param name="options"></param>
        /// <param name="to"></param>
        /// <param name="from"></param>
        /// <returns></returns>
        private static async Task UploadFolderAsync(ISecureShell shell,
                                                    CliOptions options, string to, string from)
        {
            foreach (var file in Directory.EnumerateFiles(from))
            {
                await UploadFileAsync(shell, options, to, from, file);
            }
            if (!options.IsSet("-r", "--recursive"))
            {
                return;
            }
            foreach (var dir in Directory.EnumerateDirectories(from))
            {
                await UploadFolderAsync(shell, options,
                                        to + "/" + dir, Path.Combine(from, dir));

                // TODO: Path seperator is platform specific
            }
        }
Beispiel #15
0
 /// <summary>
 /// Download with default timeout of 1 minute
 /// </summary>
 /// <param name="shell"></param>
 /// <param name="fileName"></param>
 /// <param name="fromPath"></param>
 /// <param name="isUserHomeBased"></param>
 /// <returns></returns>
 public static Task <string> DownloadAsync(this ISecureShell shell,
                                           string fileName, string fromPath, bool isUserHomeBased)
 {
     return(shell.DownloadAsync(fileName, fromPath, isUserHomeBased,
                                TimeSpan.FromMinutes(1)));
 }
Beispiel #16
0
 /// <summary>
 /// Download with timeout
 /// </summary>
 /// <param name="shell"></param>
 /// <param name="fileName"></param>
 /// <param name="fromPath"></param>
 /// <param name="isUserHomeBased"></param>
 /// <param name="timeout"></param>
 /// <returns></returns>
 public static Task <string> DownloadAsync(this ISecureShell shell,
                                           string fileName, string fromPath, bool isUserHomeBased,
                                           TimeSpan timeout) =>
 shell.DownloadAsync(fileName, fromPath, isUserHomeBased,
                     new CancellationTokenSource(timeout).Token);
Beispiel #17
0
 /// <summary>
 /// Download folder  with timeout
 /// </summary>
 /// <param name="shell"></param>
 /// <param name="toPath"></param>
 /// <param name="fromPath"></param>
 /// <param name="isUserHomeBased"></param>
 /// <param name="timeout"></param>
 /// <returns></returns>
 public static Task DownloadFolderAsync(this ISecureShell shell,
                                        string toPath, string fromPath, bool isUserHomeBased,
                                        TimeSpan timeout) =>
 shell.DownloadFolderAsync(toPath, fromPath, isUserHomeBased,
                           new CancellationTokenSource(timeout).Token);
Beispiel #18
0
 /// <summary>
 /// Upload file with default permissions
 /// </summary>
 /// <param name="shell"></param>
 /// <param name="data"></param>
 /// <param name="fileName"></param>
 /// <param name="toPath"></param>
 /// <param name="isUserHomeBased"></param>
 /// <param name="ct"></param>
 /// <returns></returns>
 public static Task UploadAsync(this ISecureShell shell, byte[] data,
                                string fileName, string toPath, bool isUserHomeBased,
                                CancellationToken ct)
 {
     return(shell.UploadAsync(data, fileName, toPath, isUserHomeBased, null, ct));
 }
Beispiel #19
0
 /// <summary>
 /// Bind shell to current terminal/console
 /// </summary>
 /// <returns></returns>
 public static Task BindAsync(this ISecureShell shell,
                              CancellationToken ct) => BindAsync(shell, false, ct);
Beispiel #20
0
 /// <summary>
 /// Upload with default timeout of 1 minute and default permissions
 /// </summary>
 /// <param name="shell"></param>
 /// <param name="data"></param>
 /// <param name="fileName"></param>
 /// <param name="toPath"></param>
 /// <param name="isUserHomeBased"></param>
 /// <returns></returns>
 public static Task UploadAsync(this ISecureShell shell, byte[] data,
                                string fileName, string toPath, bool isUserHomeBased)
 {
     return(shell.UploadAsync(data, fileName, toPath, isUserHomeBased,
                              TimeSpan.FromMinutes(1)));
 }
Beispiel #21
0
 /// <summary>
 /// Execute with default timeout of 3 minutes
 /// </summary>
 /// <param name="shell"></param>
 /// <param name="commandToExecute"></param>
 /// <returns></returns>
 public static Task <string> ExecuteCommandAsync(this ISecureShell shell,
                                                 string commandToExecute)
 {
     return(shell.ExecuteCommandAsync(commandToExecute, TimeSpan.FromMinutes(3)));
 }
Beispiel #22
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();
            }
        }
Beispiel #23
0
 /// <summary>
 /// Upload with timeout and default permissions
 /// </summary>
 /// <param name="shell"></param>
 /// <param name="data"></param>
 /// <param name="fileName"></param>
 /// <param name="toPath"></param>
 /// <param name="isUserHomeBased"></param>
 /// <param name="timeout"></param>
 /// <returns></returns>
 public static Task UploadAsync(this ISecureShell shell, byte[] data,
                                string fileName, string toPath, bool isUserHomeBased,
                                TimeSpan timeout) =>
 shell.UploadAsync(data, fileName, toPath, isUserHomeBased, null,
                   new CancellationTokenSource(timeout).Token);
Beispiel #24
0
 /// <summary>
 /// Download folder with default timeout of 5 minute
 /// </summary>
 /// <param name="shell"></param>
 /// <param name="toPath"></param>
 /// <param name="fromPath"></param>
 /// <param name="isUserHomeBased"></param>
 /// <returns></returns>
 public static Task DownloadFolderAsync(this ISecureShell shell,
                                        string toPath, string fromPath, bool isUserHomeBased)
 {
     return(shell.DownloadFolderAsync(toPath, fromPath, isUserHomeBased,
                                      TimeSpan.FromMinutes(5)));
 }
Beispiel #25
0
 /// <summary>
 /// Execute with timeout
 /// </summary>
 /// <param name="shell"></param>
 /// <param name="commandToExecute"></param>
 /// <param name="timeout"></param>
 /// <returns></returns>
 public static Task <string> ExecuteCommandAsync(this ISecureShell shell,
                                                 string commandToExecute, TimeSpan timeout) =>
 shell.ExecuteCommandAsync(commandToExecute,
                           new CancellationTokenSource(timeout).Token);