# utils Small tools needed to solve immediate tasks independently or as part of a project * [`build-python`.sh](https://git.hmp.today/pavel.muhortov/utils#build-python-sh) * [`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) * [`simplewc`.py](https://git.hmp.today/pavel.muhortov/utils#simplewc-py) * [`ovpn-client-management`.sh](https://git.hmp.today/pavel.muhortov/utils#ovpn-client-management-sh) * [`ovpn-connect-handling`.sh](https://git.hmp.today/pavel.muhortov/utils#ovpn-connect-handling-sh) * [`wg-connect-handling`.sh](https://git.hmp.today/pavel.muhortov/utils#wg-connect-handling-sh) ____ ## `build-python`.sh **Description:** > Building Python from sources. **Dependencies:** > > * [bash](https://www.gnu.org/software/bash/) (tested versions: 5.1.4 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/), 5.0.17 on [Ubuntu 20](https://wiki.ubuntu.com/FocalFossa/ReleaseNotes), 4.2.46 on [CentOS 7](https://wiki.centos.org/Manuals/ReleaseNotes/CentOS7.2009)) | 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 - 3.9.5 ``` Example usage in terminal without download: ```bash sudo su - -c "bash <(curl -s https://git.hmp.today/pavel.muhortov/utils/raw/branch/master/build-python.sh) qn 3.9.5 install-python.log" ``` ____ ## `cronutil` **Description:** > Control wrapper for the [schedule](https://github.com/dbader/schedule) package. **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/)) 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 Example usage in Python: ```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() cron.add('2,4:**:59:59', cron.stop) ``` ____ ## `confutil`.py **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/)) Example config to parse: ```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: ```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')) ``` ____ ## `sendmail`.py **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/)) | 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`| |**[--time]**|minutes of attempts to send|3| Example usage in terminal with Python: ```bash python3 ./sendmail.py -u user@gmail.com -p pass -d addr1@gmail.com,addr2@gmail.com ``` Example usage in terminal with make the script executable: ```bash chmod u+x ./sendmail.py ./sendmail.py -u user@gmail.com -p pass -d addr1@gmail.com,addr2@gmail.com ``` Example usage in Python: ```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) ``` ____ ## `simplewc`.py **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) | 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: ```bash # 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 # 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 ``` Example usage in terminal with make the script executable: ```bash chmod u+x ./simplewc.py ./simplewc.py --domain EXAMPLE.COM --server 8.8.8.8 --keyname KEY --keydata YOU_KEY_CONTENT --test --force ./simplewc.py --domain EXAMPLE.COM --server dyn.dns.he.net --keyname - --keydata YOU_DDNSKEY --test --force ``` ____ ## `ovpn-client-management`.sh **Description:** > Creating or deleting client config for openvpn and sending config and info to email. **Dependencies:** > > * chpasswd > * [openvpn](https://openvpn.net/) (tested version 2.5.1 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/)) > * [easy-rsa](https://github.com/OpenVPN/easy-rsa) (tested version 3.0.8 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/)) > * tar > * [Python 3](https://www.python.org/downloads/) (tested version 3.9.5 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/)) > * existing [/usr/local/bin/sendmail.py](http://git.hmp.today/pavel.muhortov/utils/raw/branch/master/sendmail.py) | POSITION | PARAMETERS | DESCRIPTION | DEFAULT | |-----------|--------------|------------------------|---------------| | 1 |**add\|del**|add or delete client config|**REQUIRED**| | 2 |**\**|client username|**REQUIRED**| | 3 |**\**|client password|| | 4 |**[-f]**|service will restart after username delete|| Example usage: ```bash # download sudo wget https://git.hmp.today/pavel.muhortov/utils/src/branch/master/ovpn-client-management.sh -O /etc/openvpn/server/ovpn-client-management.sh sudo chmod +x /etc/openvpn/server/ovpn-client-management.sh ``` ```bash # create link ln -s /etc/openvpn/server/ovpn-client-management.sh ./ovpn ``` ```bash # create client sudo ./ovpn add username password ``` ```bash # delete client (and restart service for applying changes) sudo ./ovpn del username -f ``` ```bash # check journal tail -f /var/log/openvpn/ovpn.log ``` ____ ## `ovpn-connect-handling`.sh **Description:** > Handling client connection and preparing stats for monitoring. **Dependencies:** > > * executing by [openvpn](https://openvpn.net/) server (tested version 2.5.1 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/)) > * [jq](https://github.com/stedolan/jq) (tested version 1.6 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/)) > [grepcidr](https://github.com/ryantig/grepcidr) (tested version 2.0 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/)) > * [Python 3](https://www.python.org/downloads/) (tested version 3.9.5 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/)) > * existing [/usr/local/bin/sendmail.py](http://git.hmp.today/pavel.muhortov/utils/raw/branch/master/sendmail.py) > * [bash](https://www.gnu.org/software/bash/) (tested versions: 5.1.4 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/), 5.0.17 on [Ubuntu 20](https://wiki.ubuntu.com/FocalFossa/ReleaseNotes), 4.2.46 on [CentOS 7](https://wiki.centos.org/Manuals/ReleaseNotes/CentOS7.2009)) | POSITION | PARAMETERS | DESCRIPTION | DEFAULT | |-----------|--------------|------------------------|---------------| | 1 |**inc\|dec**|increment or decrement counter|**REQUIRED**| | 2 |****|root path for counter, names, log|**REQUIRED**| | 3 |**[mail]**|send email notification|| | 4 |**[geo]**|check client address geolocation|| Example usage: ```bash # download sudo wget https://git.hmp.today/pavel.muhortov/utils/src/branch/master/ovpn-connect-handling.sh -O /etc/openvpn/server/ovpn-connect-handling.sh sudo chmod +x /etc/openvpn/server/ovpn-connect-handling.sh ``` ```bash # add options to openvpn server config file sudo tee -a /etc/openvpn/server/server.conf > /dev/null <<'EOF' script-security 2 client-connect "/etc/openvpn/server/ovpn-connect-handling.sh inc /var/log/openvpn mail geo" client-disconnect "/etc/openvpn/server/ovpn-connect-handling.sh dec /var/log/openvpn - -" EOF sudo systemctl restart openvpn@server ``` ```bash # check counter and names watch cat /var/log/openvpn/openvpn-counts.log # check journal tail -f /var/log/openvpn/ovpn-connect-handling.log ``` ____ ## `wg-connect-handling`.sh **Description:** > Handling client connection and preparing stats for monitoring. **Dependencies:** > > * privileged rights > * [jq](https://github.com/stedolan/jq) (tested version 1.6 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/)) > [grepcidr](https://github.com/ryantig/grepcidr) (tested version 2.0 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/)) > * [Python 3](https://www.python.org/downloads/) (tested version 3.9.5 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/)) > * existing [/usr/local/bin/sendmail.py](http://git.hmp.today/pavel.muhortov/utils/raw/branch/master/sendmail.py) > * [bash](https://www.gnu.org/software/bash/) (tested versions: 5.1.4 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/), 5.0.17 on [Ubuntu 20](https://wiki.ubuntu.com/FocalFossa/ReleaseNotes), 4.2.46 on [CentOS 7](https://wiki.centos.org/Manuals/ReleaseNotes/CentOS7.2009)) | POSITION | PARAMETERS | DESCRIPTION | DEFAULT | |-----------|--------------|------------------------|---------------| | 1 |****|root path for counter, names, log|**REQUIRED**| | 2 |**[mail]**|send email notification|| | 3 |**[geo]**|check client address geolocation|| Example usage: ```bash # download sudo wget https://git.hmp.today/pavel.muhortov/utils/src/branch/master/wg-connect-handling.sh -O /etc/wireguard/wg-connect-handling.sh sudo chmod +x /etc/wireguard/wg-connect-handling.sh ``` ```bash # create root path for counter, names, log sudo mkdir /var/log/wireguard sudo chown -R root:root /var/log/wireguard sudo chmod -R 755 /var/log/wireguard ``` ```bash # sudo crontab -e * * * * * bash /etc/wireguard/wg-connect-handling.sh /var/log/wireguard mail geo ``` ```bash # check counter and names watch cat /var/log/wireguard/wg-counts.log # check journal tail -f /var/log/wireguard/wg-connect-handling.log ```