我正在使用以下 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 命令时,应该在控制台上提示我输入密码,但不幸的是我没有。有人可以告诉我如何进行此交互,以便在我的应用程序控制台上提示我输入密码,我可以输入密码,然后将文件从所需位置复制