Providing subcommands to a command (sudo/su) executed with SSH.NET SshClient.CreateShellStream

人盡茶涼 提交于 2019-12-01 13:37:17

Just write the "commands" to the StreamWriter.

writer.WriteLine("sudo su - wwabc11");
writer.WriteLine("whoami");
// etc

See also C# send Ctrl+Y over SSH.NET.


Though note that using CreateShellStream ("shell" channel) is not the correct way to automate a commands execution. You should use CreateCommand/RunCommand ("exec" channel). Though SSH.NET limited API to the "exec" channel does not support providing an input to commands executed this way. And whoami and the others are actually inputs to sudo/su command.

A solution would be to provide the commands to su on its command-line, like:

sudo su - wwabc11 -c "whoami ; cd /wwabc11/batch/bin/ ; pwd"

For an example of code that uses CreateCommand to execute a command and reads its output, see see SSH.NET real-time command output monitoring.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!