Skip to content

Instantly share code, notes, and snippets.

View rsperl's full-sized avatar

Richard rsperl

  • North Carolina, United States
View GitHub Profile
@rsperl
rsperl / colored_text.sh
Last active July 25, 2022 16:17
create functions that output colored text #shell #snippet
# usage: source colored_text.sh
# c_red("hello")
export C_BLACK="\\033[0;30m"
export C_DK_GRAY="\\033[1;30m"
export C_RED="\\033[0;31m"
export C_LT_RED="\\033[1;31m"
export C_GREEN="\\033[0;32m"
@rsperl
rsperl / do_not_echo_stdin.sh
Last active July 25, 2022 16:16
do not echo stdin to stdout #shell #snippet
#!/bin/bash
# show prompt without a newline
echo -n "Password:"
# Turn off echo
stty -echo
# Read what the user typed
read passwd
@rsperl
rsperl / remove_colors.sh
Last active July 25, 2022 13:07
remove ansi colors #shell #sed #snippet
# GNU sed
sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"
# Mac sed
sed -E "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"
@rsperl
rsperl / show_colors.sh
Last active July 25, 2022 13:28
show terminal colors #shell #snippet
#!/bin/bash
#
# This file echoes a bunch of color codes to the
# terminal to demonstrate what's available. Each
# line is the color code of one forground color,
# out of 17 (default + 16 escapes), followed by a
# test use of that color on all nine background
# colors (default + 8 escapes).
#
@rsperl
rsperl / bash_template.sh
Last active February 2, 2023 02:46
bash script templates #shell #template #snippet
#!/bin/bash
# see also
# - Google's Shell Style Guide: https://google.github.io/styleguide/shell.xml
# - ShellCheck: https://github.com/koalaman/shellcheck
# src:
# - http://hackaday.com/2017/07/21/linux-fu-better-bash-scripting
# - https://dev.to/thiht/shell-scripts-matter
@rsperl
rsperl / python_template.py
Last active July 25, 2022 16:15
python script template #python #template #snippet
#!/usr/bin/env python3
"""
Documentation about this script or module
"""
import os
import sys
from datetime import datetime
import argparse
@rsperl
rsperl / flask_app.py
Last active July 25, 2022 16:14
flask app template #python #rest #snippet
#!/usr/bin/env python3
"""
Documentation
See also https://www.python-boilerplate.com/flask
"""
import os
import argparse
from flask import Flask, jsonify, send_file
from flask_cors import CORS
@rsperl
rsperl / resolve_hostname.py
Last active July 25, 2022 13:17
resolve a hostname directly with the nameserver #python #networking #snippet
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import dns.resolver
res = dns.resolver.Resolver()
ans = res.query("www.google.com", "A")
print("ans={}".format(ans))
@rsperl
rsperl / guid2string.py
Last active July 25, 2022 16:09
convert a binary guid from ldap or active directory to a hex string #python #ldap #snippet
#!/usr/bin/python3
import uuid
def guid_to_string(binary_guid: bytes) -> str:
return str(uuid.UUID(bytes_le=binary_guid)).lower()
@rsperl
rsperl / RetryRestDecorator.py
Last active July 25, 2022 16:13
decorator class, base class, and implementation class to provide customized retries for rest calls #python #rest #snippet
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# src: https://powerfulpython.com/blog/making-unreliable-apis-reliable-with-python/
#
# example usage:
# retry_on_auth_failure = RetryOnAuthFailure()
# retry_on_server_failure = RetryOnServerFailure(retries=5)
#
# @retry_on_server_failure