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 / list_compare.py
Last active July 25, 2022 14:14
comparing lists in python #nosnippet
src = ['a', 'b', 'c']
dst = ['c', 'd', 'e']
srconly = list(set(src) - set(dst))
# ['a', 'b']
dstonly = list(set(dst) - set(src))
# ['e', 'd']
common_to_both = list(set(src).intersection(dst))
# ['c']
@rsperl
rsperl / delete_large_number_of_files.sh
Last active July 25, 2022 14:06
deleting a large number of files #snippet
#!/bin/sh
# src: http://www.commandlinefu.com/commands/view/4284/optimal-way-of-deleting-huge-numbers-of-files
find /path/to/dir -type f -print0 | xargs -0 rm
# Using xargs is better than:
#
# find /path/to/dir -type f -exec rm \-f {} \;
#
@rsperl
rsperl / port_check.sh
Last active July 25, 2022 14:48
determine if port is open with bash and not netcat #snippet
#!/bin/bash
timeout 2 bash -c "</dev/tcp/$s/636" 2>&1
# check $?
@rsperl
rsperl / lastpass_status_check.py
Last active July 25, 2022 14:12
get status of lastpass and show a notification #nosnippet
#!/usr/bin/env python
from pync import Notifier
import json
import requests
import sys
import os
from datetime import datetime
url = "https://status.lastpass.com"
@rsperl
rsperl / argparse_read_from_file.py
Last active July 25, 2022 13:59
if an argument value starts with @, read the value from a file #snippet
def parseargs():
"""parses CLI args"""
parser = argparse.ArgumentParser()
parser.add_argument("--username")
# ...
# if an arg value begins with @ and it is the path to a file,
# replace the arg value with the contents of the file
argkeys = args.__dict__.keys()
for option in argkeys:
@rsperl
rsperl / run_ansible_playbook.py
Last active July 28, 2022 00:00
run an ansible playbook programatically from python #snippet
#!/usr/bin/env python
"""
How to programatically run a playbook
(adapted from http://docs.ansible.com/ansible/dev_guide/developing_api.html#python-api-2-0)
Note that example from URL above only supports a single play per run, while actual playbooks
contain a list of plays. A playbook cannot be read in and run, but read in, and each play
run one at at time, and evaulate the result of each play separately (see below)
Also note that this is not a general purpose playbook runner, but one specifically suited to
@rsperl
rsperl / python_mock_sqlalchemy_execute.py
Last active July 25, 2022 14:58
mock python sqlalchemy execute method #snippet
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from sqlalchemy import create_engine
# use a minimal dburi with no real info
dburi = "mysql://"
# create method to be called upon execute
def mockery(sql, *args, **kwargs):
@rsperl
rsperl / get_timestamp_ansible.yml
Last active July 25, 2022 14:15
Get a formatted timestamp in ansible with facts or the date command #nosnippet
---
- name: Two ways to get a formatted timestamp
hosts: localhost
connection: local
gather_facts: no
tasks:
#
# using facts
@rsperl
rsperl / async_examples.md
Last active July 25, 2022 14:00
python3 async examples #snippet

Asyncio basics in python

src: https://medium.com/@alairock/asyncio-basics-in-python-29bf30cf254f

Python 3.5 brought with it asyncio. An event loop based paradigm previously available as a library but now it is built in as a standard library. There are other async libraries out there, but I am going to focus on the one built in to Python.

@rsperl
rsperl / convert_between_cidr_and_netmask.sh
Last active July 25, 2022 15:27
convert between cidr and netmask #snippet
#!/bin/bash
# src: https://stackoverflow.com/questions/20762575/explanation-of-convertor-of-cidr-to-netmask-in-linux-shell-netmask2cdir-and-cdir
mask2cidr ()
{
# Assumes there's no "255." after a non-255 byte in the mask
local x=${1##*255.}
set -- 0^^^128^192^224^240^248^252^254^ $(( (${#1} - ${#x})*2 )) ${x%%.*}
x=${1%%$3*}