Skip to content

Instantly share code, notes, and snippets.

View deckerego's full-sized avatar

John Ellis deckerego

View GitHub Profile
@deckerego
deckerego / XMLSearch.java
Created December 6, 2013 14:29
Search an XML Document with dom4j
File file = new File("/path/to/file");
SAXReader reader = new SAXReader();
Document document = reader.read(file);
XPath pathSelector = DocumentHelper.createXPath("/root/childOne/leaf[@attributeName='attributeValue']");
List pathNodes = pathSelector.selectNodes(document);
for(Object pathNode : pathNodes) {
Element pathElement = (Element) pathNode;
System.out.println(pathElement.getTextTrim());
@deckerego
deckerego / Encrypt.java
Created December 6, 2013 15:35
Simple Native Encryption with Java 6
byte[] input = args[0].getBytes();
byte[] key = args[1].getBytes();
SecretKeySpec aesKey = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES", "SunJCE");
System.out.println("Input : " + new String(input));
cipher.init(Cipher.ENCRYPT_MODE, aesKey);
byte[] encryptedText = new byte[cipher.getOutputSize(input.length)];
int encryptedTextLength = cipher.update(input, 0, input.length, encryptedText, 0);
@deckerego
deckerego / MyPID.java
Created December 6, 2013 15:36
Finding The Current Process ID
import java.io.IOException;
import java.util.UUID;
import com.sun.tools.attach.AttachNotSupportedException;
import com.sun.tools.attach.VirtualMachine;
import com.sun.tools.attach.VirtualMachineDescriptor;
public class MyPID {
public static void main(String[] args) throws AttachNotSupportedException, IOException {
String uniqueId = UUID.randomUUID().toString();
System.setProperty("process.unique.id", uniqueId);
@deckerego
deckerego / RemoteJMXValue.java
Created December 6, 2013 15:46
To connect to a non-SSL JVM over TCP and retrieve values from JMX managed beans. Other authentication methods can be seen at Luis-Miguel Alventosa's blog: https://blogs.oracle.com/lmalventosa/entry/jmx_authentication_authorization
JMXServiceURL serviceUrl = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://"+ipAddress+":"+port+"/jmxrmi");
String[] credentials = { userName, password };
Map<String, Object> env = new HashMap<String, Object>();
env.put(JMXConnector.CREDENTIALS, credentials);
JMXConnector connector = JMXConnectorFactory.connect(serviceUrl, env);
MBeanServerConnection connection = connector.getMBeanServerConnection();
ObjectName managedObject = new ObjectName(domainName, "name", objectName);
@deckerego
deckerego / BasicAuth.java
Created December 6, 2013 15:47
HTTP authentication should be performed according to RFC 2617. This specifies that: - An "Authorization" name/value pair is added to the HTTP header - The value of the Authorization header is a token consisting of the authorization scheme plus authentication token - The authentication token is the value username:password, encoded as a base64 str…
String authToken = new BASE64Encoder().encode(username + ":" + password);
URLConnection urlConnection = url.getUrl().openConnection();
urlConnection.setDoOutput(true);
urlConnection.setConnectTimeout(5000);
urlConnection.setRequestProperty("Authorization", "Basic "+authToken);
@deckerego
deckerego / password.sh
Created December 6, 2013 16:53
Prompt and confirm a password in a bash/sh shell
read -s -p "Password: " PASSWORD; echo
read -s -p "Confirm Password: " PASSCONFIRM; echo
if [[ "$PASSWORD" != "$PASSCONFIRM" ]]; then
echo "Passwords do not match, exiting"
exit -1
fi
echo "Passwords match!"
@deckerego
deckerego / xzball.sh
Created December 6, 2013 16:54
Most tar implementations are supposed to support xz compression along with gzip and bzip2, but some (*cough* OS X *cough*) still haven't gotten aboard the compression train.
tar cvf - $DIRECTORY | xz - > $OUTPUT_FILE
@deckerego
deckerego / interfaces
Created December 6, 2013 16:54
A Typical Ubuntu Interface Config for /etc/network/interfaces
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address 192.168.0.2
netmask 255.255.255.0
gateway 192.168.0.1
dns-nameservers 8.8.8.8 8.8.4.4
metric 0
@deckerego
deckerego / rsync_dir.sh
Last active January 7, 2017 03:02
RSync a directory over SSH
rsync --exclude=.git -ave ssh --delete MyDirectory/ root@remotehost:/srv/files/
@deckerego
deckerego / yesterday.sh
Created December 6, 2013 16:56
To print yesterday's date using a single line, or populate a script variable...
date +%Y-%m-%d --date="@$[`date +%s` - 86400]"
DATE=$(date +%Y-%m-%d --date="@$[`date +%s` - 86400]")