2020年5月29日金曜日

stackdriver-agent のエラー can not take infinite value に対処する

TL;DR

stackdriver-agentcollectd

write_gcm: can not take infinite value
write_gcm: wg_typed_value_create_from_value_t_inline failed for swap/percent/value! Continuing.

とか言ってたら、以下のように編集して、

vim /etc/stackdriver/collectd.conf
-LoadPlugin swap
-<Plugin "swap">
-  ValuesPercentage true
-</Plugin>
+#LoadPlugin swap
+#<Plugin "swap">
+#  ValuesPercentage true
+#</Plugin>

stackdriver-agent を再起動しましょう。

service stackdriver-agent restart

何か起きたか

GCE 仮想マシンに Stackdriver Agent をインストールして動かし始めたら、以下なエラーが出ていた。

$ journalctl -u stackdriver-agent.service
  ...略...
May 29 05:34:55 myhost collectd[10338]: write_gcm: can not take infinite value
May 29 05:34:55 myhost collectd[10338]: write_gcm: wg_typed_value_create_from_value_t_inline failed for swap/percent/value! Continuing.
May 29 05:34:55 myhost collectd[10338]: write_gcm: can not take infinite value
May 29 05:34:55 myhost collectd[10338]: write_gcm: wg_typed_value_create_from_value_t_inline failed for swap/percent/value! Continuing.
May 29 05:34:55 myhost collectd[10338]: write_gcm: can not take infinite value
May 29 05:34:55 myhost collectd[10338]: write_gcm: wg_typed_value_create_from_value_t_inline failed for swap/percent/value! Continuing.

何が問題だったか

ログを見る限り、 OS に Swap 領域が存在しないにもかかわらず、

$ free
              total        used        free      shared  buff/cache   available
Mem:        7493724     3432032     1684776      360984     2376916     3396824
Swap:             0           0           0

Swap 領域の使用率のメトリクスを計算しようとして、ゼロ除算してるっぽい。

解決策 or 回避策

Swap 領域のモニタリングをしないようにした。

stackdriver-agent が利用する collectd の設定ファイルは以下にある。

vim /etc/stackdriver/collectd.conf

これを、以下のように編集し、

-LoadPlugin swap
-<Plugin "swap">
-  ValuesPercentage true
-</Plugin>
+#LoadPlugin swap
+#<Plugin "swap">
+#  ValuesPercentage true
+#</Plugin>

stackdriver-agent を再起動した。

service stackdriver-agent restart

出なくなった。

完。

2019年12月27日金曜日

Mac で sh/bash/zsh などシェルログイン時に Wireshark: Permission denied と出力される

Mac に Wireshark をインストールしてから、ターミナル起動時に

Wireshark: Permission denied

と出力されるようになってしまった。

別に ~/.bashrc 等の中で Wireshark を実行している形跡もない。

試しに他のシェルを起動してみると、 zsh でも sh でも発生する。

となると、 /etc/profile あたり?

  :
if [ -x /usr/libexec/path_helper ]; then
	eval `/usr/libexec/path_helper -s`
fi
  :

正解。

Permission denied/usr/libexec/path_helper -s を実行する際に出力されていることがわかった。

$ /usr/libexec/path_helper -s
Wireshark: Permission denied
PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/djeeno/google-cloud-sdk/bin:/Users/djeeno/.asdf/shims:/Users/djeeno/.asdf/bin:/Users/djeeno/.asdf/installs/golang/1.13.5/go/bin:/Users/djeeno/go/bin"; export PATH;

path_helper について調べる。

path_helper ($PATHを設定するコマンド) (macOS, /etc/paths.d, /etc/paths, shell間をまたいだパス設定) - いろいろ備忘録日記

デフォルトのパス設定は、/etc/pathsファイルから読み取られる。
残りは、/etc/paths.dディレクトリの下から読み取られる。

なるほど。

/etc/paths.d を見てみる。

$ ls -l /etc/paths.d
total 8
-rw-------  1 root  wheel  43 11 21 08:19 Wireshark

いた。

このファイルのパーミッションが 600 で読み込み権限が無いため、 Permission denied が出ていた。

ので、読み込めるようにする。

$ sudo chmod 644 /etc/paths.d/Wireshark
Password:
$ ls -l /etc/paths.d
total 8
-rw-r--r--  1 root  wheel  43 11 21 08:19 Wireshark

これで Permission denied は出なくなった。


参考

2019年6月28日金曜日

kubectl コマンド実行時に対象となるクラスターを表示し、実行を保留して誤爆を防ぐ kubectl ラッパーを書いた

kubectl コマンドで破壊的オペレーションを実行する際、実行時の current-context を出力することで、ユーザーに実行対象の Kubernetes クラスターを確認させ、かつユーザーの入力があるまで実行を保留し誤爆を防いでくれるやつです。

# Run the `source` command to load this file into the current shell like follow:
#   $ source kubectl-wrapper.sh

