1

I've created a simple app which uses the Renci.SshNet library to update firewall configuration on my Fortigate device.

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();

I get the following from the output:

config firewall address
fw1 # fw1 (address) # 
edit testaddress
fw1 # Unknown action 0 fw1 # 

The same commands work fine over a normal SSH connection.

fw1 # config firewall address
fw1 (address) # edit testaddress
new entry 'testaddress' added
fw1 (testaddress) #

Just wondering if I'm using it correctly sending separate CreateCommans.Execute().

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Ben
  • 609
  • 6
  • 21

1 Answers1

0

If I understand it correctly, the edit testaddress is a subcommand of the config firewall address command.

While your code executes it as a separate top-level command.

You have to feed the edit testaddress subcommand to the input of the config firewall address command. But SSH.NET unfortunately does not support providing an input with the CreateCommand interface.

You have to open a shell session (what is otherwise a not recommended approach for automating a command execution).

Use SshClient.CreateShellStream or SshClient.CreateShell and send the commands to its input:

"config firewall address\nedit testaddress\n"

For a sample code see C# send Ctrl+Y over SSH.NET.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992