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 / lockfile.sh
Last active July 25, 2022 15:27
using lockfiles in bash #snippet
# src: http://www.davidpashley.com/articles/writing-robust-shell-scripts/
# noclobber will not redirect to an existing file
if ( set -o noclobber; echo "$$" > "$lockfile") 2> /dev/null;
then
# we have the lockfile, so be sure to remove it if the script exits early
trap 'rm -f "$lockfile"; exit $?' INT TERM EXIT
# do our critical stuff
@rsperl
rsperl / M2M_Association_SQLalchemy.py
Last active July 25, 2022 13:48 — forked from SuryaSankar/M2M_Association_SQLalchemy.py
An example of a many to many relation via Association Object in SQLAlchemy #snippet
import sqlalchemy
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.orm import sessionmaker, relationship, backref
from sqlalchemy.ext.associationproxy import association_proxy
import uuid
engine = sqlalchemy.create_engine('sqlite:///:memory:')
Base = declarative_base()
@rsperl
rsperl / bind_port.py
Last active July 25, 2022 13:48
Bind to a random port and return the port #snippet
import socket
s = socket.socket(socket.AF_INET, type=socket.SOCK_STREAM)
s.bind(('localhost', 0))
address, port = s.getsockname()
print("Bound to {} on port {}".format(address, port))
s.close()
@rsperl
rsperl / pipenv.md
Last active July 25, 2022 13:49
getting started with pipenv #nosnippet
@rsperl
rsperl / tmctl.sh
Last active July 25, 2022 14:13
mount a volume, run time machine, and umount the volume #nosnippet
#!/bin/bash
volumelabel="TimeMachine"
dounmount=0
verbose=0
xtrace=0
model=$(sysctl -n hw.model)
case "$model" in
*MacBook*)
@rsperl
rsperl / main.m
Last active July 25, 2022 14:27
create an executable lockscreen on macos #snippet
#import <objc/runtime.h>
#import <Foundation/Foundation.h>
// src: https://apple.stackexchange.com/questions/80058/lock-screen-command-one-liner/123738#123738
// compile with
// clang -framework Foundation main.m -o lockscreen
int main () {
NSBundle *bundle = [NSBundle bundleWithPath:@"/Applications/Utilities/Keychain Access.app/Contents/Resources/Keychain.menu"];
@rsperl
rsperl / find_member_attributes.pl
Last active July 25, 2022 14:07
finding all member attributes when they exceed 1500 #snippet
#
# because there are more than 1500 values for the member attribute, you have to get the paged
# results. The attribute will be named member;range=x-y unless it's the remainder, in which
# case it will be member;range=x-*. If you search beyond the last result, no entries will be
# returned
#
my @current_member_dns;
# define the maximum number of members we will look for
@rsperl
rsperl / import_users_csv.ps1
Last active July 25, 2022 13:51
use powershell to import csv file to create Active Directory users #snippet
import-module activedirectory
import-csv .\createusers.csv | % {New-ADUser -GivenName $_.GivenName -Surname $_.Surname -Name $_.Name -SamAccountName $_.SamAccountName -Description $_.Description -Path $_.Path -Enabled $True -UserPrincipalName $_.UPN -EmailAddress $_.UPN -AccountPassword (convertto-securestring $_.Password -AsPlainText -force) }
## where createusers.csv looks like
# Index,GivenName,Surname,Name,SamAccountName,UPN,Description,Path,Enabled,Password,PasswordNeverExpires,ChangePasswordAtLogon
# "0001","test","user0001","test user0001","user0001","test.user0001@domain.internal","test user0001","OU=Users,OU=TST,OU=SL1,DC=domain,DC=internal","$True","Orion123_","$True","$False"
# "0002","test","user0002","test user0002","user0002","test.user0002@domain.internal","test user0002","OU=Users,OU=TST,OU=SL1,DC=domain,DC=internal","$True","Orion123_","$True","$False"
@rsperl
rsperl / bash_substrings.md
Last active July 25, 2022 14:19
bash substrings #snippets
@rsperl
rsperl / python_pid_lockfile.py
Last active July 25, 2022 14:23
using a pid lockfile in python #nosnippet
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
import os
from pid import PidFile, PidFileAlreadyLockedError
lockdir = "/tmp/"
filename = "mylockfile"
lockfile = os.path.join(lockdir, filename + ".pid")