Boppity Bop
Boppity Bop

Reputation: 10481

Windows 10 script to enable/disable TCP port

I want to simulate Oracle disconnect for my app. I need to enable/disable outgoing traffic to port 1521 quickly.

I think the best way is to add/remove a rule into the Windows Firewall.

Is there a CMD or PowerShell scrip I can use? Or WMI in C#?

(Note: I cannot disable networking as a whole because there are different connections are used in my code and only 1521 needs to be disabled).

Upvotes: 5

Views: 8913

Answers (2)

Boppity Bop
Boppity Bop

Reputation: 10481

Thanks for Anas Atef, I had to roll the sleeves up. Here is what one needs in the situation like mine:

New-NetFirewallRule -DisplayName "Disabling Port 1521" -Action Block -Direction Outbound -Profile Any -Protocol tcp -RemotePort 1521
Set-NetFirewallRule -DisplayName "Disabling Port 1521" -Action Allow
Set-NetFirewallRule -DisplayName "Disabling Port 1521" -Action Block
Remove-NetFirewallRule -Name "{751a86cb-2ef3-4eba-8c95-68aa7e4bde18}"

New creates the rule. The display name is whatever you like.

Set then helps you to enable/disable the rule effectively.

Then use Remove once you are done testing. The Name in the Remove command is the guid which you get after running New (watch the output).

Upvotes: 4

Anas Atef
Anas Atef

Reputation: 11

Try This , This command will block Any outbound traffic from port 1521 for any app or tool :

New-NetFirewallRule -DisplayName "Disabling Port 1521" -Action Block -Direction Outbound -DynamicTarget Any -EdgeTraversalPolicy Block -Profile Any -Protocol tcp -RemotePort 1521

Upvotes: 1

Related Questions