Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to block a port for a specific time duration, say 1 am to 3 am. I can add a rule to built in firewall but this blocks the port all the time. Is there any way to do this for only a specif period of time ?

share|improve this question

2 Answers 2

No. It is not doable unless you make your own VBS script to automate with Task Scheduler. For which you need to implement Windows Firewall API. If oyu make a script post it. It would be of immense help to others.

share|improve this answer
    
+1 for controlling with Task Manager suggestion. It doesn't need to be VBS script though, PowerShell scripting would work, but easiest may be to use a couple batch files and Windows' netsh command. –  Ƭᴇcʜιᴇ007 Aug 19 '12 at 15:48

Finally I found a solution. First add a rule to block desired application in the firewall. Then the rule can be enabled or disabled using the command:

netsh advfirewall firewall set rule name="MyRule" new enable=yes

This can be added to a script that periodically checks time and enables/disables MuRule accordingly. I could not find sleep command in batch script and I don't know PowerShell so I wrote a simple c++ program.

#include<ctime>
#include<windows.h>
using namespace std;

int main()
{//code for hiding console
    HWND window; 
    AllocConsole();
    window = FindWindowA("ConsoleWindowClass", NULL);
    ShowWindow(window,0);

    Sleep(60*1000);// 1 min delay
    time_t now;
    struct tm *current;
    now = time(0);
    while(1)
    {
    current = localtime(&now);
    if(current->tm_hour>=22||current->tm_hour<=6) // 10 pm to 6 am
        system ("netsh advfirewall firewall set rule name=\"MyRule\" new enable=yes");
    else
        system ("netsh advfirewall firewall set rule name=\"MyRule\" new enable=no");
    Sleep(10*60*1000);// 10 min delay
    }
    return 0;
}

Compile using gcc and run at logon using Task Scheduler with Admin Privilege.

share|improve this answer
1  
I think there's some inconsistancy between 'MyRule' "MuRule" and "Murule" there. I'm not sure tho, I don't read C well enough –  Journeyman Geek Oct 2 '12 at 11:38
    
Corrected. I had different name in original program. Made mistake while changing name. –  Serious Oct 2 '12 at 11:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.