-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart-stop.py
88 lines (66 loc) · 2.32 KB
/
start-stop.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# -*- coding: utf-8 -*-
# @autor: Felipe Ucelli
# @github: github.com/felipeucelli
# Built-in
import logging
import subprocess
import configparser
from time import sleep
from datetime import datetime, date
def shutdown(ip: str, username: str, password: str):
"""
Turn off the machines
:return:
"""
try:
result = subprocess.Popen(
['net', 'rpc', 'shutdown', '-I', ip, '-U', username + '%' + password],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
result_stdout = result.stdout.read().decode()
result_stderr = result.stderr.read().decode()
if result_stdout != '':
logging.info(f'{result_stdout} - {ip}')
else:
logging.error(f'{result_stderr}')
except Exception as error:
logging.critical(error)
def wakeonlan(mac: str):
"""
Turn on the machines using Wake up on Lan
:param mac: List of macs of machines to be connected
:return:
"""
try:
result = subprocess.Popen(
['wakeonlan', mac],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
result_stdout = result.stdout.read().decode()
result_stderr = result.stderr.read().decode()
logging.info(f'{result_stdout}') if result_stdout != '' else logging.error(f'{result_stderr}')
except Exception as error:
logging.critical(error)
def main(section: list):
"""
Checks the time list and calls the corresponding function when it is time
:return:
"""
while True:
now = str(datetime.now().time()).split('.')[0]
day = str(date(date.today().year, date.today().month, date.today().day).weekday())
sleep(1)
for items in section:
item = dict(config.items(items))
if now in item['time_shutdown'] and day in item['day_shutdown']:
shutdown(item['ip'], item['username'], item['password'])
if now in item['time_wake'] and day in item['day_wake']:
wakeonlan(item['mac'])
config = configparser.RawConfigParser()
config.read('start-stop.conf')
log_format = '%(asctime)s - %(levelname)s : %(message)s'
log_path = config.get('LOG', 'path')
logging.basicConfig(
filename=log_path,
level=logging.DEBUG,
format=log_format)
if __name__ == '__main__':
main(list(config.sections()[1:]))