Skip to content

Instantly share code, notes, and snippets.

#!/bin/bash
set -ex
# I am on server A
sshpass -e ssh -o PubkeyAuthentication=no -o StrictHostKeyChecking=no "${SSH_USERNAME}@${SERVER_B}" <<EOF
# Now I am on server B
export SSH_USERNAME=$SSH_USERNAME # Copy env. variable from server A to server B
export SERVER_C=$SERVER_C # Copy env. variable from server A to server B
export SSHPASS=$SSHPASS # INSECURE!! copy env. variable from server A to server B
@davidpelfree
davidpelfree / ActiveDirectoryUtils.java
Created July 17, 2017 17:12
Active Directory error code parser in Java
package util;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public final class ActiveDirectoryUtils {
private static final Pattern ERROR_CODE = Pattern.compile(".*LDAP: error code\\s([0-9]*).*data\\s([0-9a-f]{3,4}).*");
public static final int USERNAME_NOT_FOUND = 0x525;
@davidpelfree
davidpelfree / Util.java
Created May 25, 2017 18:29
Java 8 retry pattern
package util;
import java.util.Arrays;
import java.util.function.Supplier;
public final class Util {
/**
* Retry to run a function a few times, retry if specific exceptions occur.
*
error() {
local parent_lineno="$1"
local message="$2"
local code="${3:-1}"
if [[ -n "$message" ]] ; then
echo "Error on or near line ${parent_lineno}: ${message}; exiting with status ${code}"
else
echo "Error on or near line ${parent_lineno}; exiting with status ${code}"
fi
MIN_LINE=$(($parent_lineno - 5 > 0 ? $parent_lineno - 5 : 1))
@davidpelfree
davidpelfree / interesting-links
Created August 26, 2012 12:14
Interesting links
@davidpelfree
davidpelfree / mySql_upsert.sql
Created August 23, 2012 13:50
How to "upsert" using MySQL: update value, and if it doesn't exist, create it
insert into counter_hourly(DataCenter,Env,HostName,CustomerID,Hour,TotalRequests,TotalLatency,TotalBytes)
values
('DC1','ENV2','NTC-CLFSTGAPP3','Lic_ID1','2012-8-23 10:00','11','2190','8822')
on duplicate key update
`TotalRequests` = `TotalRequests` + '11' ,
`TotalLatency` = `TotalLatency` + '2190' ,
`TotalBytes` = `TotalBytes` + '8822'
;
-- Here's the DDL used to create this table.
@davidpelfree
davidpelfree / parseCsv.groovy
Created August 23, 2012 13:44
Easily and quickly parse CSV/TSV line
List<String> values = []
int start, stop
// Quickly parse TSV line
while (true) {
stop = line.indexOf('\t', start)
if (stop < 0) break;
values.push(line.substring(start, stop))
start = stop + 1
}
@davidpelfree
davidpelfree / sizeGroupAnalytics.groovy
Created August 23, 2012 13:36
For analytics, here's an easy and elegant way to devide numeric value (document size in this case) to groups of ranges using Groovy.
long contentLength = doc.getContentLength() // never mind where the long value comes from.
// Classify doc size. Notice that numbers in map's key have to be ordered (TreeMap) and last key should be Integer.MAX_VALUE:
// This is map of max length size value to a string describing this group
def docGroupMap = new TreeMap([2048:'0-2 KB', 5120:'2-5 KB', 10240:'5-10 KB', (Integer.MAX_VALUE): '10+ KB'])
// The find() method iterates the map and checks every key with the expression in its closure-parameter.
// Since TreeMap is used, the keys are ordered.
// Once the expression is true, the value is being assigned to the string.