This is how I ended up solving the problem..
Note: I try 5 times so that this script can be put in startup. Without the retry, the script may run before the firewall has initialized.
use strict;
my $count=0;
my $ok=0;
my $progname=shift || usage();
my $progexe=shift || usage();
while(1){
$ok=®isterFirewall($progname,$progexe);
last if $ok;
last if $count>5;
$count++;
sleep(5);
}
if(!$ok){die "Unable to register with windows firewall.\n";}
###############
sub usage{
print "$0 Name PathToExe\n";
print "\tRegisters program Name found at PathToExe with Windows Fi
+rewall\n";
exit;
}
###############
sub registerFirewall{
my $name=shift || return "No Name";
my $path=shift || return "No Path";
return "$path does not exist" if !-s $path;
my $cmd=qq|netsh firewall add allowedprogram "program=$path" "name
+=$name"|;
#print "$cmd\n";
my @tmp=`$cmd`;
my $result=join('',@tmp);
if($result=~/ok/is){return 1;}
print "registerFirewall Error: $result\n";
return 0;
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.