-1

I'm trying to write a program utilizing the os.system() method to call CMD commands, but I'm a little lost. I wanted to have it check if the firewall is enabled every couple of minutes, and if it isn't, send the command to enable it. Would I have it output to a txt file and then loop through the file to check for keywords and once those keywords are found it executes the turn firewall on command in CMD?

I'm also not sure if it's best to create the program and then link it in task scheduler or to have the program run in a loop.

I also wanted to do the same thing where it sends the command to check the users in CMD, if any were, all the account passwords are changed.

I was also looking into writing it utilizing subprocess. Any suggestions on the best method or way to go about this? I'm using python 3.5.

Lukas
  • 446
  • 3
  • 11
  • Once you have written a program to detect and send the command if needed, it can be scheduled on the Windows Scheduler to run every two (2) minutes. – lit Oct 09 '18 at 20:23

1 Answers1

0

example for subprocess.check_output:

import subprocess          
cmd = "netsh advfirewall show allprofiles"

# returns output as byte string
returned_output = subprocess.check_output(cmd)
# print(type(returned_output))
# using decode() function to convert byte string to string
print('CMD OUTPUT:' + returned_output.decode("utf-8", "ignore")) 
# I HAVE CMD ON GERMAN LANGUAGE
# EIN / AUS [GER] for ON / OFF in Domainprofile:
print('CMD OUTPUT: ' + returned_output.decode("utf-8", "ignore").split("Status")[1].split("Firewallrichtlinie")[0].strip())

if returned_output.decode("utf-8", "ignore").split("Status")[1].split("Firewallrichtlinie")[0].strip() == "AUS":
    # set FIREWALL ON
else:
    # everything ok :)

I think "task scheduler" is the best way to automate the Python process. Look at this answer: Scheduling a .py file on Task Scheduler in Windows 10

By the way for me (set windows german), i have umlauts, so i use script like this:

import subprocess
import sys
import locale

cmd = "netsh advfirewall show allprofiles"

# returns output as byte string
returned_output = subprocess.check_output(cmd)

# using decode() function to convert byte string to string
print('CMD OUTPUT: ' + returned_output.decode("cp850", "replace"))
# I HAVE CMD ON GERMAN LANGUAGE
# EIN / AUS [GER] for ON / OFF in Domainprofile:
print('CMD OUTPUT: ' + returned_output.decode("cp850", "replace").split("Status")[1].split("Firewallrichtlinie")[0].strip())

if returned_output.decode("utf-8", "replace").split("Status")[1].split("Firewallrichtlinie")[0].strip() == "AUS":
    # set FIREWALL ON ALL PROFILES
    cmd = "netsh advfirewall set allprofiles state on"
    returned_output = subprocess.check_output(cmd)
    print(returned_output.decode("cp850", "replace"))
else:
    # everything ok :)
    sys.exit()

YOU MAY HAVE TO ADAPT the encoding at the decode to your local windows look at this: Python subprocess check_output decoding specials characters

Lukas
  • 446
  • 3
  • 11