0

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 = "xxx.xxx.xxx.xxx";
string _username = "root";
string _password = "xxxxx";
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"

iop04329
  • 13
  • 6
  • See also [Providing input/subcommands to a command (cli) executed with SSH.NET SshClient.RunCommand](https://stackoverflow.com/q/57666090/850848). – Martin Prikryl Oct 24 '22 at 10:40
  • Side notes: `sshClient` needs a `using` to dispose it correctly, and you should probably consider using `async` `await` – Charlieface Oct 24 '22 at 11:15
  • NB: (1) I did not try it, but if this host/username/password combination is a real one, you just shared your root password with the world. I suggest you change it ASAP. (2) If you log in as root, there's no need to use sudo. – Heinzi Oct 24 '22 at 15:03
  • Hey if `runcommand()` no getting response can it setting the timeout ? Does it use `ConnectionInfo.Timeout` ? – iop04329 Oct 26 '22 at 06:03
  • @MartinPrikryl if use like "sudo ufw delete 3" to RunCommand and no response is there any way to give a timeout to run out of the code and give a timeout , because i try to add `sshClient.ConnectionInfo.Timeout = TimeSpan.FromSeconds(10);` it doesn't work , by the way this problem is i want to avoid the command that users use incorrect. – iop04329 Oct 26 '22 at 16:56

2 Answers2

1

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
Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • It is work ! Thanks for help @Heinzi , This way i would call an api to do so ,and add jwt token to avoid security problem . – iop04329 Oct 24 '22 at 14:46
0

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.

ItzDC2
  • 11
  • 5
  • Thanks for reply ,i try to use `SshCommand sshCmd = sshClient.RunCommand($"echo y");` And `sshCmd = sshClient.RunCommand($"sudo ufw delete 3");` , is still no response on second command. – iop04329 Oct 24 '22 at 09:41
  • 1
    What's not what ItzDC2 has suggested you to do. Though the solution by @Heizi is better anyway. – Martin Prikryl Oct 24 '22 at 10:39
  • I meant you to do something like this: SshCommand sshCmd = sshClient.RunCommand($"echo 'y' | sudo ufw delete 3"). You need the pipe operator in the same query. I also think like @MartinPrikryl, the Heinzi's answer is better than mine. – ItzDC2 Oct 24 '22 at 10:42
  • Thanks @ItzDC2 for give me another suggest i will try later and give you answer, i will use the better way to run my code . – iop04329 Oct 24 '22 at 14:48
  • @ItzDC2 the echo 'y' this way is works ! Thanks for help. – iop04329 Oct 26 '22 at 05:45