Skip to content

Instantly share code, notes, and snippets.

View ckhung's full-sized avatar

Chao-Kuei Hung ckhung

View GitHub Profile
@ckhung
ckhung / gen-dot-tree.py
Created December 13, 2015 12:34
generate a random tree in .dot format (for graphviz to process)
#!/usr/bin/python
import argparse, numpy as np
def gen_tree(depth, id, branch):
label = id if args.label is None else args.label
print ' '*depth + id + '[label="' + label + '"];'
if depth >= args.depth:
return
nchild = np.random.poisson(branch)
@ckhung
ckhung / tidy.conf
Last active April 3, 2016 07:54
tidy configuration file
# 詳見 「既輕鬆上手又無限擴充的跨平臺文字編輯器 geany」
# http://newtoypia.blogspot.tw/2016/03/geany.html
tidy-mark: yes
markup: yes
wrap: 40
tab-size: 8
indent: auto
indent-spaces: 2
output-xhtml: yes
doctype: loose
@ckhung
ckhung / extract.php
Last active April 9, 2021 04:01
extract a piece of a web page using css selector
#!/usr/bin/php
<?php
require_once 'QueryPath/qp.php';
// 若是使用舊版 (2.1.2) 的 QueryPath, 應該這樣寫:
// require_once 'QueryPath/QueryPath.php';
// 詳見 「網頁搜括小工具: 用 extract.php 擷取網頁當中的一小塊」
// http://newtoypia.blogspot.tw/2016/03/web-scraping.html
// extract.php -s 'div.hentry' < ckhung.html > ckhung2.html
@ckhung
ckhung / sendmail-lines.conf
Last active April 15, 2016 01:30
/etc/fail2ban/action.d/sendmail-lines.conf
# Fail2Ban configuration file
#
# Author: Cyril Jaquier
#
# $Revision$
#
# 詳見: 「fail2ban: 新手老手 root 網管都要練的金鐘罩」
# http://newtoypia.blogspot.tw/2016/04/fail2ban.html
@ckhung
ckhung / repeated-fail2ban.pl
Last active December 3, 2019 02:49
summarize sites/subnets repeatedly banned by fail2ban
#!/usr/bin/perl -w
# 詳見: 「fail2ban: 新手老手 root 網管都要練的金鐘罩」
# http://newtoypia.blogspot.tw/2016/04/fail2ban.html
use DateTime;
use Getopt::Std;
my (%opts) = (
d => 7, # number of days to look back
@ckhung
ckhung / repeated-fail2ban.sh
Last active December 3, 2019 02:54
daily cron script to ban (for a longer time) sites repeatedly banned by fail2ban
#!/bin/sh
# 詳見: 「fail2ban: 新手老手 root 網管都要練的金鐘罩」
# http://newtoypia.blogspot.tw/2016/04/fail2ban.html
sed -i.bak '/repeated-fail2ban/,+1d' /etc/hosts.deny
/etc/fail2ban/repeated-fail2ban.pl -d 7 -g 3 -i 10 >> /etc/hosts.deny
cat <<EOF > /etc/apache2/conf.d/repeated-fail2ban
<Location />
Order Allow,Deny
Allow from all
@ckhung
ckhung / apache-deepurls.conf
Last active April 15, 2016 01:32
/etc/fail2ban/filter.d/apache-deepurls.conf to block "deep path" requests attacking apache2
# Fail2Ban configuration file
# 詳見: 「fail2ban: 新手老手 root 網管都要練的金鐘罩」
# http://newtoypia.blogspot.tw/2016/04/fail2ban.html
#
# Author: Chao-Kuei Hung
#
# $Revision: 1 $
#
[Definition]
@ckhung
ckhung / f2b-ad.conf
Last active April 15, 2016 01:33
filter to catch repeated offenders from /var/log/fail2ban.log
# Fail2Ban configuration file
#
# Author: Chao-Kuei Hung
#
# $Revision: 1 $
#
# 詳見: 「fail2ban: 新手老手 root 網管都要練的金鐘罩」
# http://newtoypia.blogspot.tw/2016/04/fail2ban.html
[Definition]
@ckhung
ckhung / decolumn-csv.perl
Last active May 4, 2016 12:50
把 csv 檔的很多個同性質欄位合併成兩個欄位
#!/usr/bin/perl -w
use strict;
use utf8;
use Getopt::Std;
my (%opts, @header, @decol, $x, $i);
%opts = (
d => ',', # delimiter
c => '0-', # columns to be de-columned. '0-6,8,12-15'
);
@ckhung
ckhung / readfile-jq.js
Last active January 9, 2017 11:08
the simplest file-reading example using jquery, plus fancy csv- and json-data display on html page
/* global $, document, console */
$(document).ready(function (){
// http://stackoverflow.com/questions/9898813/jquery-wait-till-multiple-get-requests-are-successully-processed
var f1 = 'https://raw.githubusercontent.com/g0v/potluckmap/gh-pages/data/shop-clothes.csv';
var f2 = 'https://raw.githubusercontent.com/g0v/potluckmap/gh-pages/data/shop-gift.geojson';
var clothes, gift;
clothes = $.get(f1);
gift = $.get(f2);
$.when(clothes, gift).done(init);
});