示例#1
0
文件: Util.cs 项目: akrisiun/SharpSSH
        /// <summary>
        /// Get input from the user
        /// </summary>
        public static SshConnectionInfo GetInput()
        {
            SshConnectionInfo info = new SshConnectionInfo();

            Console.Write("Enter Remote Host: ");
            info.Host = Console.ReadLine();
            Console.Write("Enter Username: "******"Use publickey authentication? [Yes|No] :");
            //string resp = Console.ReadLine();
            //if(resp.ToLower().StartsWith("y"))
            //{
            //	Console.Write("Enter identity key filename: ");
            //	info.IdentityFile = Console.ReadLine();
            //}
            //else
            //{

            Console.Write("Enter Password: ");
            info.Pass = Console.ReadLine();

            Console.WriteLine();
            return info;
        }
示例#2
0
        public static void RunExample()
        {
            try
            {
                SshConnectionInfo input = Util.GetInput();
                SshShell          shell = new SshShell(input.Host, input.User);
                if (input.Pass != null)
                {
                    shell.Password = input.Pass;
                }
                if (input.IdentityFile != null)
                {
                    shell.AddIdentityFile(input.IdentityFile);
                }

                //This statement must be prior to connecting
                shell.RedirectToConsole();

                Console.Write("Connecting...");
                shell.Connect();
                Console.WriteLine("OK");

                while (shell.ShellOpened)
                {
                    System.Threading.Thread.Sleep(500);
                }
                Console.Write("Disconnecting...");
                shell.Close();
                Console.WriteLine("OK");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
示例#3
0
        /// <summary>
        /// Get input from the user
        /// </summary>
        public static SshConnectionInfo GetInput()
        {
            SshConnectionInfo info = new SshConnectionInfo();

            Console.Write("Enter Remote Host: ");
            info.Host = Console.ReadLine();
            Console.Write("Enter Username: "******"Use publickey authentication? [Yes|No] :");
            //string resp = Console.ReadLine();
            //if(resp.ToLower().StartsWith("y"))
            //{
            //	Console.Write("Enter identity key filename: ");
            //	info.IdentityFile = Console.ReadLine();
            //}
            //else
            //{

            Console.Write("Enter Password: ");
            info.Pass = Console.ReadLine();

            Console.WriteLine();
            return(info);
        }
示例#4
0
        public static void RunExample()
        {
            try
            {
                SshConnectionInfo input = Util.GetInput();
                SshShell          ssh   = new SshShell(input.Host, input.User);
                if (input.Pass != null)
                {
                    ssh.Password = input.Pass;
                }
                if (input.IdentityFile != null)
                {
                    ssh.AddIdentityFile(input.IdentityFile);
                }

                Console.Write("Connecting...");
                ssh.Connect();
                Console.WriteLine("OK");


                Console.Write("Enter a pattern to expect in response [e.g. '#', '$', C:\\\\.*>, etc...]: ");
                string pattern = Console.ReadLine();

                ssh.ExpectPattern = pattern;
                ssh.RemoveTerminalEmulationCharacters = true;

                Console.WriteLine();
                Console.WriteLine(ssh.Expect(pattern));

                while (ssh.ShellOpened)
                {
                    Console.WriteLine();
                    Console.Write("Enter some data to write ['Enter' to cancel]: ");
                    string data = Console.ReadLine();
                    if (data == "")
                    {
                        break;
                    }
                    ssh.WriteLine(data);

                    string output = ssh.Expect(pattern);
                    Console.WriteLine(output);
                }

                Console.Write("Disconnecting...");
                ssh.Close();
                Console.WriteLine("OK");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
示例#5
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);
            }
        }
示例#6
0
        public static void RunExample()
        {
            try
            {
                SshConnectionInfo       input = Util.GetInput();
                string                  proto = GetProtocol();
                SshTransferProtocolBase sshCp;

                if (proto.Equals("scp"))
                {
                    sshCp = new Scp(input.Host, input.User);
                }
                else
                {
                    sshCp = new Sftp(input.Host, input.User);
                }

                if (input.Pass != null)
                {
                    sshCp.Password = input.Pass;
                }
                if (input.IdentityFile != null)
                {
                    sshCp.AddIdentityFile(input.IdentityFile);
                }
                sshCp.OnTransferStart    += new FileTransferEvent(sshCp_OnTransferStart);
                sshCp.OnTransferProgress += new FileTransferEvent(sshCp_OnTransferProgress);
                sshCp.OnTransferEnd      += new FileTransferEvent(sshCp_OnTransferEnd);

                Console.Write("Connecting...");
                sshCp.Connect();
                Console.WriteLine("OK");

                while (true)
                {
                    string direction = GetTransferDirection();
                    if (direction.Equals("to"))
                    {
                        string lfile = GetArg("Enter local file ['Enter to cancel']");
                        if (lfile == "")
                        {
                            break;
                        }
                        string rfile = GetArg("Enter remote file ['Enter to cancel']");
                        if (rfile == "")
                        {
                            break;
                        }
                        sshCp.Put(lfile, rfile);
                    }
                    else
                    {
                        string rfile = GetArg("Enter remote file ['Enter to cancel']");
                        if (rfile == "")
                        {
                            break;
                        }
                        string lpath = GetArg("Enter local path ['Enter to cancel']");
                        if (lpath == "")
                        {
                            break;
                        }
                        sshCp.Get(rfile, lpath);
                    }
                }

                Console.Write("Disconnecting...");
                sshCp.Close();
                Console.WriteLine("OK");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }