Skip to content

Instantly share code, notes, and snippets.

View brettfreer's full-sized avatar

Brett Freer brettfreer

View GitHub Profile
@brettfreer
brettfreer / convert.py
Last active June 2, 2016 23:49
Convert integer to another base, returning a string
# convert base-10 integer a to base-b, returning a string
def convert(a,b):
add = a % b
if a <= 1:
return str(a)
return str(convert(a//b, b)) + str(add)
@brettfreer
brettfreer / printer_restart.sh
Created May 23, 2016 08:32
Linux bash script to attempt restarting specifed cups print queues
#!/bin/sh
# restart print queues
process()
{
host=$1 queue=$2
ping -c 1 ${host} 2>&1 >/dev/null
status=$?
@brettfreer
brettfreer / linux_idleout.sh
Last active May 23, 2016 08:40
Auto-logout script for linux
#!/bin/sh
#
# linux_idleout.sh [idle time]
#
# Auto-logout process for telnet/ssh sessions
#
IDLE_TIME=180
LOG_FILE=/tmp/idle.out
EXEMPT_PROCESSES="app1|app2|app3|etc" # optional list of apps which can be excluded
@brettfreer
brettfreer / first_day_of_month.py
Last active May 24, 2016 06:28
Return first day of month
import datetime
def first_day_of_month(d):
return datetime.date(d.year, d.month, 1)
@brettfreer
brettfreer / last_day_of_month.py
Last active May 24, 2016 06:30
Return last day of month
import datetime
import calendar
def last_day_of_month(d):
return datetime.datetime(d.year, d.month, calendar.mdays[d.month]).date()
@brettfreer
brettfreer / perl-post-xml.pl
Created May 26, 2016 00:09
Example perl script showing http(s) post of a cXML document
#!/usr/bin/perl -w
use strict;
use LWP::UserAgent;
use HTTP::Request::Common qw(POST);
use Net::SSL;
use LWP::Protocol::https;
use URI::https;
my $url = 'https://someurl.com/SubmitCXML';
@brettfreer
brettfreer / perl-post-multipart.pl
Created May 26, 2016 00:12
Example showing perl multipart http(s) post
#!/usr/bin/perl -w
use strict;
use LWP::UserAgent;
my $url = 'https://someurl.com/einvoice.dll';
my $filename = $ARGV[0];
my $sid = "username";
my $spwd = "password";
@brettfreer
brettfreer / linux_user_expire.sh
Created June 3, 2016 00:49
Expire/delete inactive linux user accounts
#!/bin/sh
#
# Expire and delete inactive linux users
#
# check this before running!
exclude_users="keepme|metoo|daemon|adm|lp|sync|shutdown|halt|mail|news|uucp|operator|man|postmaster|smmsp|portage|nobody|sshd|cron|ntp|messagebus|mysql|apache|haldaemon|ftp|postfix|dhcp|ntop|fetchmail|squid|hsqldb|tomcat"
remove_period="548" # 18 months
@brettfreer
brettfreer / combinations.py
Created June 5, 2016 10:55
Return all 2 part combinations of a list
from itertools import combinations
def f(A):
for p in combinations(A, 2):
return p[0], p[1]
@brettfreer
brettfreer / gcd.py
Created June 5, 2016 10:58
Return the greatest common denominator (GCD) of a list of integers A.
def GCD(a, b):
if b == 0:
return a
else:
return GCD(b, a%b)
gcd = reduce(lambda x, y: GCD(x, y), A)