Skip to content

Instantly share code, notes, and snippets.

Avatar

Amir Kadivar amirkdv

View GitHub Profile
@amirkdv
amirkdv / php_mdb.rst
Last active August 14, 2022 10:53
How to handle MS Access MDB files in Linux with PHP5 PDO and ODBC
View php_mdb.rst

To be able to use PHP5 PDO with MS Access mdb files the following is required (the same applies for the PHP4 style of using odbc_X except for the obviously PDO specific requirements):

PHP ODBC module

In Linux this is achieved by intalling the php5-odbc package:

@amirkdv
amirkdv / csv_rw.php
Last active August 29, 2015 13:57
simple CSV r/w utility
View csv_rw.php
<?php
// First pass at a small CSV r/w utility. Before hacking at this, consider https://packagist.org/search/?q=csv
/**
* extracts the list of fields of a given associative array,
* @param array[][] $data
* @return array[]
* @throws Exception
* if provided associative array is inconsistent
*/
@amirkdv
amirkdv / toHtml.php
Created April 24, 2014 21:55
Convert arbitrary data objects to HTML tables
View toHtml.php
<?php
/**
* Render arbitrary nested PHP variables into readable HTML. toHtmlTable()
* accepts an arbitrary variable of any of the following forms:
* - objects are first cast to associative arrays and then rendered as such.
* - arrays are rendered as a table with two columns: keys and values.
* - primitive data types are cast to strings
*
* Example usage:
@amirkdv
amirkdv / mechanic.rb
Last active August 29, 2015 14:02
Login, click through, and get the data you need from an HTTPS site
View mechanic.rb
#!/usr/bin/env ruby
# Use Mechanize to automatically login, click through, and fetch the data you
# need from an HTTPS site. The following works for the McGill admission portal.
# ruby mechanic.rb
# # Ready for Review
# # Ready for Review
require 'rubygems'
require 'mechanize'
@amirkdv
amirkdv / lv_rsync_backup.sh
Last active November 22, 2022 17:02
Backup an LVM logical volume via Rsync
View lv_rsync_backup.sh
#!/bin/bash
# Backup an entire LVM logical volume via a snapshot LV and rsync
[[ $( whoami ) != 'root' ]] && echo "sudo please" && exit 1
set -o errexit
set -o nounset
# LVM logical volume to backup
lv=book01
@amirkdv
amirkdv / Monitor.md
Last active November 20, 2015 19:33
Simple HTTP monitor
View Monitor.md

POC simple HTTP monitoring script. First build the docker images:

git clone [this-gist] monitor
cd monitor
docker build -t amirkdv/monitor .

Now start the container, and all sites under sites.yaml will be pinged one after another:

docker run -i -t amirkdv/monitor
@amirkdv
amirkdv / Transparent Proxy to docker containers.md
Last active August 22, 2020 02:14
Transparent Proxy to docker containers
View Transparent Proxy to docker containers.md

This is an example of using Linux Kernel's Transparent Proxy to route all TCP traffic to docker containers without having to resort to PROXY protocol which is not supported by some applications (e.g. sshd). To get the demo to work you only need vagrant installed:

git clone [this-gist] tproxy-demo
cd tproxy-demo
vagrant up
@amirkdv
amirkdv / mysqldo.sh
Last active August 29, 2015 14:03
Cleanly run commands that require mysql running
View mysqldo.sh
#!/bin/bash
# Executes the provided arguments as a shell command with a twist: if mysql is
# not already running: starts mysql, runs the commands, and stops mysql.
# Example usage:
# - mysqldo mysql -u root -p # will open an interactive shell and will stop mysql
# # when you leave the shell
# - mysqldo 'echo "show databases;" | mysql -u root -p"my_password"'
# # note that if your commands involve pipes or anything that the shell
# # interpretter would not pass to mysqldo as arguments, you should wrap the
# # entire command in quotes.
View docker-entrypoint-dev.md

In a fresh checkout:

cat provision
# #!/bin/bash
#
# echo step 1 >> /report.log
# echo step 2 >> /report.log

docker build -t entrypoint-exp01 .
# ...
@amirkdv
amirkdv / Allocate ports.md
Last active August 29, 2015 14:08
Generate deterministic *nix ports for services
View Allocate ports.md

Example usage:

$ python gimme_port.py
usage: gimme_port.py [-r|--resolve] NAME

$ python gimme_port.py muhc.ca.http
29595

$ python gimme_port.py staging.muhc.ca.http
port 31079 is in use by 'nc' (pid: 28569), use --resolve