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

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

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

2018年1月20日土曜日

LinuxのコマンドでGithubに登録したSSH公開鍵を確認/ダウンロードする方法

LinuxのコマンドでGithubに登録したSSH公開鍵を確認/ダウンロードする方法。

Githubに登録したSSH公開鍵は以下のURLとして公開されています。

https://github.com/$USERNAME.keys

よってcurlなりwgetなりでその内容を取得することができます。

curlの場合。

curl https://github.com/djeeno.keys

wgetの場合。

wget -qO- https://github.com/djeeno.keys

ただ、人によっては複数の公開鍵が登録されているなど、
そのままauthorized_keys等に投げ込むには若干厄介かと。
(少なくとも僕はそうです。)

なので、僕はいつもGithub Pagesでkeysってファイルを公開してます。

curl https://djeeno.github.io/keys

2018年1月19日金曜日

Linuxのコマンドで自分のグローバルIPアドレスを調べる方法(+おまけ【cli.fyi】)

Linuxのコマンドで自分のグローバルIPアドレスを調べる方法(+おまけ【cli.fyi】)。

curl checkip.amazonaws.com
curl cli.fyi/me
curl ifconfig.io
curl ifconfig.me
curl ipinfo.io

bashスクリプトに組み込む時などは、
一番可用性が高そうなcheckip.amazonaws.comを使っています。
ちょっと長いですが。


cli.fyi(cli for your information)は他にも色々できるみたいです。
公式 https://cli.fyi/ を見ると色々載っています。
以下、参考。

  • Crypto Currency Prices(暗号通貨の現在価格)
    • curl cli.fyi/BTC
  • Email Address Information
    • curl cli.fyi/john.doe@10minutemail.com
  • IP Address Information
    • curl cli.fyi/8.8.8.8
  • Media/URL Information
    • curl cli.fyi/https://vimeo.com/231191863
  • Client Information
    • curl cli.fyi/me
  • Domain Whois / DNS information
    • curl cli.fyi/github.com
  • Date/Time Information
    • curl cli.fyi/time
  • Programming Language Links
    • curl cli.fyi/PHP
  • Country Information
    • curl cli.fyi/united-states
  • Popular Emojis(絵文字)
    • curl cli.fyi/emojis
  • Help
    • curl cli.fyi/help

詳細はこちらから。

2018年1月17日水曜日

【ワンライナー】カレントディレクトリ配下のディレクトリ群をzip

【ワンライナー】カレントディレクトリ配下のディレクトリ群をzip


find ./* -maxdepth 1 -type d | xargs -P254 -I@ zip -qr "@.zip" "@"
  • findコマンド
    • -maxdepthオプションで階層を指定。
      • 今回の-maxdepth 1の場合、探しに行くのは1階層まで。
    • -typeオプションで対象を指定。
      • dでディレクトリ、fでファイルを示す。
  • xargsコマンド
    • 今回はパイプ|から渡された標準入力を処理している。
    • -Iオプションで、標準入力から受け取った内容を展開する。
      • 今回の-I@の場合、zip -qr "@.zip" "@"は以下のように展開される。
        • zip -qr ディレクトリ名.zip ディレクトリ名
    • -Pオプションで、複数プロセスを立ち上げ並列処理を実施する。
      • 今回の-P254の場合、254個まで並列化する。

昔はこんな感じでやってました。拙い感じが懐かしいです。

ls | while read LINE; do zip -qr "$LINE".zip "$LINE"; done

2018年1月15日月曜日

【ワンライナー】世界一簡単なSELinuxの無効化

【ワンライナー】世界一簡単なSELinuxの無効化

早速書くことがないので、SELinuxの無効化のワンライナーでも書いてお茶を濁します。

test -r /etc/selinux/config && sudo su -c '(grep -q ^SELINUX=enforcing /etc/selinux/config && { sed "s/^\(SELINUX=\)\(enforcing\)/$(date +\#\ \%Y\%m\%d\ \#)\1\2\n\1disabled/g" $_ -i_$(date +\%Y\%m\%d_\%H\%M\%S).backup ; setenforce 0 ; })'

動作としては以下。

  • 何かしらのスクリプトに組み込む際、複数ディストリビューションに対応するため、最初に/etc/selinux/configの存在確認を行う。
  • /etc/selinux/configファイルにSELINUX=enforcingもしくはSELINUX=permissiveの記載があれば、
    • /etc/selinux/config_YYYYmmdd_HHMMSS.backupとして、/etc/selinux/configファイルのバックアップを取得する。
    • SELINUX=enforcingもしくはSELINUX=permissiveをコメントアウトし、SELINUX=disabledを追記する。
  • SELinuxが起動している場合、Permissiveモードに変更する。