无法在linux框C#上执行脚本

时间:2013-06-12 11:12:25

标签: c# linux ssh

我使用以下C#代码连接到Linux机器并执行放在那里的脚本

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);

    }

}

我的Ftp.sh脚本如下:

#!/bin/bash
cd /apps/sample_folder

sudo -u (authorized_username) ftp (some_server)

cd命令成功执行但是当控件到达sudo命令时,我应该在控制台上提示输入密码,但不幸的是我没有。有人可以告诉我如何使这个交互式,以便在我的应用程序控制台上提示我输入密码,然后我可以输入密码然后将文件复制到所需的位置

0 个答案:

没有答案