Skip to content

Instantly share code, notes, and snippets.

James Barclay futureimperfect

View GitHub Profile
View weird_encoding.py
def weird_encoding(s):
assert(isinstance(s, str))
first = list(s[::-1])
for i, c in enumerate(s):
if c.isupper():
first[i] = first[i].upper()
elif c.islower():
first[i] = first[i].lower()
first = ''.join(first)
second = str(len(s))
@futureimperfect
futureimperfect / clean_up_whitespace.sh
Last active Aug 29, 2015
Clean Up Whitespace With find and sed
View clean_up_whitespace.sh
find . -regex '.*\.[mh]$' -exec sed -i '' 's/[ \t]*$//' {} \;
@futureimperfect
futureimperfect / get_os_x_minor_version.sh
Last active Aug 29, 2015
Get OS X Minor Version (Yosemite Compatible)
View get_os_x_minor_version.sh
osversionlong=$(sw_vers -productVersion)
osversionshort=$(echo $osversionlong | python -c 'import sys; out = sys.stdin.read().split(".")[1]; print out')
echo "$osversionshort"
@futureimperfect
futureimperfect / complex_ldap_query.py
Created Jun 2, 2014
Complex LDAP Queries with python-ldap
View complex_ldap_query.py
import ldap
import sys
LDAP_URI = 'ldap://ldap.example.com'
SEARCH_BASE = 'ou=Example,dc=example,dc=com'
QUERY = '(&(nickname=M*)(employeeType=fulltime)(|(!(departmentNumber=5*))(loginShell=/bin/bash)))'
def ldap_search(ldap_uri, base, query):
'''
@futureimperfect
futureimperfect / complex_ldapsearch.sh
Last active Aug 29, 2015
Complex LDAP query with ldapsearch
View complex_ldapsearch.sh
ldapsearch -LLL -x -H ldap://ldap.example.com -b "ou=someou,dc=example,dc=com" '(&(nickname=M*)(employeeType=fulltime)(|(!(departmentNumber=5*))(loginShell=/bin/bash)))' mail | awk '/mail: / { print $2 }'
@futureimperfect
futureimperfect / java_vendor.py
Last active Aug 29, 2015
OS X Java Vendor (Oracle, Apple) One-Liner
View java_vendor.py
python -c 'import os,plistlib; jv = plistlib.readPlist(os.path.join(os.path.realpath("/Library/Internet Plug-Ins/JavaAppletPlugin.plugin"), "Contents/Info.plist"))["CFBundleIdentifier"].split(".")[1]; print jv.capitalize()'
@futureimperfect
futureimperfect / query_ad_group_membership.sh
Created Jan 4, 2014
Query AD Group Membership Status with ldapsearch
View query_ad_group_membership.sh
#!/bin/bash
console_user="$(/usr/bin/stat -f%Su /dev/console)"
ldap_uri="ldap://example.com"
search_base="DC=example,DC=com"
group="Some Group"
is_member_of_group=`/usr/bin/ldapsearch -LLL -Q -H "$ldap_uri" -b "$search_base" "sAMAccountName=$console_user" sAMAccountName | $grep "OU=$group"` # -Q prevents the authentication prompt, works if kerberos ticket exists
if [[ "${#is_member_of_group}" -ne "0" ]]; then
echo "$console_user is a member of $group."
@futureimperfect
futureimperfect / guest_login_status.sh
Last active Dec 20, 2015
Determine Guest Login Status from CLI on OS X. Will return 1 if enabled and 0 if not.
View guest_login_status.sh
/usr/bin/defaults read /Library/Preferences/com.apple.loginwindow | awk '/GuestEnabled/ { print $3 }' | sed 's/;//g'
@futureimperfect
futureimperfect / curl_speed_test.sh
Last active Dec 18, 2015
A cURL-based command-line download speed test. Add this to your ~/.bash_profile.
View curl_speed_test.sh
alias speedtest='echo "scale=2; `curl --progress-bar -w "%{speed_download}" http://speedtest.wdc01.softlayer.com/downloads/test10.zip -o /dev/null` / 131072" | bc | xargs -I {} echo {} mbps'
You can’t perform that action at this time.