-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5810e38
commit d3a9148
Showing
12 changed files
with
234 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/etc/start-stop/start-stop.conf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Package: start-stop | ||
Version: 2.0 | ||
Architecture: amd64 | ||
Depends: wakeonlan, samba-common, python3 | ||
Maintainer: https://github.com/felipeucelli | ||
Description: Turn on and shutdown machines on the local network |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/bin/bash | ||
|
||
set -e | ||
|
||
systemctl daemon-reload | ||
|
||
service start-stop start | ||
|
||
systemctl enable start-stop.service | ||
|
||
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/bin/bash | ||
|
||
set -e | ||
|
||
systemctl daemon-reload | ||
|
||
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/bin/bash | ||
|
||
set -e | ||
|
||
service start-stop stop | ||
systemctl disable start-stop.service | ||
|
||
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[LOG] | ||
path=/var/log/start-stop.log | ||
|
||
[PC1] | ||
ip=192.168.0.2 | ||
username=user | ||
password=pass | ||
mac=00:00:00:00:00:00 | ||
time_shutdown=21:00:00 | ||
time_wake=07:30:00 | ||
day_shutdown=0,1,2,3,4,5,6 | ||
day_wake=0,1,2,3,4,5,6 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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('/etc/start-stop/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:])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[Unit] | ||
Description="Turn on and shutdown machines on the local network" | ||
After=network.target | ||
|
||
[Service] | ||
Type=simple | ||
ExecStart=/bin/python3 /etc/start-stop/start-stop.py | ||
TimeoutStartSec=0 | ||
|
||
[Install] | ||
WantedBy=default.target | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
[LOG] | ||
path=/var/log/start-stop.log | ||
|
||
[PC1] | ||
ip=192.168.0.2 | ||
username=user | ||
password=pass | ||
mac=00:00:00:00:00:00 | ||
time_shutdown=21:00:00 | ||
time_wake=07:30:00 | ||
day_shutdown=0,1,2,3,4,5,6 | ||
day_wake=0,1,2,3,4,5,6 | ||
|
||
[PC2] | ||
ip=192.168.0.3 | ||
username=user | ||
password=pass | ||
mac=00:00:00:00:00:00 | ||
time_shutdown=21:00:00 | ||
time_wake=07:30:00 | ||
day_shutdown=0,1,2,3,4,5,6 | ||
day_wake=0,1,2,3,4,5,6 |
Oops, something went wrong.