如何解决C#SSH文本,数字发送?
脚本运行时,要我输入数字,卡在命令2上,“ SshCommand sc3”未运行命令3,
脚本运行时,您要运行多长时间?她问。他要我输入数字 但是我无法发送该号码,也没有发送到那一行。
using (var client = new SshClient(host,user,pass))
{
client.Connect();
SshCommand sc1 = client.CreateCommand("chmod +x xxx.sh");
SshCommand sc2 = client.CreateCommand("./xxx.sh");
SshCommand sc3 = client.CreateCommand("echo 500");
SshCommand sc4 = client.CreateCommand("500");
client.Disconnect();
}
解决方法
我对您的流程的理解是,您chmod
并启动一个脚本,将一些输入传递给它,然后等待它完成。如果我的问题陈述正确无误,那么使用ShellStream
可能会更好。这样,您的互动就成为一种练习或确定命令并Expecting
做出一定的反应:
/*
#i used the following script on the other side to simulate activity:
#!/bin/bash
echo "Input time to sleep:"
read timeToSleep
sleep $timeToSleep
echo "We are done"
*/
using (var client = new SshClient(host,user,pass))
{
client.Connect();
var shell = client.CreateShellStream("bash",80,50,1024,1024); // I believe shell name is critical to get your control character sequences parsing right
shell.Expect(new ExpectAction("#",(_) => // we wait for shell prompt here. if your shell is different you might need to tweak the regex
{
shell.WriteLine("chmod +x xxx.sh && ./xxx.sh");
}));
shell.Expect(new ExpectAction("Input time to sleep:",(_) => {
shell.WriteLine("5");
}));
shell.Expect(new ExpectAction("#",scriptOutput => {
Console.WriteLine(scriptOutput);
}));
client.Disconnect();
}
由于您还没有共享运行的实际脚本,因此我无法更具体地说明。希望这能使您步入正轨,充实细节。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。