如何解决C#plink基于StandardOutput执行SSH命令
我正在使用以下方法执行SSH命令,
public async Task RequestInfoAsync(string remoteHost,string userName,string password,string cmd)
{
string plink_path = Path.Combine(Application.StartupPath,"plink.exe");
ProcessStartInfo psi = new ProcessStartInfo()
{
FileName = plink_path,// A const or a readonly string that points to the plink executable
Arguments = String.Format("-ssh {0}@{1} -pw {2}",userName,remoteHost,password),RedirectStandardError = true,RedirectStandardOutput = true,RedirectStandardInput = true,UseShellExecute = false,CreateNoWindow = true
};
Process p = Process.Start(psi);
m_objLock = new Object();
m_blnDoRead = true;
AsyncReadFeedback(p.StandardOutput); // start the async read of stdout
AsyncReadFeedback(p.StandardError); // start the async read of stderr
StreamReader reader = p.StandardOutput;
using (StreamWriter strw = p.StandardInput)
{
await strw.WriteAsync("123456789\r");
await strw.WriteAsync("123456789\r");
await strw.WriteAsync("sys rst\r");
await strw.WriteAsync("y\r");
//await strw.WriteAsync("y\r");
//await strw.WriteAsync(cmd + "\r");
//await strw.WriteAsync("Y\r");
//}
strw.WriteLine("exit"); // send exit command at the end*/
}
MessageBox.Show("Feedback \n" + m_szFeedback);
p.WaitForExit(); // block thread until remote operations are done
}
如屏幕截图所示,有时(对于首次登录),用户必须将默认密码登录更改为SSH shell并执行命令。上面的代码在没有密码更改方案的情况下可以正常工作,但是当我尝试更改密码时,同样会失败。 如何使上述方法与SSH服务器交互并在需要时提供新密码。
谢谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。