我已经创建了一个简单的应用程序,它使用Renci.SshNet库来更新我的Fortigate设备上的防火墙配置。
var auth =
new AuthenticationMethod[] { new PasswordAuthenticationMethod(username, password) };
ConnectionInfo ConnNfo = new ConnectionInfo(server, 22, username, auth);
sshclient.Connect();
string command;
string addressname = "testaddress";
command = "config firewall address";
output.Add(command);
output.Add(sshclient.CreateCommand(command).Execute());
command = string.Format(@"edit {0}", addressName);
output.Add(command);
output.Add(sshclient.CreateCommand(command).Execute());
sshclient.Disconnect();我从输出中得到以下内容:
config firewall address
fw1 # fw1 (address) #
edit testaddress
fw1 # Unknown action 0 fw1 # 相同的命令可以在正常的SSH连接上正常工作。
fw1 # config firewall address
fw1 (address) # edit testaddress
new entry 'testaddress' added
fw1 (testaddress) #只是想知道我是否正确地使用它发送单独的CreateCommans.Execute()。
发布于 2017-11-28 23:49:41
如果我理解正确的话,edit testaddress是config firewall address命令的一个子命令。
而您的代码将其作为单独的顶级命令执行。
您必须将edit testaddress子命令提供给config firewall address命令的输入。但不幸的是,SSH.NET不支持使用CreateCommand接口提供输入。
您必须打开一个shell会话(否则不推荐使用这种方法来自动执行命令)。
使用SshClient.CreateShellStream或SshClient.CreateShell并将命令发送到其输入:
"config firewall address\nedit testaddress\n"有关示例代码,请参阅C# send Ctrl+Y over SSH.NET。
https://stackoverflow.com/questions/47534964
复制相似问题