Sudo Command in the C# SSH .Net library

后端 未结 1 762
夕颜
夕颜 2021-01-06 08:14

This is the simplest sudo implementation of the SSH .Net library I can find. However, I can not get it to work.

    using (var ssh = new SshClient(\"hostnam         


        
1条回答
  •  时光说笑
    2021-01-06 08:53

            public void ExpectSSH (string address, string login, string password, string command)
        {
            try
            {
                SshClient sshClient = new SshClient(address, 22, login, password);
    
                sshClient.Connect();
                IDictionary termkvp = new Dictionary();
                termkvp.Add(Renci.SshNet.Common.TerminalModes.ECHO, 53);
    
                ShellStream shellStream = sshClient.CreateShellStream("xterm", 80,24, 800, 600, 1024, termkvp);
    
    
                //Get logged in
                string rep = shellStream.Expect(new Regex(@"[$>]")); //expect user prompt
                this.writeOutput(results, rep);
    
                //send command
                shellStream.WriteLine(commandTxt.Text);
                rep = shellStream.Expect(new Regex(@"([$#>:])")); //expect password or user prompt
                this.writeOutput(results, rep);
    
                //check to send password
                if (rep.Contains(":"))
                {
                    //send password
                    shellStream.WriteLine(password);
                    rep = shellStream.Expect(new Regex(@"[$#>]")); //expect user or root prompt
                    this.writeOutput(results, rep);
                }
    
                sshClient.Disconnect();      
            }//try to open connection
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.ToString());
                throw ex;
            }
    
        }
    

    0 讨论(0)
提交回复
热议问题