ラベル RHEL の投稿を表示しています。 すべての投稿を表示
ラベル RHEL の投稿を表示しています。 すべての投稿を表示

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月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年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年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アドレスを振られているか調べる時とか、非常に便利です。

2018年1月24日水曜日

世界一簡単な git コマンドのソースからのインストール for Linux

yum や apt などのパッケージ管理ツールでインストールできる git のバージョンは大抵かなり古いので、僕はいつもソースから最新版をインストールするようにしています。

sudo権限を持つユーザで以下を実行するだけ。

UbuntuなどDebian系の場合

install_git_from_source() { sudo bash -cx 'apt-get update && apt-get -y install lib{curl4-gnutls,expat1,ssl,z}-dev gcc gettext make wget && cd /usr/local/src && BASE_URL="https://www.kernel.org/pub/software/scm/git/" && wget -cSv "${BASE_URL}$(wget -qO- ${BASE_URL} | grep -o -E "git-[0-9\\.]+.tar.gz" | sort -uV | tail -n 1)" && tar vxzf $(basename $_) && cd $(basename -s .tar.gz $_) && make prefix=/usr/local all && make prefix=/usr/local install && git --version'; } && install_git_from_source

CentOSなどRHEL系の場合

install_git_from_source() { sudo bash -cx 'yum makecache fast && yum -y install {curl,expat,gettext,openssl,zlib}-devel gcc make perl-ExtUtils-MakeMaker wget && cd /usr/local/src && BASE_URL="https://www.kernel.org/pub/software/scm/git/" && wget -cSv "${BASE_URL}$(wget -qO- ${BASE_URL} | grep -o -E "git-[0-9\\.]+.tar.gz" | sort -uV | tail -n 1)" && tar vxzf $(basename $_) && cd $(basename -s .tar.gz $_) && make prefix=/usr/local all && make prefix=/usr/local install && git --version'; } && install_git_from_source

世界一簡単。

[user@localhost ~]$ git --version
git version 2.16.1