Skip to content

Instantly share code, notes, and snippets.

@mendhak
mendhak / gist:1297742
Created October 19, 2011 08:37
Get a URL in Java
public static String GetUrl(String url) throws Exception
{
URL serverAddress = null;
HttpURLConnection connection = null;
// OutputStreamWriter wr = null;
BufferedReader rd = null;
StringBuilder sb = null;
String line = null;
try
@mendhak
mendhak / gist:3996857
Created November 1, 2012 21:50
Mount an SSHFS share
sshfs username@machinename.local:/home/username /mnt/mountname -p 1234 -o allow_other -o IdentityFile=/home/mendhak/Documents/keys/id_rsa
import time
import socket
def collect_metric(name, value, timestamp):
sock = socket.socket()
sock.connect( ("localhost", 2003) )
sock.send("%s %d %d\n" % (name, value, timestamp))
sock.close()
def now():
return int(time.time())

Keybase proof

I hereby claim:

  • I am mendhak on github.
  • I am mendhak (https://keybase.io/mendhak) on keybase.
  • I have a public key whose fingerprint is 6989 CF77 4903 69CF FDCB CD89 95E7 D75C 76CB E9A9

To claim this, I am signing this object:

@mendhak
mendhak / ssh-tunnel.sh
Last active May 30, 2017 21:45
Reliable methods of creating SSH tunnels for TCP forwarding
#Source: https://stackoverflow.com/questions/2241063/bash-script-to-setup-a-temporary-ssh-tunnel
#SSH feature - control sockets
ssh -4 -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa -M -S my-ctrl-socket -fnNT -L 10023:remoteserver.com:8000 dev@middleserver.internal
#Get PID - you'll have to parse this
ssh -S my-ctrl-socket -O check dev@middleserver.internal
#Exit
ssh -S my-ctrl-socket -O exit dev@middleserver.internal
@mendhak
mendhak / initialize-raw-disks.ps1
Created October 27, 2017 13:27
Initialize raw disks in Windows, partition, format and assign drive letters
$rawdisks = gwmi win32_diskdrive | where {$_.partitions -eq 0}
foreach ($r in $rawdisks)
{
$available=ls function:[d-z]: -n | ?{ !(test-path $_) } | SELECT -First 1
$diskIndex = $r.Index
Write-Host "Initializing Disk $diskIndex as $available. This will take a while."
(echo "list disk
select disk $diskIndex
online disk
attributes disk clear readonly
@mendhak
mendhak / gist:3369288
Created August 16, 2012 10:59
Extension method to get value from a field in a datarow
public static T GetValue<T>(this DataRow row, string field)
{
if (!row.Table.Columns.Contains(field))
{
return default(T);
}
else
{
return (T)Convert.ChangeType(row[field].ToString(), typeof(T));
@mendhak
mendhak / gist:3369293
Created August 16, 2012 11:00
Extension method to get value from a field in a SQLDataReader
public static class DataRecordExtensions
{
public static T GetValue<T>(this IDataRecord reader, string columnName)
{
object columnValue = reader[columnName];
T returnValue = default(T);
if (!(columnValue is DBNull))
{
returnValue = (T)Convert.ChangeType(columnValue, typeof(T));
}
@mendhak
mendhak / nmea_checksum.py
Created November 24, 2018 09:53
Calculate checksum from the data part of an NMEA sentence
import sys
import operator
# Given the data part of an NMEA sentence (Remove the $, remove the * onwards, keep the last comma)
# calculates the checksum
def checksum_calculate(nmeadata):
csum = 0
for c in nmeadata:
csum ^= ord(c)
@mendhak
mendhak / s3downloader.py
Created March 23, 2014 01:47
A script to download and concatenate AWS ELB Access logs
import os
import pytz
import sys
import datetime
import dateutil.parser
import boto
downloadFolder = "downloads"
if len(sys.argv) > 1: