• コマンドラインオプションいちいち覚えてないのでメモっとく用ページ
  • このページの内容を信用して起きた事故など一切感知しないので、自己責任で見てください

ファイル・ディレクトリの存在確認

複数のサーバーにディレクトリが存在するかチェックする

実行結果

$ for i in `awk /gomi-/'{print $2}' /etc/hosts`; do echo "$i:"; ssh $i '[ -d /etc/elasticsearch ]; echo $?'; done;
gomi-web01
1
gomi-back01
0
  • -w: writable
  • -x: executable
  • -e: path
  • -d: directory
  • -f: file
  • -s: empty file

ファイル・ディレクトリのサイズ確認

権限周りでうまく集計できないケースがあるので sudo をつけたり、適切なユーザーになってから実行するとよい

$ sudo du -sh /home/common/*
4.0K	/home/common/app
112K	/home/common/logs
  • -s: --summarize
  • -h: --human-readable

秘密鍵関連

生成

$ ssh-keygen -t rsa -b 4096

公開鍵の確認

$ ssh-keygen -y -f ~/.ssh/id_rsa

mkdir && cd

tmp $ mkdir hoge
tmp $ cd $_
hoge $

Git

差分がでたファイル名だけを取得する

$ git diff --name-only HEAD HEAD~1
group_vars/web/php_vars.yml
roles/nginx/tasks/main.yml

コミットハッシュの取得

$ git rev-parse HEAD
efdcc7fdcfbdc53149c92e666f0190bfc530562f

コネクションまわり

とりあえず打っとく

root にならないと一部の情報が見えないので、気をつける

$ sudo netstat -antp
  • -a: With the default display, show the state of all sockets; normally sockets used by server processes are not shown. With the routing table display (option -r, as described below), show protocol-cloned routes (routes generated by a RTF_PRCLONING parent route); normally these routes are not shown.
  • -n: Show network addresses as numbers (normally netstat interprets addresses and attempts to display them symbolically). This option may be used with any of the display formats.
  • -t: tcp (--tcp) に絞る
  • -p: Show the PID and name of the program to which each socket belongs.

bash

  • Ctrl + u: ラインクリア
  • Ctrl + y: クリアした文字列を復活させる

並列にリモートコマンドを実行する

  • 以下のようにすれば、並列に実行し、すべての終了を待ち合わせて処理の実行を完了させてくれる
for host in `grep web /etc/hosts | awk '{print $2}'`;
do
  ssh $host ...重い処理... &;
  wait;
done;

ジョブ制御

$ # 重いタスク
$ sleep 100000000


^Z #=> タスクのサスペンド
[1]+  Stopped                 sleep 100000000
$
$ # バックグラウンドで再開
$ bg 1
[1]+ sleep 100000000 &
$
$ # ジョブ一覧確認
$ jobs
[1]+  Running                 sleep 100000000 &
$
$ # フォアグラウンドへ
$ fg 1
sleep 100000000

grep

ps grep 時に grep プロセスを引っ掛けない

  • grep の末尾を [] に突っ込んでおく古来より伝わる方法(?)がある
$ ps -ef | grep ngin[x]
nginx      317 26695  1 22:31 ?        00:00:16 php-fpm: pool www
nginx      825 26695  1 22:35 ?        00:00:15 php-fpm: pool www
nginx    14515 32243  0 15:00 ?        00:00:59 nginx: worker process
nginx    14516 32243  0 15:00 ?        00:00:36 nginx: worker process
nginx    18690 26695  0 20:25 ?        00:01:30 php-fpm: pool www
root     32243     1  0 Feb03 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

AWS CLI

インスタンスリストの取得

$ aws ec2 describe-instances | jq '.Reservations[].Instances[] | { Name: (.Tags[]? | select(.Key == "Name")).Value, State: .State.Name, InstanceId, PublicIpAddress }'
{
  "Name": "web01",
  "State": "stopped",
  "InstanceId": "i-............",
  "PublicIpAddress": null
}
{
  "Name": "bastion01",
  "State": "stopped",
  "InstanceId": "i-............",
  "PublicIpAddress": "xx.xx.xx.xx"
}

SNS トピックのサブスクリプション ARN の抽出

$ aws sns list-subscriptions | jq -r '.Subscriptions[] | select(.TopicArn | test("HogeFuga")) | .SubscriptionArn'
arn:aws:sns:ap-northeast-1:😇:HogeFuga:😇
arn:aws:sns:ap-northeast-1:😇:HogeFuga:😇
...

MongoDB チートシート

Docker イメージ

mongo - Docker Hub

$ brew tap mongodb/brew
$ brew install mongodb-community@4.0
$ docker run --name test-mongo -d mongo:latest
$ docker exec -it test-mongo bash

基本操作

# コレクションの作成
> db.createCollection('test')
{ "ok" : 1 }

# コレクションの一覧
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
test    0.000GB

# コレクションの削除
> db.test.drop()
true

# データの追加
> db.test.insert({'id':1, 'name':'hage'})
WriteResult({ "nInserted" : 1 })

# データの検索
db.test.find()
{ "_id" : ObjectId("5ce823f39130afabdf41a5fd"), "id" : 1, "name" : "hage" }
{ "_id" : ObjectId("5ce823fb9130afabdf41a5fe"), "id" : 2, "name" : "fuga" }
{ "_id" : ObjectId("5ce824029130afabdf41a5ff"), "id" : 3, "name" : "piyo" }

> db.test.find({'name':'fuga'})
{ "_id" : ObjectId("5ce823fb9130afabdf41a5fe"), "id" : 2, "name" : "fuga" }

# データの削除
> res = db.test.find()[0]
{ "_id" : ObjectId("5ce822d59130afabdf41a5fc"), "id" : 1, "name" : "hage" }
> db.test.remove(res)
WriteResult({ "nRemoved" : 1 })

集計

> db.order.find()
{ "_id" : ObjectId("5ce825729130afabdf41a604"), "order_id" : 1, "user_id" : 2, "amount" : 12 }
{ "_id" : ObjectId("5ce8257a9130afabdf41a605"), "order_id" : 2, "user_id" : 1, "amount" : 41 }
{ "_id" : ObjectId("5ce825809130afabdf41a606"), "order_id" : 1, "user_id" : 1, "amount" : 13 }
> db.order.aggregate([
     { $match: { 'user_id':1} },
     { $group: { _id : "$user_id", sum: { $sum: "$amount" }} }
])
{ "_id" : 1, "sum" : 54 }

openssl コマンド

OpenSSL Essentials: Working with SSL Certificates, Private Keys and CSRs | DigitalOcean

CSR とは何か

SSL 証明書を認証局(CA: Certificate Authority)から得るために署名リクエスト(CSR: Certificate Signing Request)が必要となります。

CSR を発行するときには DN(Distinguished Name) とよばれる情報を指定する必要があります。SSL 証明書の発行に際しては特に Common Name すなわち FQDN が重要な項目となってきます。そのほか組織名などの項目があります。

CSR の生成

openssl コマンドを利用して簡単に CSR を生成可能です

$ openssl req \
>        -newkey rsa:2048 \
>        -nodes -keyout domain.key \
>        -out domain.csr
Generating a 2048 bit RSA private key
..........+++
............................+++
writing new private key to 'domain.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:JP
State or Province Name (full name) [Some-State]:Tokyo
Locality Name (eg, city) []:Tokyo
Organization Name (eg, company) [Internet Widgits Pty Ltd]:53ningen
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:53ningen.com
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

自己署名証明書の生成

$ openssl req \
>        -newkey rsa:2048 -nodes -keyout domain.key \
>        -x509 -days 365 -out domain.crt
Generating a 2048 bit RSA private key
........................................+++
.......................................................+++
writing new private key to 'domain.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:JP
State or Province Name (full name) [Some-State]:Tokyo
Locality Name (eg, city) []:Tokyo
Organization Name (eg, company) [Internet Widgits Pty Ltd]:example
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:53ningen.com
Email Address []:
Copyright © 53ningen.com