Home > Software engineering >  How to using Ssh.net to run a command "sudo ufw delete 3" and give a next command "y&
How to using Ssh.net to run a command "sudo ufw delete 3" and give a next command "y&

Time:10-24

I need to execute the delete IP command ("sudo ufw delete 3") but after sending Ssh.net's RunCommand or CreateCommand & Execute, there will be no response and let me execute the next step, so I can't execute the command Y to delete, if I use putty it is It can be done by executing y after executing sudo ufw delete 3. By the way, is it possible to delete the specified IP.

string _host = "202.182.113.226";
string _username = "root";
string _password = "5r Q-HdbZ.M@tt?{";
int _port = 22;
SshClient sshClient = new SshClient(_host,_port,_username,_password);
if (!sshClient.IsConnected)
{
    sshClient.Connect();
}
SshCommand sshCmd = sshClient.RunCommand($"sudo ufw delete 3");   <--- no response
sshCmd = sshClient.RunCommand($"y");
sshClient.Disconnect();
sshClient.Dispose()

I have test RunCommand on

SshCommand sshCmd = sshClient.RunCommand($"sudo ufw allow from {_IP} to any port 22");

This can work.But it doesnt need press "Y"

CodePudding user response:

Instead of sending "y", just tell ufw not to ask for confirmation:

sudo ufw --force delete 3

Do note, though, that what you are doing is quite dangerous: You delete the third firewall rule, whatever that rule may be. Unless you are absolutely sure that the rule currently at the third position is the one you want to delete, you seriously risk removing some random rule and either opening a security hole in your firewall or locking yourself out.

A better alternative would be to refer to the rule by content rather than by number, e.g.

ufw delete allow 443     # removes the rule that allows access to port 443

CodePudding user response:

I think there're 2 ways to do it, get the one you're interested in:

  1. echo "y" | sudo ufw delete 3
  2. yes | sudo ufw delete 3

Try that and let met know. Greetings.

  • Related