ssh.net 与服务器保持对话
ssh.net maintain conversation with server
我希望使用 ssh.net
与远程服务器保持对话
我正在做的是连接到主机,发送一个命令,比如更改目录...到除根目录之外的某个目录。将结果值存储为全局值。
然后我通过 RunCommand() 发送另一个命令来检查当前目录...
发生的事情是,我正在获取根目录,而不是我刚刚在初始 运行 命令中更改到的目录。
似乎正在发生的事情是,虽然与服务器的连接保持打开状态,但我以某种方式重置了终端会话,从而失去了我与服务器的对话。
有谁知道如何使用 ssh.net 保持与远程服务器的对话,以便我可以发送多个命令并保持状态?
例如命令 1 = cd/somedir
命令 2 = pwd,命令 2 的结果是 /somedir
例如命令 1 = cd/somedir 命令 2 = pwd 命令 2 的结果是 /somedir
你的例子似乎很好。但我认为,您希望更改目录和 运行 该目录中的第二个命令。
服务器连接是到服务器的 ssh 隧道,它不会启动 shell。 RunCommand() 创建一个 shell 和 运行s 命令,第二个 RunCommand 也创建一个新的 shell 和 运行s 命令,因此更改目录不会在命令之间持续存在。
建立连接后,使用 ShellStream 创建一个 shell,您可以从中发送和接收交互式命令。
来自 codeplex 的示例:
string command = "your command";
using (var ssh = new SshClient(connectionInfo))
{
ssh.Connect();
string reply = String.Empty;
try
{
using (var shellStream = ssh.CreateShellStream("dumb", 0, 0, 0, 0, BUFFERSIZE))
{
// Wait for promt for 10 seconds, if time expires, exception is thrown
reply = shellStream.Expect(new Regex(@":.*>"), new TimeSpan(0, 0, 10));
shellStream.WriteLine(command);
// Wait for Read for 10 seconds, if time expires, exception is thrown
string result = shellStream.ReadLine(new TimeSpan(0, 0, 10));
}
}
catch
{
// Do something
}
}
我希望使用 ssh.net
与远程服务器保持对话我正在做的是连接到主机,发送一个命令,比如更改目录...到除根目录之外的某个目录。将结果值存储为全局值。
然后我通过 RunCommand() 发送另一个命令来检查当前目录...
发生的事情是,我正在获取根目录,而不是我刚刚在初始 运行 命令中更改到的目录。
似乎正在发生的事情是,虽然与服务器的连接保持打开状态,但我以某种方式重置了终端会话,从而失去了我与服务器的对话。
有谁知道如何使用 ssh.net 保持与远程服务器的对话,以便我可以发送多个命令并保持状态?
例如命令 1 = cd/somedir 命令 2 = pwd,命令 2 的结果是 /somedir
例如命令 1 = cd/somedir 命令 2 = pwd 命令 2 的结果是 /somedir
你的例子似乎很好。但我认为,您希望更改目录和 运行 该目录中的第二个命令。
服务器连接是到服务器的 ssh 隧道,它不会启动 shell。 RunCommand() 创建一个 shell 和 运行s 命令,第二个 RunCommand 也创建一个新的 shell 和 运行s 命令,因此更改目录不会在命令之间持续存在。
建立连接后,使用 ShellStream 创建一个 shell,您可以从中发送和接收交互式命令。
来自 codeplex 的示例:
string command = "your command";
using (var ssh = new SshClient(connectionInfo))
{
ssh.Connect();
string reply = String.Empty;
try
{
using (var shellStream = ssh.CreateShellStream("dumb", 0, 0, 0, 0, BUFFERSIZE))
{
// Wait for promt for 10 seconds, if time expires, exception is thrown
reply = shellStream.Expect(new Regex(@":.*>"), new TimeSpan(0, 0, 10));
shellStream.WriteLine(command);
// Wait for Read for 10 seconds, if time expires, exception is thrown
string result = shellStream.ReadLine(new TimeSpan(0, 0, 10));
}
}
catch
{
// Do something
}
}