Skip to content

Instantly share code, notes, and snippets.

@akanehara
akanehara / useful-oneliners.sh
Last active February 22, 2024 05:51
実用一行野郎
VBoxManage list runningvms | awk '$0=$1' | tr -d '"' | xargs -n1 -I@ VBoxManage controlvm @ acpipowerbutton
# 事前ソート不要の uniq
awk '!s[$0]{s[$0]=1;print}'
# nl 用例
# - ゼロ埋め2桁連番空白セパレータ
nl -nrz -w2 -s ' '
# 使われていないvmの削除コマンド (さらにshにパイプして実行)
@akanehara
akanehara / iptables.bash
Last active November 28, 2022 02:51
iptablesスニペット
# 接続先で遮断
iptables -I INPUT -p tcp --tcp-flags ALL,SYN SYN -s ${SRC_ADDR} --dport ${PORT} -j DROP
iptables -I INPUT -p tcp -s ${SRC_ADDR} --dport ${PORT} -j REJECT
# 接続元で遮断
iptables -I OUTPUT -p tcp -d ${DEST_ADDR} --dport ${PORT} -j REJECT
iptables -I OUTPUT -p tcp --tcp-flags ALL,SYN SYN -d ${DEST_ADDR} --dport ${PORT} -j DROP
# 何か入力するまで遮断
{ CMD='sudo iptables $op OUTPUT -p tcp --dport 9200 -j REJECT'; op=-I;eval "$CMD"; read; op=-D; eval "$CMD" ;}
@akanehara
akanehara / cli-template.pl
Last active December 21, 2021 01:09
CLIツール雛形 (Perl)
#!/usr/bin/perl
use strict;
use warnings;
# use lib "."; # Module search path
use File::Basename qw/basename dirname/;
use Pod::Usage;
use Getopt::Long qw(:config posix_default no_ignore_case gnu_compat);
@akanehara
akanehara / elem.pl
Created February 17, 2021 02:23
HTTP::Message などで使われているアクセッサ実装ヘルパー
sub foo { shift->_elem('_foo', @_); }
sub bar { shift->_elem('_bar', @_); }
sub _elem
{
my $self = shift;
my $elem = shift;
my $old = $self->{$elem};
$self->{$elem} = $_[0] if @_;
return $old;
@akanehara
akanehara / giwaku.sh
Last active August 1, 2020 02:06
疑惑の総合商社の初出は2002年3月11日
curl -s "https://kokkai.ndl.go.jp/api/speech?$(echo 'spaker=辻本清美&any=疑惑の総合商社' | perl -MURI::Escape -ple '$_=uri_escape $_;')" | xmllint --encode utf-8 --xpath "//speechRecord/session|//speechRecord/date|//speechRecord/speech|//speechRecord/speaker" - | nkf --numchar-input | gtr -d '\n' | gsed -e 's/[:space:]*//g' | gsed -e 's/<[\/0-9a-zA-Z]*>/ /g' | xargs -n4
@akanehara
akanehara / gist:97a7ee8627413eb5d3d5dece13ac4948
Last active August 1, 2020 02:02
斎藤衛氏の国会証人喚問での発言
curl -s "https://kokkai.ndl.go.jp/api/speech?$(echo 'speaker=斎藤衛' | perl -MURI::Escape -ple '$_=uri_escape $_;')" | xmllint --encode utf-8 --xpath "//session|//date|//speech|//speaker" - | nkf --numchar-input | tr -d '\n' | LANG=C LC_ALL=C sed -e 's/ *//g' | sed -e 's/<[\/0-9a-zA-Z]*>/ /g' | xargs -n4
@akanehara
akanehara / README.md
Last active April 5, 2020 09:28
https://code-for-philly.gitbook.io/chime/ の更新差分確認スクリプト

Usage: code-for-philly.bash

https://code-for-philly.gitbook.io/chime/ をダウンロードし, 過去のダウンロード分があれば Gitbook JSON の変更有無を比較します. もしも変更があった場合は日付ディレクトリに保存され,変更差分を確認するための diff コマンドラインを stdout に出力します.

stdout に出力された内容は code-for-philly.bash.log にも保存されます.

注意: ログや日付ディレクトリはカレントディレクトリ下に作成されます!

code-for-philly.bash.log の出力例

@akanehara
akanehara / bowl-score.hs
Last active January 6, 2020 10:41
すごいHaskell読書会 in 大阪で紹介されたボウリングのスコア計算のお題(http://yashigani.hatenablog.com/entry/2013/10/23/150059)が面白そうなのでぼくもやってみました。
{-
- 課題
- ボウリングのスコアを計算するプログラムを作成する。
- ストライクはX,スペアは/,ガーターは-と表現し、
- ゲームの結果は以下のように入力される。
- XXXXXXXXXXXX #パーフェクト
- ------------------/5 # 9フレームまでガーターで10フレームでスペア
-}
module Main (main) where
@akanehara
akanehara / unuseful-onliners.sh
Last active August 2, 2019 01:40
非実用一行野郎
# スプレッドシートの37万8861列目は UNKO
echo UNKO | perl -aple 'my($d,$x);for(map ord(uc$_)-65,reverse split//,$F[0]){$x+=($_+1)*(26**$d++)};$_=$x'
# dcイディオム(map)
seq 10 | dc -e '[2*]sF' -e '?[lFxpc?z0<.]ds.x'
# dcイディオム(reduce)
seq 10 | dc -e '[+]sF?' -e '?[lFx?z1<.]s.z0<.p'
# 10進encode
printf akanehara | xxd -u -p | tr -d '\n' | dc -e '16i?p' | tr -d '\n\\'
@akanehara
akanehara / keyrepeat.mac.bash
Created April 5, 2019 06:48
Macのキーリピート高速化
defaults read -g KeyRepeat
defaults write -g KeyRepeat -int 1
defaults read -g InitialKeyRepeat
defaults write -g InitialKeyRepeat -int 10