Skip to content

Commit

Permalink
Added config file
Browse files Browse the repository at this point in the history
  • Loading branch information
felipeucelli committed Mar 26, 2023
1 parent 5810e38 commit d3a9148
Show file tree
Hide file tree
Showing 12 changed files with 234 additions and 166 deletions.
90 changes: 25 additions & 65 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,80 +4,40 @@ A simple script that turns Windows machines off and on automatically
## Description
The script was built to run inside a linux machine for shutdown and wake up Windows machines on the local network.

## Quickstart
This guide covers faster script usage.

### Installation
## Installation
start-stop requires an installation of Python3 or greater. And run on a linux machine with some packages installed

Installing samba package with apt
```bash
$ sudo apt install samba
```

Installing wakeonlan package with apt
Installing packages with apt
```bash
$ sudo apt install wakeonlan
$ sudo apt install samba-common wakeonlan python3
```

### Windows configuration
* You must have a username with a password
* Open regedit and go to: `\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System`
* create the file DWORD (32 bits): `LocalAccountTokenFilterPolicy` and set its value to `1`
* Configure your motherboard to wake up with magic packages

### Configuring the script
It is necessary to configure the script before starting to use it.

Edit the parameters below as per your need
```python
# IP address and username and password of Windows machines
# Username and password syntax: 'username%password'
_address = [
('192.168.0.105', 'user1%password1'),
('192.168.0.106', 'user2%password2')
]
## Windows configuration
* You must have a username with a password.
* Open regedit and go to: `\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System`.
* create the file DWORD (32 bits): `LocalAccountTokenFilterPolicy` and set its value to `1`.
* Configure your motherboard to wake up with magic packages.

# MAC address of Windows machines
_mac = [
'00:00:00:00:00:00',
'11:11:11:11:11:11'
]
## Configuration file

# Shutdown time
_time_shutdown = [
'21:00:00',
'22:00:00'
]
Change **start-stop.conf** parameters as needed.

# Monday: 0, Tuesday: 1, Wednesday: 2, Thursday: 3, Friday: 4, Saturday: 5, Sunday: 6
_week_shutdown = [
0, # Monday
1, # Tuesday
2, # Wednesday
3, # Thursday
4, # Friday
5, # Saturday
6, # Sunday
]
You can create more than one machines session.

# Wake up on lan time
_time_wakeonlan = [
'21:30:00',
'07:30:00'
]
```text
[LOG]
path=/var/log/start-stop.log
# Monday: 0, Tuesday: 1, Wednesday: 2, Thursday: 3, Friday: 4, Saturday: 5, Sunday: 6
_week_wakeonlan = [
0, # Monday
1, # Tuesday
2, # Wednesday
3, # Thursday
4, # Friday
5, # Saturday
6, # Sunday
]
[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
```

## Observation
wakeonlan doesn't handle machines that are outside the local network very well.
# Observation
Wakeonlan doesn't handle machines that are outside the local network very well.
Binary file added deb/start-stop.deb
Binary file not shown.
1 change: 1 addition & 0 deletions deb/start-stop/DEBIAN/conffiles
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/etc/start-stop/start-stop.conf
6 changes: 6 additions & 0 deletions deb/start-stop/DEBIAN/control
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
11 changes: 11 additions & 0 deletions deb/start-stop/DEBIAN/postinst
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
7 changes: 7 additions & 0 deletions deb/start-stop/DEBIAN/postrm
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

set -e

systemctl daemon-reload

exit 0
8 changes: 8 additions & 0 deletions deb/start-stop/DEBIAN/prerm
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
13 changes: 13 additions & 0 deletions deb/start-stop/etc/start-stop/start-stop.conf
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

88 changes: 88 additions & 0 deletions deb/start-stop/etc/start-stop/start-stop.py
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:]))
13 changes: 13 additions & 0 deletions deb/start-stop/etc/systemd/system/start-stop.service
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


22 changes: 22 additions & 0 deletions start-stop.conf
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
Loading

0 comments on commit d3a9148

Please sign in to comment.