first commit

This commit is contained in:
TheFlipside88 2022-10-02 12:43:53 +02:00
commit 87492a7afb
6 changed files with 367 additions and 0 deletions

15
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
}

4
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,4 @@
{
"python.linting.pycodestyleEnabled": true,
"python.linting.enabled": true
}

78
login-log.py Normal file
View File

@ -0,0 +1,78 @@
import os
from datetime import date
from datetime import datetime
# Determine if the OS is MS Windows
if os.name == 'nt':
# Set the location of the file to write to, in the user Documents directory
logfile = os.path.join(os.path.join(os.environ['USERPROFILE']),
r'Documents\login.log')
# For OS which are not MS Windows
else:
logfile = os.path.join(os.path.join(os.environ['HOME']),
r'Documents\login.log')
# Create the file if it does not exist yet
if not os.path.exists(logfile):
with open(logfile, 'w'):
pass
# Determine the current date and time
today = date.today()
now = datetime.now()
current_day = today.strftime("%d.%m.%Y")
current_time = now.strftime("%H:%M:%S")
# Determine the client hostname
client_hostname = os.getenv("CLIENTNAME")
# Class for prepending the information to the log file
class Prepender(object):
def __init__(self,
file_path,
):
# Read in the existing file, so we can write it back later
with open(file_path, mode='r') as f:
self.__write_queue = f.readlines()
self.__open_file = open(file_path, mode='w')
def write_line(self, line):
self.__write_queue.insert(0,
"%s\n" % line,
)
def write_lines(self, lines):
lines.reverse()
for line in lines:
self.write_line(line)
def close(self):
self.__exit__(None, None, None)
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
if self.__write_queue:
self.__open_file.writelines(self.__write_queue)
self.__open_file.close()
# Prepend the determined information to the top of the log file
with Prepender(logfile) as f:
# Must write individual lines in reverse order
f.write_line(current_day+' - '+current_time+' - '+client_hostname)
# with Prepender(logfile) as f:
# Or, use write_lines instead - that maintains order.
# f.write_lines(
# ['This will be line 1',
# 'This will be line 2',
# 'This will be line 3',
# ]
# )

70
logoff-log.py Normal file
View File

@ -0,0 +1,70 @@
import os
from datetime import date
from datetime import datetime
# Set the location of the file to write to, in the user Documents directory
logfile = os.path.join(os.path.join(os.environ['USERPROFILE']),
r'Documents\logoff.log')
# Create the file if it does not exist yet
if not os.path.exists(logfile):
with open(logfile, 'w'):
pass
# Determine the current date and time
today = date.today()
now = datetime.now()
current_day = today.strftime("%d.%m.%Y")
# print("Today's date:", current_day)
current_time = now.strftime("%H:%M:%S")
# print("Current Time =", current_time)
# Class for prepending the date and time to the log file
class Prepender(object):
def __init__(self,
file_path,
):
# Read in the existing file, so we can write it back later
with open(file_path, mode='r') as f:
self.__write_queue = f.readlines()
self.__open_file = open(file_path, mode='w')
def write_line(self, line):
self.__write_queue.insert(0,
"%s\n" % line,
)
def write_lines(self, lines):
lines.reverse()
for line in lines:
self.write_line(line)
def close(self):
self.__exit__(None, None, None)
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
if self.__write_queue:
self.__open_file.writelines(self.__write_queue)
self.__open_file.close()
# Prepend the determined date and time to the top of the log file
with Prepender(logfile) as f:
# Must write individual lines in reverse order
f.write_line(current_day+' - '+current_time)
# with Prepender(logfile) as f:
# Or, use write_lines instead - that maintains order.
# f.write_lines(
# ['This will be line 1',
# 'This will be line 2',
# 'This will be line 3',
# ]
# )

30
sendmail.py Normal file
View File

@ -0,0 +1,30 @@
import smtplib
import ssl
smtp_server = "mail.example.com"
port = 465 # For SSL
sender_email = "user01@example.com"
password = "smtpPassw0rd"
# password = input("Type your password and press enter: ") # Interactive
receiver_email = "user02@example.com"
message = """\
From: user01@example.com
Subject: Test Email
This message is sent from Python."""
# Create a secure SSL context
context = ssl.create_default_context()
# Try to log in to server and send email
try:
server = smtplib.SMTP_SSL(smtp_server, port, context=context)
server.ehlo() # Can be omitted
server.login(sender_email, password)
# Send email
server.sendmail(sender_email, receiver_email, message)
except Exception as e:
# Print any error messages to stdout
print(e)
finally:
server.quit()

