Skip to content

Instantly share code, notes, and snippets.

@NanoDano
NanoDano / bash_check_return_value.sh
Created June 3, 2020 21:02
Bash check return values
# See if last statement executed was successful
./runSomething
if [ $? -eq 0 ]; then
echo OK
else
echo FAIL
fi
# The return value can also be stored as a variable
@NanoDano
NanoDano / bash_history_tips.md
Created June 3, 2020 21:08
Bash history tips

Bash history tips

Run the last command again

!!

Check history

@NanoDano
NanoDano / bash_for_loop_examples.sh
Created June 3, 2020 21:09
Bash For loop examples
# Specific number of iterations
for i in $(seq 1 10)
do
echo $i
done
# Specify list
for x in test.txt misc.txt other.txt
do
rm $x
@NanoDano
NanoDano / bash_get_user_input.sh
Created June 3, 2020 21:11
Bash get user input
# Simple user prompt
echo -n "Enter name: "
read name
echo $name
# Read with a prompt
read -p "Enter name: " name
echo $name
# If reading from current shell and not running a separate process, use -e
@NanoDano
NanoDano / maps_caps_to_esc.ahk
Created June 3, 2020 21:26
Map Caps Lock key to Escape in Windows with AutoHotKey
; maps_caps_to_esc.ahk
; AutoHoyKey script to remap Caps lock to Escape key
; Double-click the file or run it directly from the
; command line to start the script in the background.
; To run the script on startup, place in the Startup directory
; by pressing `Windows Key + r` and running `shell:startup`.
Capslock::Esc
@NanoDano
NanoDano / minecraft_backup.py
Created July 14, 2020 01:24
Backup, zip, FTP upload, rotate script for Minecraft server
#!/usr/bin/python3
from ftplib import FTP
import glob
from os import system, path, remove
from os.path import join
from datetime import datetime
import logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)s %(message)s')
@NanoDano
NanoDano / django_backup_db.py
Last active August 11, 2020 05:13
Django management command to database backup, upload over FTP, and rotate files
# appname/management/commands/django_backup_db.py
# Then run with manage.py django_backup_db
from ftplib import FTP_TLS
from glob import glob
from os import system, remove
from os.path import join, getmtime, exists
from socket import gethostname
from django.core.management import BaseCommand, call_command
from django.utils.datetime_safe import datetime
@NanoDano
NanoDano / gen_self_signed_cert.py
Last active November 6, 2024 23:01
Generate a self-signed SSL certificate with Python OpenSSL
# pip install pyopenssl
from OpenSSL import crypto, SSL
from socket import gethostname
from pprint import pprint
from time import gmtime, mktime
CERT_FILE = "cert.pem"
KEY_FILE = "key.pem"
def create_self_signed_cert():
@NanoDano
NanoDano / require_root_user.sh
Created July 20, 2020 03:17
Require script to be run by root user
#!/usr/bin/bash
if [[ $EUID -ne 0 ]]; then
echo "This script needs escalated privs. Exiting"
exit 1
fi
@NanoDano
NanoDano / gunicorn_app.service
Created July 20, 2020 03:20
Systemd service file for a Gunicorn WSGI application
# /etc/systemd/system/gunicorn_app.service
# sudo python3 -m pip install gunicorn
[Unit]
Description=My WSGI app
After=network.target
[Service]
Type=simple
User=pi
WorkingDirectory=/var/www/my_wsgi_ap