C# 无法在linux box C上执行脚本#
我使用下面的C#代码连接到Linux设备并执行一个脚本C# 无法在linux box C上执行脚本#,c#,linux,ssh,C#,Linux,Ssh,我使用下面的C#代码连接到Linux设备并执行一个脚本 public static void linux_connect() { KeyboardInteractiveAuthenticationMethod kauth = new KeyboardInteractiveAuthenticationMethod(<username>); PasswordAuthenticationMethod pauth = new PasswordAuthenticati
public static void linux_connect()
{
KeyboardInteractiveAuthenticationMethod kauth = new KeyboardInteractiveAuthenticationMethod(<username>);
PasswordAuthenticationMethod pauth = new PasswordAuthenticationMethod(<username>,<password>);
ConnectionInfo connectionInfo = new ConnectionInfo(<host ip>, 22, <username>, kauth, pauth);
using (var ssh = new SshClient(connectionInfo))
{
ssh.Connect();
string command = "tmp/Ftp.sh";
var cmd = ssh.CreateCommand(command);
Console.WriteLine(cmd.Result);
var asynch = cmd.BeginExecute(delegate(IAsyncResult ar)
{
}, null);
var reader = new StreamReader(cmd.OutputStream);
while (!asynch.IsCompleted)
{
var result = reader.ReadToEnd();
if (string.IsNullOrEmpty(result))
continue;
Console.Write(result);
}
cmd.EndExecute(asynch);
}
}
cd命令执行成功,但当控件到达sudo命令时,应提示我在控制台上输入密码,但不幸的是,我没有。有人能告诉我如何使此交互,以便在我的应用程序控制台上提示我输入密码,然后我可以输入密码,然后将文件从复制到所需的位置我不知道您的SSHClient如何工作,但我猜您正在尝试在命令完成时读取输出。但命令未完成,因为它需要密码。您需要在调用命令之后但在其执行结束之前以某种方式发送密码。我不知道您的SSHClient类是否可以在这种情况下通知您。阅读手册。
#!/bin/bash
cd /apps/sample_folder
sudo -u (authorized_username) ftp (some_server)