170
wol.py Normal file
View File

@ -0,0 +1,170 @@
#!/usr/bin/env python3
"""
https://github.com/remcohaszing/pywakeonlan
A small python module for wake on lan.
For more information on the wake on lan protocol
please take a look at Wikipedia.
http://en.wikipedia.org/wiki/Wake-on-LAN
Usage
To wake up a computer using wake on lan it must first be enabled in the BIOS
settings. Please note the computer you are trying to power on does not have an
ip address, but it does have a mac address.
The package needs to be sent as a broadcast package.
As a python module
Import the module
>>> from wakeonlan import send_magic_packet
Wake up a single computer by its mac address
>>> send_magic_packet('ff.ff.ff.ff.ff.ff')
Wake up multiple computers by their mac addresses.
>>> send_magic_packet('ff.ff.ff.ff.ff.ff', '00-00-00-00-00-00',
... 'FFFFFFFFFFFF')
An external host may be specified.
Do note that port forwarding on that host is required.
The default ip address is 255.255.255.255 and the default port is 9.
>>> send_magic_packet('ff.ff.ff.ff.ff.ff',
... ip_address='example.com',
... port=1337)
A network adapter may be specified.
The magic packet will be routed through this interface.
>>> send_magic_packet('ff.ff.ff.ff.ff.ff',
... interface='192.168.0.2')
As a standalone script
usage:
wakeonlan [-h] [-i ip] [-p port] [-n interface] mac address [mac address ...]
Wake one or more computers using the wake on lan protocol.
positional arguments:
mac address The mac addresses of the computers you are trying to wake.
optional arguments:
-h, --help show this help message and exit
-i ip The ip address of the host to send the magic packet to.
(default 255.255.255.255)
-p port The port of the host to send the magic packet to. (default 9)
-n interface The ip address of the network adapter to route the
magic packet through. (optional)
Dependencies
Python3.x
License
MIT
"""
import argparse
import socket
from typing import List
BROADCAST_IP = "255.255.255.255"
DEFAULT_PORT = 9
def create_magic_packet(macaddress: str) -> bytes:
"""
Create a magic packet.
A magic packet is a packet that can be used with the wake on lan protocol
to wake up a computer. The packet is constructed from the mac address
given as a parameter.
Args:
macaddress: the mac address that should be parsed into a magic packet.
"""
if len(macaddress) == 17:
sep = macaddress[2]
macaddress = macaddress.replace(sep, "")
elif len(macaddress) != 12:
raise ValueError("Incorrect MAC address format")
return bytes.fromhex("F" * 12 + macaddress * 16)
def send_magic_packet(
*macs: str,
ip_address: str = BROADCAST_IP,
port: int = DEFAULT_PORT,
interface: str = None
) -> None:
"""
Wake up computers having any of the given mac addresses.
Wake on lan must be enabled on the host device.
Args:
macs: One or more macaddresses of machines to wake.
Keyword Args:
ip_address: the ip address of the host to send the magic packet to.
port: the port of the host to send the magic packet to.
interface: the ip address of the network adapter to route the
magic packet through.
"""
packets = [create_magic_packet(mac) for mac in macs]
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
if interface is not None:
sock.bind((interface, 0))
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.connect((ip_address, port))
for packet in packets:
sock.send(packet)
def main(argv: List[str] = None) -> None:
"""
Run wake on lan as a CLI application.
"""
parser = argparse.ArgumentParser(
description="Wake one or more computers using the \
wake on lan protocol.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"macs",
metavar="mac address",
nargs="+",
help="The mac addresses of the computers you are trying to wake.",
)
parser.add_argument(
"-i",
metavar="ip",
default=BROADCAST_IP,
help="The ip address of the host to send the magic packet to.",
)
parser.add_argument(
"-p",
metavar="port",
type=int,
default=DEFAULT_PORT,
help="The port of the host to send the magic packet to.",
)
parser.add_argument(
"-n",
metavar="interface",
default=None,
help="The ip address of the network adapter to route the \
magic packet through.",
)
args = parser.parse_args(argv)
send_magic_packet(*args.macs,
ip_address=args.i,
port=args.p,
interface=args.n)
if __name__ == "__main__": # pragma: nocover
main()