# kubect-wrapper displays the current-context and prompts for user confirmation
# when trying to execute kubectl's subcommands that can update the cluster's state.
kubectl() { (
  # if has 1 argument || has -h --help --dry-run option || Sub-commands that are not dangerous if executed without confirmation; then
  if [ $# -le 1 ] || { echo " $* " | grep -Eq -- ' -h | --help | --dry-run '; } \
    || { echo " $* " | grep -Eq ' api-resources | api-version | cluster-info | completion | config | describe | diff | explain | get | logs | top | version | wait '; }; then
    command kubectl "$@"
    return $?
  fi
  printf "\e[01;33m%s\n%s\e[0m" "# CurrentContext: " "#   "
  command kubectl config current-context
  printf "\e[01;33m%s\e[0m" "# Press Enter key to continue... "
  read -r
  command kubectl "$@"
)}

GKE に限らずですが、複数の k8s クラスターを使い分けてると、 context を誤爆しそうで怖かった。

gcloud container clusters get-credentials --zone asia-east1-a --project MY_PROJECT MY_KUBERNETES_CLUSTER_NAME

動作確認は bash 入りの自機でしかしていないので悪しからず…。

2019年6月16日日曜日

WSL1 上で Docker をカジュアルに利用する

2019-06-18 ここから追記。

元々書いていた方法だと、 build image 時に以下の問題が有ると怒られたので、おとなしく DOCKER_HOST 環境変数を使うようにしました…。

SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have ‘-rwxr-xr-x’ permissions. It is recommended to double check and reset permissions for sensitive files and directories.

以下、その手順。

1. タスクトレイアイコンの Docker を右クリック → Settings から設定を開く

2. □ Expose daemon on tcp://localhost:2375 without TLS にチェックを入れる

3. DOCKER_HOST 環境変数を Docker for Windows に向ける

export DOCKER_HOST=tcp://localhost:2375

~/.bashrc は dotfiles リポジトリで一括管理しているので、
Mac でも Windows でも同じ ~/.bashrc を使いたい。
ので、以下のようにいい感じに追記した。

# If exist docker.exe in $PATH, set env:DOCKER_HOST.
command -v docker.exe && export DOCKER_HOST=tcp://localhost:2375

4. docker run hello-world

$ curl -LRsS https://get.docker.com/ | bash
   :
   :
$ docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the “hello-world” image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

負けた。


2019-06-18 ここまで追記。

以下、元の記事を残しますが、非推奨です。


「Ubuntu いくつと Docker いくつ、特定のバージョン同士だと動作する!」とか「DOCKER_HOST=tcp://localhost:2375」みたいなのに疲れた人用。

1. (インストール済みの場合) WSL 上でインストールした Docker を削除する

最終的に以下な状態になれば OK 。

$ docker
docker: command not found

お手元の環境に合わせて適切に削除してください。

apt purge docker.io
apt purge docker-ce

など。

2. Docker for Windows をインストールする

こちら

コマンドプロンプト上から docker.exe が実行できることを確認して下さい。

C:\Users\djeeno>docker.exe

Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

...

実行できない場合は PATH が通ってないです。
Docker for Windows インストール後に一度も再起動していない場合は、再起動して OS 環境変数の PATH を読み直し。

3. WSL の PATH の通ったディレクトリ配下に以下のファイルを設置する。

こちら

手っ取り早く /usr/local/bin/docker にインストールしたい場合は以下を実行してください。

sudo curl -LRsS https://raw.githubusercontent.com/djeeno/windows/master/docker -o /usr/local/bin/docker
sudo chmod +x /usr/local/bin/docker

以下、ファイルの中身を転記。

#!/bin/sh

# This script executes a command with the same file name as the executable file itself,
# or a file name with ".exe" added after that.
# It's just like BusyBox.

command_name=$(basename "$0")

if command -v "${command_name}.exe" >/dev/null; then
  exec "${command_name}.exe" "$@"
else
  exec "${command_name}" "$@"
fi

ざっくり言うと、このファイルを docker という名前で実行すると、 PATH 配下にある docker.exe を実行してくれます。

おわり

$ type docker
docker is /usr/local/bin/docker

$ docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

シンプル。

2019年3月21日木曜日

Docker コンテナが [!!!!!!] Failed to early mount API filesystems, freezing. と言って死ぬときの回避策

TL;DR

コンテナを init で起動すると死ぬ。

$ docker run -it ubuntu:16.04 init
[!!!!!!] Failed to early mount API filesystems, freezing.

--cap-add=SYS_ADMIN-v /sys/fs/cgroup:/sys/fs/cgroup:ro を付けたら行けた。

$ docker run -it --cap-add=SYS_ADMIN -v /sys/fs/cgroup:/sys/fs/cgroup:ro ubuntu:16.04 init
systemd 229 running in system mode. (+PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ -LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD -IDN)
Detected virtualization vm-other.
Detected architecture x86-64.

Welcome to Ubuntu 16.04.5 LTS!

 ...

何か起きたか

ansiblesystemd 辺りをセットアップする playbook を書いたため検証しようと思ったが、わざわざ仮想マシンを起こすのもめんどくさかった。
ので、コンテナの中で systemd を、ひいては init を起こした。

docker run -it ubuntu:16.04 init

すると、 [!!!!!!] Failed to early mount API filesystems, freezing. と出力して死んだ。

何が問題だったか

どうやらコンテナ起動コマンドを init にすると、諸々の権限が足りず起動に失敗するらしい。
( 詳細までは元気が足りず調べられていない )

解決策 or 回避策

docker run するときに capability SYS_ADMIN を追加した上で、/sys/fs/cgroup を読み込み専用でマウントしてやると起動できた。

docker run -it --cap-add=SYS_ADMIN -v /sys/fs/cgroup:/sys/fs/cgroup:ro ubuntu:16.04 init

備考

最初に Google 先生に聞いてみたときには、 docker run するときに --privileged オプションを付けて特権モードで起動しろなどという恐ろしい助言が出てきたが、試してみると確かに init は起こせた。

しかし、複数のコンテナを --privileged で起動すると謎の CPU の高騰に悩まされた。
( 確か。結構昔のことなので正確に覚えていない。 )

できることなら特権を与えるなんて危険なことはしたくなかったので、もうちょっと権限を絞りたいと思って色々試したところ、↑な感じで行けた。

SYS_ADMIN よりも細かく capabilities を絞ってできるかどうかは試せていない。


参考

2019年3月16日土曜日

tcpdump を時限開始して自動で停止させる

tcpdump を時間を指定して実行したいけど、 わざわざ cron を設定したくなかった。

以下例では、 2019 年 3 月 16 日 10:00 AM に tcpdump を開始し、 300 秒後に終了する。

while sleep 0.1; do
  if [ $(date +%Y%m%d%H%M) -ge 201903161000 ]; then
    sudo tcpdump -G300 -W1 -vvv -n -p -s 65535 -w "$(uname -n)_%Y%m%dT%H%M%S.pcap"
    break
  fi
done
  • tcpdump
    • -G {数字}: {数字}秒間実行する
    • -W {数字}: {数字}回ローテートする
    • -n: IPアドレスやポートをそのまま表示する
    • -p: プロミスキャスモード を有効にしない
    • -s: キャプチャするサイズを指定する ( MTUより大きくすれば OK )

2019年3月15日金曜日

バージョニングが有効化されている S3 バケットを削除したい

バージョニングが有効化されている S3 バケットを削除したいと思ったのですが、aws s3api に見当たらなかったので boto3 で簡単に書きました。

以下を delete-bucket.py などとして実行します。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import boto3
from boto3.session import Session

boto3.set_stream_logger()

session = Session(profile_name='YOUR_AWS_PROFILE_NAME')
s3 = session.resource('s3')
bucket = s3.Bucket('YOUR_S3_BUCKET_NAME')

# 全オブジェクトの削除
bucket.object_versions.delete()

# バケットの削除
bucket.delete()

実行するときの注意

上のまま実行すると、標準出力に大量のログ(削除したオブジェクトのバージョンの一覧)が出力されるので、以下のようにして実行することをおすすめします。

$ python ./delete-bucket.py | gzip -c >./delete-bucket_$(date +%Y%m%d_%H%M%S).log.gz

参考

2019年3月13日水曜日

diff コマンドに色をつけたいけど colordiff みたいな別のコマンドはインストールしたくない

色付き diff が見たいたいけど colordiff みたいな別のコマンドはインストールしたくないシーンがあったので、 プレーンな diff コマンドに bash で、というか主に sed で、無理やり色をつけました。
ssh先 ( 最近だと kubectl exec ですかね ) のサーバーで色付き diff が欲しい時に重宝します。

difff() {(R=$(printf '\e[31m');G=$(printf '\e[32m');B=$(printf '\e[36m');W=$(printf '\e[1m');N=$(printf '\e[0m');diff -u "$@"|sed $(uname -s|grep -q '^Darwin'&&printf -- -E||printf -- -r) "s/^(@@.+@@|@@.+@@)$/$B\1$N/g;s/^(\+.*)$/$G\1$N/g;s/^(\-.*)$/$R\1$N/g;s/^[^\+\-]*((\+{3}|-{3}) [^ ].*)/$W\1/g;")}

実行例はこちら。

$ difff A.txt B.txt

git diff で見慣れた感じで出すようにしています。

Ubuntu 16.04, 同 18.04, CentOS 7, Alpine Linux (2019-03-12 時点で latest で降ってきたやつ) 辺りで動作確認済み。

-r オプションないし -E オプションがある sed + POSIX な環境であれば動くんじゃないかなと思います。

production 環境で apt install colordiff とか yum install colordiff とか apk --add colordiff なんて罪深いことはやりたくないです。

2018年7月29日日曜日

Mackerel で snap のループデバイス /dev/loop の filesystem Usage 100% を 無視する

TL;DR

mackerel-agent.conf に以下を追記

[filesystems]
ignore = "/dev/loop.*"

何か起きたか

新しいAMIからEC2を起動したらsnap でインストールされたサービスを起動すると、
空き容量には余裕があるにもかかわらず、 disk Usage 100% のアラートが発報される。

何が問題だったか

謎のデバイス /dev/loop0/dev/loop1 が増えており、かつ、それらのディスク使用率が 100% になっていた。

何ものかと調べたところ、どうやら Snappy のものらしい。

snap がデーモンを起こす際 /dev/loop[0-9]+ な命名の ループデバイスを Read Only でマウントするようで、
このデバイスの見かけ上の Usage が 100% なためdisk full アラートが発報されているようだ

解決策

mackerel-agent.conf に以下を追記。

[filesystems]
ignore = "/dev/loop.*"

これで snap がマウントするループデバイスの監視を無視することができる。

備考

なお今回の事の発端は、突然 Ubuntu 16.04 ( と Ubuntu 18.04 ) の
最新 AMI に ssm-agent がプリインストールされるようになった
こと。
この ssm-agent が snap でインストールされている。

2018年5月4日金曜日

【CentOS7】エフェメラルポートを除いた全てのアウトバウンド通信をDROP(もしくはREJECT)

「指定のポート以外の外向きの通信を禁止したい」
「Outbound通信のポートを全塞ぎしたい」
からと言って、安易に以下のように全通信をDROPしてはいけません

firewall-cmd --permanent --direct --add-rule ipv4 filter OUTPUT 65498 -p tcp -j REJECT

エフェメラルポートまで閉じてしまうと、
内→外全ての通信が死んでしまうので…。

アウトバウンドのポートを閉じたいときは、
事前に必ずエフェメラルポートを開ける設定を入れなければいけません

加えて、エフェメラルポートを開けるルールは、
全通信をDROPするルールよりも優先度を高くしなければいけません

(ファイアウォールルールの優先度ですが、 65498 とか 65499 とか、この記事では適当な数字を使っています)


というわけで、CentOS7でエフェメラルポートを開けた後、全通信をDROPします。

エフェメラルポートを確認する

sysctl コマンドから

sysctl -a 2> /dev/null | grep net.ipv4.ip_local_port_range

proc ファイルシステムから

cat /proc/sys/net/ipv4/ip_local_port_range

以下、実行結果。

# cat /proc/sys/net/ipv4/ip_local_port_range
32768   60999

CentOS7 デフォルトのエフェメラルポートは
32768 から 60999 までのようです。

エフェメラルポートを開放する

上記で確認した ip_local_port_range を元に、許可ルールを追加します。

firewall-cmd --permanent --direct --add-rule ipv4 filter OUTPUT 65498 -p tcp --dport 32768:60999 -j ACCEPT

全通信をDROPする

firewall-cmd --permanent --direct --add-rule ipv4 filter OUTPUT 65499 -p tcp -j REJECT

reload して設定を反映する

firewall-cmd --reload

ついでにループバック通信を許可する

firewall-cmd --permanent --direct --add-rule ipv4 filter OUTPUT 65497 -p tcp -d 127.0.0.0/8 -j ACCEPT
firewall-cmd --reload

2018年4月1日日曜日

AWS ElastiCache Redis クラスターのメンテナンスについて

  • replica ロールのノード宛のメンテナンスの場合は瞬断も発生しない(若干の性能劣化はある)。特段対応は必要無し。
  • primary ロールのノード宛のメンテナンスの場合は、 Redis Cluster が有効/無効か、 MultiAZ が有効/無効かによって対応が変わる。
  • 詳細はこちら: ノードの置換 - Redis 用 Amazon ElastiCache

2018年2月6日火曜日

【1分かからずにできる】Linuxから(curlで)LINEに通知メッセージを送る - LINE Notify

めちゃくそ簡単でした。

以下のURLにアクセスします。

https://notify-bot.line.me/my/

「トークンを発行する」をクリックします。

「トークン名」を入力し、

通知の送信先を選択し、トークンを発行します。

※自身もしくはグループのみが選択できます。
 画像は自身への通知(1:1でLINE Notifyから通知を受け取る)を選択しています。

「発行したトークン」をコピーします。

以下を実行します。

curl https://notify-api.line.me/api/notify -X POST -H 'Authorization: Bearer <発行したトークン>' -F 'message=帰りたい'

おわり。

以下、レスポンス。

{"status":200,"message":"ok"}

2018年2月4日日曜日

dpkg/apt/rpm/yumでインストールしたパッケージについて、パッケージの情報やファイルのインストール場所等を確認する

パッケージをインストールしたは良いけど、
どこに何が展開されたか?を確認したいとき用。

以下コマンドで確認できる。

RedHat系の場合

rpm -lqi <パッケージ名>

Debian系の場合

dpkg -s <パッケージ名>
dpkg -L <パッケージ名>

以下、具体例。

  • rpm -lqi <パッケージ名>
$ sudo rpm -lqi openldap-clients
Name        : openldap-clients
Version     : 2.4.44
Release     : 5.el7
Architecture: x86_64
Install Date: Fri 04 Sun 2018 10:00:00 AM JST
Group       : Applications/Internet
Size        : 583815
License     : OpenLDAP
Signature   : RSA/SHA256, Fri 11 Aug 2017 03:38:04 AM JST, Key ID 24c6a8a7f4a80eb5
Source RPM  : openldap-2.4.44-5.el7.src.rpm
Build Date  : Fri 04 Aug 2017 11:24:19 PM JST
Build Host  : c1bm.rdu2.centos.org
Relocations : (not relocatable)
Packager    : CentOS BuildSystem <http://bugs.centos.org>
Vendor      : CentOS
URL         : http://www.openldap.org/
Summary     : LDAP client utilities
Description :
OpenLDAP is an open-source suite of LDAP (Lightweight Directory Access
Protocol) applications and development tools. LDAP is a set of
protocols for accessing directory services (usually phone book style
information, but other information is possible) over the Internet,
similar to the way DNS (Domain Name System) information is propagated
over the Internet. The openldap-clients package contains the client
programs needed for accessing and modifying OpenLDAP directories.
/usr/bin/ldapadd
/usr/bin/ldapcompare
/usr/bin/ldapdelete
/usr/bin/ldapexop
/usr/bin/ldapmodify
/usr/bin/ldapmodrdn
/usr/bin/ldappasswd
/usr/bin/ldapsearch
/usr/bin/ldapurl
/usr/bin/ldapwhoami
/usr/share/man/man1/ldapadd.1.gz
/usr/share/man/man1/ldapcompare.1.gz
/usr/share/man/man1/ldapdelete.1.gz
/usr/share/man/man1/ldapexop.1.gz
/usr/share/man/man1/ldapmodify.1.gz
/usr/share/man/man1/ldapmodrdn.1.gz
/usr/share/man/man1/ldappasswd.1.gz
/usr/share/man/man1/ldapsearch.1.gz
/usr/share/man/man1/ldapurl.1.gz
/usr/share/man/man1/ldapwhoami.1.gz
  • dpkg -s <パッケージ名>; dpkg -L <パッケージ名>
$ sudo dpkg -s ldap-utils
Package: ldap-utils
Status: install ok installed
Priority: optional
Section: net
Installed-Size: 675
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Architecture: amd64
Source: openldap
Version: 2.4.42+dfsg-2ubuntu3.2
Replaces: openldap-utils, openldapd, slapd (<< 2.2.23-0.pre6)
Provides: ldap-client, openldap-utils
Depends: libc6 (>= 2.14), libldap-2.4-2 (= 2.4.42+dfsg-2ubuntu3.2), libsasl2-2
Recommends: libsasl2-modules
Suggests: libsasl2-modules-gssapi-mit | libsasl2-modules-gssapi-heimdal
Conflicts: ldap-client, openldap-utils, umich-ldap-utils
Description: OpenLDAP utilities
 This package provides utilities from the OpenLDAP (Lightweight
 Directory Access Protocol) package. These utilities can access a
 local or remote LDAP server and contain all the client programs
 required to access LDAP servers.
Homepage: http://www.openldap.org/
Original-Maintainer: Debian OpenLDAP Maintainers <pkg-openldap-devel@lists.alioth.debian.org>

$ dpkg -L ldap-utils
/.
/usr
/usr/bin
/usr/bin/ldapmodify
/usr/bin/ldapexop
/usr/bin/ldapurl
/usr/bin/ldapsearch
/usr/bin/ldapwhoami
/usr/bin/ldapcompare
/usr/bin/ldappasswd
/usr/bin/ldapmodrdn
/usr/bin/ldapdelete
/usr/share
/usr/share/man
/usr/share/man/man5
/usr/share/man/man5/ldif.5.gz
/usr/share/man/man1
/usr/share/man/man1/ldapmodify.1.gz
/usr/share/man/man1/ldappasswd.1.gz
/usr/share/man/man1/ldapmodrdn.1.gz
/usr/share/man/man1/ldapsearch.1.gz
/usr/share/man/man1/ldapcompare.1.gz
/usr/share/man/man1/ldapdelete.1.gz
/usr/share/man/man1/ldapadd.1.gz
/usr/share/man/man1/ldapwhoami.1.gz
/usr/share/man/man1/ldapurl.1.gz
/usr/share/man/man1/ldapexop.1.gz
/usr/share/doc
/usr/share/doc/ldap-utils
/usr/share/doc/ldap-utils/README.Debian
/usr/share/doc/ldap-utils/copyright
/usr/bin/ldapadd
/usr/share/doc/ldap-utils/changelog.Debian.gz

2018年2月3日土曜日

【コピペ用Tips】ldapsearchコマンド

# vars
_ldapuri=ldap://ldap.example.com/
_binddn=cn=user01,ou=administrators,dc=example,dc=com
_basedn=dc=example,dc=com

# command
ldapsearch -v -LLL -H $_ldapuri -D $_binddn -W -b $_basedn "(objectClass=*)"

ちょっとだけ解説。

  • -v: 詳細を出力する(stderrへ出力)
  • -LLL: レスポンスをコメントなしでLDIF形式で出力する
  • -H: 接続するLDAPサーバのURIを指定
  • -D: bindユーザのDN(distinguished name)を指定
  • -W: bindユーザのパスワードをプロンプトから入力する
  • -b: サーチベースDNを指定
  • "(...)": LDAPフィルタ

Ubuntu、CentOSへのインストールについてはこちら

2018年2月2日金曜日

Ubuntuにldapsearch/ldapadd/ldapmodify/ldapwhoami等各種コマンドをインストール

結論から。

sudo apt -y install ldap-utils

無事インストールできました。

$ ldapsearch -V
ldapsearch: @(#) $OpenLDAP: ldapsearch  (Ubuntu) (May 30 2017 19:20:53) $
        buildd@lgw01-18:/build/openldap-JXEADB/openldap-2.4.42+dfsg/debian/build/clients/tools
        (LDAP library: OpenLDAP 20442)

CentOS だったらこんな感じですね。

sudo yum -y install openldap-clients
$ ldapsearch -V
ldapsearch: @(#) $OpenLDAP: ldapsearch 2.4.44 (Aug  4 2017 14:23:23) $
        mockbuild@c1bm.rdu2.centos.org:/builddir/build/BUILD/openldap-2.4.44/openldap-2.4.44/clients/tools
        (LDAP library: OpenLDAP 20444)

普段 CentOS ユーザなので、ちょっと迷いました。

$ sudo apt list *openldap*
Listing... Done
golang-openldap-dev/xenial 0.2-1 all

$ sudo apt list *ldap*
Listing... Done
aolserver4-nsldap/xenial 0.8-4build1 amd64
argonaut-ldap2zone/xenial 0.9.2-1 all
autofs-ldap/xenial-updates 5.1.1-1ubuntu3.1 amd64
autofs5-ldap/xenial-updates 5.1.1-1ubuntu3.1 all
bind9-dyndb-ldap/xenial 8.0-4 amd64
buildapp/xenial 1.5.5-1 amd64
courier-authlib-ldap/xenial 0.66.4-3build1 amd64
courier-ldap/xenial 0.68.2-1ubuntu7 amd64
dlz-ldap-enum/xenial 1.0.3-1 amd64
dovecot-ldap/xenial-updates,xenial-security 1:2.2.22-1ubuntu2.4 amd64
erlang-eldap/xenial 1:18.3-dfsg-1ubuntu3 amd64
freeradius-ldap/xenial-updates,xenial-security 2.2.8+dfsg-0.1ubuntu0.1 amd64
fts-fai-ldap/xenial 1.1-2 all
fts-ltsp-ldap/xenial 1.1-2 all
fusiondirectory-plugin-ldapdump/xenial 1.0.8.8-3ubuntu2 all
fusiondirectory-plugin-ldapmanager/xenial 1.0.8.8-3ubuntu2 all
fusionforge-plugin-authldap/xenial 6.0.3+20151023-1ubuntu1 all
fusionforge-plugin-sysauthldap/xenial 6.0.3+20151023-1ubuntu1 all
golang-github-go-ldap-ldap-dev/xenial 0.0~git20150817.24.12f2865-1 all
golang-openldap-dev/xenial 0.2-1 all
gosa-plugin-ldapmanager/xenial 2.7.4+reloaded2-9ubuntu1 all
isc-dhcp-server-ldap/xenial-updates 4.3.3-5ubuntu12.7 amd64
jmeter-ldap/xenial 2.11-5 all
kamailio-ldap-modules/xenial 4.3.4-1.1ubuntu2 amd64
krb5-kdc-ldap/xenial-updates 1.13.2+dfsg-5ubuntu2 amd64
lcmaps-plugins-basic-ldap/xenial 1.6.1-3 amd64
ldap-account-manager/xenial 5.2-1ubuntu1 all
ldap-account-manager-lamdaemon/xenial 5.2-1ubuntu1 all
ldap-auth-client/xenial 0.5.3 all
ldap-auth-config/xenial 0.5.3 all
ldap-git-backup/xenial 1.0.7-1 all
ldap-utils/xenial-updates,xenial-security 2.4.42+dfsg-2ubuntu3.2 amd64
ldap2dns/xenial 0.3.1-3.1 amd64
ldap2zone/xenial 0.2-8 amd64
ldapscripts/xenial 2.0.6-1ubuntu1 all
ldaptor-doc/xenial 0.0.43+debian1-7 all
ldaptor-utils/xenial 0.0.43+debian1-7 all
ldapvi/xenial 1.7-10 amd64
lemonldap-ng/xenial 1.4.6-3 all
lemonldap-ng-doc/xenial 1.4.6-3 all
libapache-authznetldap-perl/xenial 0.07-6 all
libapache-session-ldap-perl/xenial 0.4-1 all
libapache2-mod-ldap-userdir/xenial 1.1.19-2.1 amd64
libapache2-mod-ldap-userdir-dbg/xenial 1.1.19-2.1 amd64
libapache2-mod-vhost-ldap/xenial 2.4.0-1 amd64
libapache2-mod-webauthldap/xenial 4.7.0-3build1 amd64
libaprutil1-ldap/xenial 1.5.4-1build1 amd64
libauthen-simple-ldap-perl/xenial 0.3-1 all
libdbd-ldap-perl/xenial 0.20-1 all
libghc-ldap-dev/xenial 0.6.10-5 amd64
libghc-ldap-doc/xenial 0.6.10-5 all
libghc-ldap-prof/xenial 0.6.10-5 amd64
libkf5ldap-dbg/xenial 15.12.3-0ubuntu1 amd64
libkf5ldap-dev/xenial 15.12.3-0ubuntu1 amd64
libkf5ldap5/xenial 15.12.3-0ubuntu1 amd64
libkldap4/xenial 4:4.14.10-1ubuntu2 amd64
libldap-2.4-2/xenial-updates,xenial-security,now 2.4.42+dfsg-2ubuntu3.2 amd64 [installed]
libldap-2.4-2-dbg/xenial-updates,xenial-security 2.4.42+dfsg-2ubuntu3.2 amd64
libldap-java/xenial 4.18+dfsg1-1 all
libldap-ocaml-dev/xenial 2.1.8-10build1 amd64
libldap2-dev/xenial-updates,xenial-security 2.4.42+dfsg-2ubuntu3.2 amd64
liblemonldap-ng-common-perl/xenial 1.4.6-3 all
liblemonldap-ng-conf-perl/xenial 1.4.6-3 all
liblemonldap-ng-handler-perl/xenial 1.4.6-3 all
liblemonldap-ng-manager-perl/xenial 1.4.6-3 all
liblemonldap-ng-portal-perl/xenial 1.4.6-3 all
libmono-ldap4.0-cil/xenial 4.2.1.102+dfsg2-7ubuntu4 all
libmono-system-ldap-protocols4.0-cil/xenial 4.2.1.102+dfsg2-7ubuntu4 all
libmono-system-ldap4.0-cil/xenial 4.2.1.102+dfsg2-7ubuntu4 all
libmozilla-ldap-perl/xenial 1.5.3-2build2 amd64
libnet-ldap-filterbuilder-perl/xenial 1.0004-1 all
libnet-ldap-perl/xenial 1:0.6500+dfsg-1 all
libnet-ldap-server-perl/xenial 0.4-2 all
libnet-ldapapi-perl/xenial 3.0.3-7build5 amd64
libnss-ldap/xenial 265-3ubuntu2 amd64
libnss-ldapd/xenial 0.9.6-3 amd64
libpam-ldap/xenial 184-8.7ubuntu1 amd64
libpam-ldapd/xenial 0.9.6-3 amd64
libroot-net-ldap-dev/xenial 5.34.30-0ubuntu8 amd64
libroot-net-ldap5.34/xenial 5.34.30-0ubuntu8 amd64
libsasl2-modules-ldap/xenial 2.1.26.dfsg1-14build1 amd64
libtest-net-ldap-perl/xenial 0.07-1 all
libvt-ldap-java/xenial 3.3.8-1 all
libvt-ldap-java-doc/xenial 3.3.8-1 all
lua-ldap/xenial 1.1.0-1-geeac494-6 amd64
lua-ldap-dev/xenial 1.1.0-1-geeac494-6 amd64
nordugrid-arc-ldap-infosys/xenial 5.0.5-1ubuntu1 all
nordugrid-arc-ldap-monitor/xenial 5.0.5-1ubuntu1 all
openvpn-auth-ldap/xenial-updates 2.0.3-6.1ubuntu0.16.04.1 amd64
pdns-backend-ldap/xenial 4.0.0~alpha2-3build1 amd64
perdition-ldap/xenial 2.1-2build1 amd64
phamm-ldap/xenial 0.6.2-1.2ubuntu1 all
phamm-ldap-amavis/xenial 0.6.2-1.2ubuntu1 all
phamm-ldap-vacation/xenial 0.6.2-1.2ubuntu1 all
php-horde-ldap/xenial 2.3.2-1ubuntu1 all
php-ldap/xenial 1:7.0+35ubuntu6 all
php-net-ldap/xenial 1:1.1.5-3ubuntu1 all
php-net-ldap2/xenial 2.2.0-1ubuntu1 all
php-net-ldap3/xenial 1.0.3-1build1 all
php7.0-ldap/xenial-updates,xenial-security 7.0.22-0ubuntu0.16.04.1 amd64
phpldapadmin/xenial-updates,xenial-security 1.2.2-5.2ubuntu2.1 all
postfix-ldap/xenial-updates 3.1.0-3ubuntu0.2 amd64
proftpd-mod-ldap/xenial 1.3.5a-1build1 amd64
pure-ftpd-ldap/xenial 1.0.36-3.2build1 amd64
python-django-auth-ldap/xenial 1.2.7+dfsg-1 all
python-django-auth-ldap-doc/xenial 1.2.7+dfsg-1 all
python-django-ldapdb/xenial 0.2.0-1 all
python-django-python3-ldap/xenial 0.9.8-1 all
python-ldap/xenial 2.4.22-0.1 amd64
python-ldap-dbg/xenial 2.4.22-0.1 amd64
python-ldap3/xenial 1.0.3-1 all
python-ldappool/xenial 1.0-1ubuntu1 all
python-ldaptor/xenial 0.0.43+debian1-7 all
python-mockldap/xenial 0.2.5-1 all
python-mockldap-doc/xenial 0.2.5-1 all
python-schooltool.ldap/xenial 1.0.2-0ubuntu1 all
python3-django-python3-ldap/xenial 0.9.8-1 all
python3-ldap3/xenial 1.0.3-1 all
ruby-activeldap/xenial 4.0.3-2 all
ruby-activeldap-doc/xenial 4.0.3-2 all
ruby-ldap/xenial 0.9.16-1build6 amd64
ruby-net-ldap/xenial 0.8.0-1 all
ruby-omniauth-ldap/xenial 1.0.4-4 all
shelldap/xenial 1.3.1-2 all
simpleid-ldap/xenial 1.0.1-1ubuntu1 all
smbldap-tools/xenial-updates 0.9.9-1ubuntu1.16.04.2 all
sssd-ldap/xenial-updates,xenial-security 1.13.4-1ubuntu1.10 amd64
strongswan-plugin-ldap/xenial-updates 5.3.5-1ubuntu3.5 all
sudo-ldap/xenial-updates 1.8.16-0ubuntu1.5 amd64
tryton-modules-ldap-authentication/xenial 3.8.0-1 all
uwsgi-plugin-ldap/xenial-updates 2.0.12-5ubuntu3.1 amd64
web2ldap/xenial 1.1.43~dfsg-1 all

# どれ・・・?

2018年2月1日木曜日

Windows Subsystem for Linux (WSL, 旧 Bash on Windows) でデフォルトで使用するユーザを変更する。

以下を実行します。

ubuntu config --default-user djeeno

内訳は以下。

<インストールしたWSLのディストリビューション> --default-user=<ユーザ名>

ユーザのパスワードがわからなくなったときの初期化用途にも使えるようですね。

ubuntu config --default-user root

デフォルトユーザを root に戻して、 passwd コマンドでパスワード初期化、って流れらしいです。


ちなみに、Bash on Windows時代は、以下コマンドで変更していました。

lxrun /setdefaultuser djeeno

現時点 (2018/02/01) では、実行してもエラーになるだけで、変更できません。

C:\> lxrun /setdefaultuser djeeno
警告: lxrun.exe は、Linux ディストリビューションのためにレガシ Windows サブシステムを構成するだけに使用されます。
ディストリビューションは次の Windows ストアを訪問してインストールすることができます:
https://aka.ms/wslstore

エラー: 0x80070002

2018年1月31日水曜日

Webページを完全に単一のHTMLファイルで保存(cssや画像もhtml内に埋め込み)

Chromeの話です。

Ctrl+Sで保存しようとすると、以下2通りの選択があります。

  • ウェブページ、HTMLのみ
  • ウェブページ、完全

しかし何れも一長一短で、
「HTMLのみ」は、単純にhtmlファイルダウンロードするだけなので、cssやなどはリンクのままになり、
逆に「完全」は、リンクされているcssやイメージ画像などを全てダウンロードしてしまいます。


そこでこちら。

  • ウェブページ、1つのファイル

こいつを使うと、cssはファイル内に埋め込まれ、
画像ファイルはbase64変換され、
ひとつのHTMLファイルとしてWebページを保存できます。

Web魚拓などにも使われている技術らしいですね。
絶対にロストしたくないページについてはこれで
ローカルにでもアーカイブするのがよさそうです。


以下、機能の有効化の手順です。

まずは以下URLをChromeで開きます。

chrome://flags/#save-page-as-mhtml

すると以下画面が表示されるため、 Sage Page as MHTML
有効にするをクリックします。

☑ Sage Page as MHTML チェックが入ったことを確認し、
今すぐ再起動をクリックします。

以後、 ウェブページ、1つのファイル が利用できるようになります。


MHTMLってのがあるんですね。

2018年1月30日火曜日

ntpdateコマンドの代わりにchronyで強制同期

chronyでntpdateコマンドの代わりに強制同期

時刻を強制同期するときにはCentOS6まではntpdateコマンドを使用していましたが、
CentOS7からntpdがプリインストールされなくなり、代わりに
chronyがインストールされるようになりました。

今までは以下で強制的に時刻同期していましたが、
ntpdateコマンドもntpdと同様デフォルトでは入っていないため、
とりあえず以下打って強制同期、ってことができなくなりました。

ntpdate ntp.nict.jp

同等のことをchronyで行うには、以下を実行します。

chronyc makestep

以下のように応答があれば問題なく同期できています。

$ sudo chronyc makestep
200 OK

以下のように出力される場合、多分chronydが起動していません。

$ sudo chronyc makestep
506 Cannot talk to daemon
$ sudo systemctl status chronyd
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Wed 2018-01-30 09:00:00 JST; 30s ago
     Docs: man:chronyd(8)
           man:chrony.conf(5)
  Process: ***** ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
  Process: ***** ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: ***** (code=exited, status=0/SUCCESS)
     :
     :

-dオプションでデバッグ出力。

$ sudo chronyc -d makestep
Resolved 127.0.0.1 to 127.0.0.1
Resolved ::1 to ::1
Opening connection to /var/run/chrony/chronyd.sock
Could not connect socket : No such file or directory
Opening connection to 127.0.0.1:323
Sent 28 bytes
Timeout 0.999909 seconds
Could not receive : Connection refused
Sent 28 bytes
Timeout 1.999987 seconds
Could not receive : Connection refused
Sent 28 bytes
Timeout 3.999990 seconds
Could not receive : Connection refused
Opening connection to ::1:323
Sent 28 bytes
Timeout 0.999927 seconds
Could not receive : Connection refused
Sent 28 bytes
Timeout 1.999986 seconds
Could not receive : Connection refused
Sent 28 bytes
Timeout 3.999988 seconds
Could not receive : Connection refused
506 Cannot talk to daemon

デーモンを起動してリトライ。

$ sudo systemctl start chronyd
$ sudo chronyc makestep
200 OK

どこと同期しているか、は以下で確認できます。

chronyc sources

以下、出力例。

$ sudo chronyc sources
210 Number of sources = 4
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^+ nipper.paina.net              2   6   377    26   +779us[ +793us] +/-   27ms
^+ y.ns.gin.ntt.net              2   6   377    25   -434us[ -434us] +/-  101ms
^+ 122x215x240x52.ap122.ftt>     2   6   377   157   -397us[ -373us] +/-   49ms
^* timpany.srv.jre655.com        2   6   377    26   -820us[ -806us] +/-   47ms

2018年1月29日月曜日

jqコマンドが入っていない環境でJSONを整形

jqコマンドなんてバイナリ一個なんだから何も考えず放り込んじゃっていいじゃん…
って思うんですけど、運用ポリシー的にNGな環境も有るんですよね…。

そんなイケてないポリシーの方をなんとかしろっていう議論は置いておき、
標記、jqコマンドが入っていない環境でJSONを整形する方法です。

めっちゃ簡単です。

19.2. json — 19.2.5. コマンドラインインターフェイス — Python 3.6.3 ドキュメント

JSONをパイプで以下コマンドに渡すだけ。

python -m json.tool

以下、例。

$ echo '{"date":"2018-01-29T10:00:00+0900","place":"workplace","status":["sleepy","hungry","wanna go home"]}' | python -m json.tool
{
    "date": "2018-01-29T10:00:00+0900",
    "place": "workplace",
    "status": [
        "sleepy",
        "hungry",
        "wanna go home"
    ]
}

いい感じにパースしてくれます。

「filterが無い」とか「色が付かない」とか言うわがままは我慢します。
あるだけマシです。

2018年1月28日日曜日

CIDR表記のネットワークアドレスを計算する/指定したネットワークに属するIPアドレス一覧を出力する/そのIPアドレス群のPing応答を確認するスクリプト

CIDR表記のネットワークのネットワークアドレスを出力したり、
ブロードキャストアドレス以外のIPアドレスを一覧出力したり、
またそのIPアドレス群のPing応答を確認したりするbashスクリプトを、
個人的に欲しかったので、作りました。

https://gist.github.com/djeeno/fcd09b666a8e7bd6fc356b5f410f0ff5

CentOS7、Ubuntu16.04、Ubuntu16.04(WSL)、Debian9、macOS High Sierraで動作を確認しています。

使い方を以下に例示。

# IPアドレスを一覧出力: netaddr
$ ip.sh netaddr 192.168.100.50/29
192.168.100.48

# IPアドレスを一覧出力: list
$ ip.sh list 192.168.100.0/29
192.168.100.49
192.168.100.50
192.168.100.51
192.168.100.52
192.168.100.53
192.168.100.54

# IPアドレスに対してPingを飛ばし応答のあったアドレスを出力: inuse
$ ip.sh ping 192.168.100.1/29
192.168.100.49  ttl=64 Linu   time=7.43ms
192.168.100.50  ttl=128 Windows time=8.88ms
192.168.100.54  ttl=64   Linux    time=5.69ms

Ping応答のttlからOSを(ある程度)判別できるので、
「これが何のOSなのか」も横に出力するようにしました。

(と言っても、Linux or UNIX or Windowsの3択ですが…)


適当に立てたマシンがDHCPからどのIPアドレスを振られているか調べる時とか、非常に便利です。