2021-06-14 08:52:58 +03:00
|
|
|
# utils
|
2023-03-11 23:18:58 +03:00
|
|
|
|
2021-06-24 19:00:59 +03:00
|
|
|
Small tools needed to solve immediate tasks independently or as part of a project
|
|
|
|
|
2023-03-25 18:17:23 +03:00
|
|
|
* [`build-python`.sh](https://git.hmp.today/pavel.muhortov/utils#build-python-sh)
|
2021-06-24 19:00:59 +03:00
|
|
|
* [`cronutil`](https://git.hmp.today/pavel.muhortov/utils#cronutil)
|
|
|
|
* [`confutil`.py](https://git.hmp.today/pavel.muhortov/utils#confutil-py)
|
|
|
|
* [`sendmail`.py](https://git.hmp.today/pavel.muhortov/utils#sendmail-py)
|
2021-07-10 11:06:54 +03:00
|
|
|
* [`simplewc`.py](https://git.hmp.today/pavel.muhortov/utils#simplewc-py)
|
2021-06-23 01:10:06 +03:00
|
|
|
|
2021-06-24 18:30:44 +03:00
|
|
|
____
|
2023-03-11 23:18:58 +03:00
|
|
|
|
2023-03-25 18:17:23 +03:00
|
|
|
## `build-python`.sh
|
|
|
|
|
|
|
|
**Description:**
|
|
|
|
> Building Python from sources.
|
|
|
|
|
|
|
|
**Dependencies:**
|
|
|
|
>
|
|
|
|
> * [bash](https://www.gnu.org/software/bash/) (tested version 5.1.4 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/))
|
|
|
|
|
|
|
|
| POSITION | PARAMETERS | DESCRIPTION | DEFAULT |
|
|
|
|
|-----------|--------------|------------------------|---------------|
|
|
|
|
| 1 |**[qn]**|execution without pauses||
|
|
|
|
| 2 |**[version]**|version of Python|`3.9.5`|
|
|
|
|
| 3 |**[path/to/log]**|path to log|`/dev/null`|
|
|
|
|
|
|
|
|
Example usage in terminal with make the script executable:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
wget https://git.hmp.today/pavel.muhortov/utils/raw/branch/master/build-python.sh
|
|
|
|
chmod u+x ./build-python.sh
|
|
|
|
sudo ./build-python.sh qn 3.9.5 ./build-python.log
|
|
|
|
```
|
|
|
|
|
|
|
|
____
|
|
|
|
|
2021-06-24 19:00:59 +03:00
|
|
|
## `cronutil`
|
2023-03-11 23:18:58 +03:00
|
|
|
|
2023-03-25 18:17:23 +03:00
|
|
|
**Description:**
|
|
|
|
> Control wrapper for the [schedule](https://github.com/dbader/schedule) package.
|
2021-06-24 18:30:44 +03:00
|
|
|
|
2023-03-25 18:17:23 +03:00
|
|
|
**Dependencies:**
|
|
|
|
>
|
|
|
|
> * [Python 3](https://www.python.org/downloads/) (tested version 3.9.5 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/))
|
2021-06-24 18:30:44 +03:00
|
|
|
|
2023-03-25 18:17:23 +03:00
|
|
|
Scheduler works only on a weekly scale. This is due to the use of the [schedule](https://github.com/dbader/schedule) package.
|
|
|
|
Target format: wD:HH:MM:SS, where:
|
|
|
|
>
|
|
|
|
> wD - day of week unit: `'1'`, `'2'`, ... , `'6'`, `'7'`, `'*'`
|
|
|
|
> HH - hour unit in 24-hours format: `'00'`, `'01'`, ... , `'22'`, `'23'`, `'**'`
|
|
|
|
> MM - minute unit: `'00'`, `'01'`, ... , `'58'`, `'59'`, `'**'`
|
|
|
|
> SS - second unit: `'00'`, `'01'`, ... , `'58'`, `'59'`, `'**'`
|
|
|
|
|
|
|
|
Units can be listed separated by commas. Examples:
|
|
|
|
> `'*:**:**:**'` - every second of every minute of every hour of every day
|
|
|
|
> `'*:**:**:*5'` - every 05,15,25,35,45,55 seconds of every minute of every hour of every day
|
|
|
|
> `'*:2*:**:**'` - every second of every minute of every 20,21,22,23 hours of every day
|
|
|
|
> `'*:2*:**:*5'` - every 05,15,25,35,45,55 seconds of every minute of every 20,21,22,23 hours of every day
|
|
|
|
> `'3,5:2*:**:*5'` - every 05,15,25,35,45,55 seconds every minute every 20,21,22,23 hours of Wednesday, Friday
|
|
|
|
> `'1,7:12:00:**'` - every second 00 minutes 12 hours of Monday, Sunday
|
|
|
|
> `'1:07:00:00'` - every 00 seconds 00 minutes 07 hours Monday
|
2021-06-24 18:30:44 +03:00
|
|
|
|
|
|
|
Example usage in Python:
|
2023-03-11 23:18:58 +03:00
|
|
|
|
2021-06-24 18:30:44 +03:00
|
|
|
```Python
|
|
|
|
from time import strftime
|
|
|
|
from cronutil import Scheduler
|
|
|
|
|
|
|
|
|
|
|
|
def now():
|
|
|
|
print(strftime('%Y.%m.%d %H:%M:%S'))
|
|
|
|
|
|
|
|
|
|
|
|
cron = Scheduler()
|
|
|
|
cron.add('2,4:**:*0:00,15,30,45', now)
|
|
|
|
cron.start()
|
2021-06-25 09:21:25 +03:00
|
|
|
cron.add('2,4:**:59:59', cron.stop)
|
2021-06-24 18:30:44 +03:00
|
|
|
```
|
|
|
|
|
2021-06-15 15:34:01 +03:00
|
|
|
____
|
2023-03-11 23:18:58 +03:00
|
|
|
|
2021-06-24 19:00:59 +03:00
|
|
|
## `confutil`.py
|
2023-03-11 23:18:58 +03:00
|
|
|
|
2023-03-25 18:17:23 +03:00
|
|
|
**Description:**
|
|
|
|
> Parser of configs, arguments, parameters.
|
|
|
|
|
|
|
|
**Dependencies:**
|
|
|
|
>
|
|
|
|
> * [Python 3](https://www.python.org/downloads/) (tested version 3.9.5 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/))
|
2021-06-24 19:00:59 +03:00
|
|
|
|
|
|
|
Example config to parse:
|
2023-03-11 23:18:58 +03:00
|
|
|
|
2021-06-24 19:00:59 +03:00
|
|
|
```text
|
|
|
|
[main]
|
|
|
|
# This block contains basic parameters
|
|
|
|
|
|
|
|
|
|
|
|
[httpd]
|
|
|
|
# This block contains parameters for the http server
|
|
|
|
|
|
|
|
# Address to which to bind listening
|
|
|
|
#address=0.0.0.0;
|
|
|
|
# Port to which to bind listening. Port below 1024 requires root privileges.
|
|
|
|
port=8800;
|
|
|
|
# Working directory (available to everyone)
|
|
|
|
directory=www;
|
|
|
|
```
|
|
|
|
|
|
|
|
Example usage in Python:
|
2023-03-11 23:18:58 +03:00
|
|
|
|
2021-06-24 19:00:59 +03:00
|
|
|
```Python
|
|
|
|
from os import path
|
|
|
|
from confutil import Parse
|
|
|
|
|
|
|
|
conf = path.splitext(__file__)[0] + '.conf'
|
|
|
|
if path.exists(conf):
|
|
|
|
print(Parse(parameters=conf, block='httpd'))
|
|
|
|
```
|
|
|
|
|
|
|
|
____
|
2021-06-20 15:56:25 +03:00
|
|
|
|
2021-06-24 19:00:59 +03:00
|
|
|
## `sendmail`.py
|
2023-03-11 23:18:58 +03:00
|
|
|
|
2023-03-25 18:17:23 +03:00
|
|
|
**Description:**
|
|
|
|
> Sending email from Python.
|
|
|
|
|
|
|
|
**Dependencies:**
|
|
|
|
>
|
|
|
|
> * [Python 3](https://www.python.org/downloads/) (tested version 3.9.5 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/))
|
2021-06-20 15:56:25 +03:00
|
|
|
|
2021-06-15 15:34:01 +03:00
|
|
|
| PARAMETERS | DESCRIPTION | DEFAULT|
|
|
|
|
|-------------|-------------|--------|
|
|
|
|
|**-u**, **--user**|smtp valid user|**REQUIRED**|
|
|
|
|
|**-p**, **--pass**|smtp valid password|**REQUIRED**|
|
|
|
|
|**-d**, **--dest**|destination addresses|**REQUIRED**|
|
|
|
|
|**[-h]**|print help and exit||
|
|
|
|
|**[--smtp]**|smtp hostname or ip address|smtp.gmail.com|
|
|
|
|
|**[--port]**|smtp port number|587|
|
|
|
|
|**[--stls]**|smtp required TLS|`True`|
|
|
|
|
|**[--from]**|mail from alias|**--user** value|
|
|
|
|
|**[--subj]**|mail subject|'no subject'|
|
|
|
|
|**[--text]**|mail body text|'no text'|
|
|
|
|
|**[--type]**|mail body type: plain, html|plain|
|
|
|
|
|**[--file]**|mail attachment files|`None`|
|
2021-12-19 17:52:18 +03:00
|
|
|
|**[--time]**|minutes of attempts to send|3|
|
2021-06-16 20:20:35 +03:00
|
|
|
|
|
|
|
Example usage in terminal with Python:
|
2023-03-11 23:18:58 +03:00
|
|
|
|
2023-03-25 18:17:23 +03:00
|
|
|
```bash
|
2021-06-15 15:34:01 +03:00
|
|
|
python3 ./sendmail.py -u user@gmail.com -p pass -d addr1@gmail.com,addr2@gmail.com
|
|
|
|
```
|
2023-03-11 23:18:58 +03:00
|
|
|
|
2021-06-15 15:34:01 +03:00
|
|
|
Example usage in terminal with make the script executable:
|
2023-03-11 23:18:58 +03:00
|
|
|
|
2023-03-25 18:17:23 +03:00
|
|
|
```bash
|
2021-06-15 15:34:01 +03:00
|
|
|
chmod u+x ./sendmail.py
|
|
|
|
./sendmail.py -u user@gmail.com -p pass -d addr1@gmail.com,addr2@gmail.com
|
|
|
|
```
|
2023-03-11 23:18:58 +03:00
|
|
|
|
2021-06-15 15:34:01 +03:00
|
|
|
Example usage in Python:
|
2023-03-11 23:18:58 +03:00
|
|
|
|
2021-06-15 15:34:01 +03:00
|
|
|
```Python
|
|
|
|
from sendmail import Mail
|
|
|
|
|
|
|
|
msg = Mail(smtp_user='user@gmail.com', smtp_pass='pass', mail_dest='addr1@gmail.com,addr2@gmail.com')
|
|
|
|
log = msg.send()
|
|
|
|
print(log)
|
2021-06-16 20:20:35 +03:00
|
|
|
```
|
2021-06-20 19:23:02 +03:00
|
|
|
|
2021-06-16 20:20:35 +03:00
|
|
|
____
|
2023-03-11 23:18:58 +03:00
|
|
|
|
2021-07-10 11:06:54 +03:00
|
|
|
## `simplewc`.py
|
2023-03-11 23:18:58 +03:00
|
|
|
|
2023-03-25 18:17:23 +03:00
|
|
|
**Description:**
|
|
|
|
> Update Let's Encrypt wildcard certificate with DNS-01 challenge.
|
|
|
|
|
|
|
|
**Dependencies:**
|
|
|
|
>
|
|
|
|
> * [Python 3](https://www.python.org/downloads/) (tested version 3.9.5 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/))
|
|
|
|
> * installed or downloaded [acme.sh](https://github.com/Neilpang/acme.sh)
|
|
|
|
> * installed [dnspython](https://github.com/rthalley/dnspython) package
|
|
|
|
> * dns is supported to [dynamic update](https://en.wikipedia.org/wiki/Dynamic_DNS)
|
2021-07-10 11:06:54 +03:00
|
|
|
|
|
|
|
| PARAMETERS | DESCRIPTION | DEFAULT|
|
|
|
|
|-------------|-------------|--------|
|
|
|
|
|**--domain**|domain for which the wildcard certificate is issued|**REQUIRED**|
|
|
|
|
|**--server**|master server containing the domain zone|**REQUIRED**|
|
|
|
|
|**--keyname**|name of the key to update the zone|**REQUIRED**|
|
|
|
|
|**--keydata**|content of the key to update the zone|**REQUIRED**|
|
|
|
|
|**[-h]**|print help and exit||
|
|
|
|
|**[--acmepath]**|alternative path to bin (example: ~/.acme.sh/acme.sh)|`None`|
|
|
|
|
|**[--force]**|"force" argument for the acme.sh|`False`|
|
|
|
|
|**[--test]**|"test" argument for the acme.sh|`False`|
|
|
|
|
|
|
|
|
Example usage in cron with Python:
|
2023-03-11 23:18:58 +03:00
|
|
|
|
2023-03-25 18:17:23 +03:00
|
|
|
```bash
|
2021-07-10 11:06:54 +03:00
|
|
|
# at 00:00 on Monday
|
|
|
|
0 0 * * 1 /usr/bin/python3 ~/simplewc.py --domain EXAMPLE.COM --server 8.8.8.8 --keyname KEY --keydata YOU_KEY_CONTENT > /dev/null
|
2022-04-19 16:08:19 +03:00
|
|
|
# 00:00 on day-of-month 1 and 15
|
|
|
|
0 0 1,15 * * /usr/bin/python3 ~/simplewc.py --domain EXAMPLE.COM --server dyn.dns.he.net --keyname - --keydata YOU_DDNSKEY > /dev/null
|
2023-03-11 23:18:58 +03:00
|
|
|
|
2021-07-10 11:06:54 +03:00
|
|
|
```
|
2023-03-11 23:18:58 +03:00
|
|
|
|
2021-07-10 11:06:54 +03:00
|
|
|
Example usage in terminal with make the script executable:
|
2023-03-11 23:18:58 +03:00
|
|
|
|
2023-03-25 18:17:23 +03:00
|
|
|
```bash
|
2021-07-10 11:06:54 +03:00
|
|
|
chmod u+x ./simplewc.py
|
|
|
|
./simplewc.py --domain EXAMPLE.COM --server 8.8.8.8 --keyname KEY --keydata YOU_KEY_CONTENT --test --force
|
2022-04-19 16:08:19 +03:00
|
|
|
./simplewc.py --domain EXAMPLE.COM --server dyn.dns.he.net --keyname - --keydata YOU_DDNSKEY --test --force
|
2021-07-10 11:06:54 +03:00
|
|
|
```
|
|
|
|
|
|
|
|
____
|