1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ラズパイにslackcatを導入する

Last updated at Posted at 2018-12-25

slackcatとは

コマンド出力をslackにポストしたり、ファイルをslackにアップロードできるツールです。
http://slackcat.chat/

導入環境

  • Raspberry Pi 3 Model B
  • OSバージョン
$ uname -a
Linux raspberrypi 4.14.79-v7  #1159 SMP Sun Nov 4 17:50:20 GMT 2018 armv7l GNU/Linux
$ grep -H "" /etc/*version ; grep -H "" /etc/*release
/etc/debian_version:9.6
/etc/os-release:PRETTY_NAME="Raspbian GNU/Linux 9 (stretch)"
/etc/os-release:NAME="Raspbian GNU/Linux"
/etc/os-release:VERSION_ID="9"
/etc/os-release:VERSION="9 (stretch)"
/etc/os-release:ID=raspbian
/etc/os-release:ID_LIKE=debian
/etc/os-release:HOME_URL="http://www.raspbian.org/"
/etc/os-release:SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
/etc/os-release:BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

インストール

https://github.com/bcicen/slackcat
のInstallingセクション通りに実行するだけ。
但し、releasesにある最新のarmバイナリを指定します。

$ curl -Lo slackcat https://github.com/bcicen/slackcat/releases/download/v1.5/slackcat-1.5-linux-arm
$ sudo mv slackcat /usr/local/bin/
$ sudo chmod +x /usr/local/bin/slackcat

設定

Configurationセクション通りに実行するとブラウザが起動してslackと連携されますが、sshでリモートで作業していてブラウザ起動ができない場合は、事前にローカル環境でslackcatのサイト ( http://slackcat.chat/ ) の右上の「Add to Slack」ボタンから連携を完了させておき、取得したtokenを入力します。

$ slackcat --configure
nickname for team: slackワークスペース名
slackcat creating token request for slackcat
slackcat Use the below URL to authorize slackcat if browser fails to launch
slackcat http://slackcat.chat/configure
token issued: (xoxp- で始まる OAuth Access TOken)
slackcat added team to config file at /home/pi/.config/slackcat/config

実行

コマンドをパイプでつなぐだけでslackにpostされるので、いざ実行!

$ date | slackcat
slackcat no channel provided

チャンネル指定が必要と言われるので~/.config/slackcat/config に default_channelを設定します。

$ cat ~/.config/slackcat/config
default_team = "ワークスペース名"
default_channel = "postしたいチャンネル名"

[teams]
 ワークスペース名 = "xoxp-で始まるOAuth Access Token" 

再度、実行!
optionでstream,tee,usernameもついでに指定。

$ date | slackcat -s -t -u "slackcat"
slackcat connected to (ワークスペース名) as (ユーザ名)
slackcat starting stream
2018年 12月 26日 水曜日 23:13:46 JST
slackcat flushing remaining messages to Slack...
slackcat posted 1 message lines to (チャンネル名)

無事、slackにもポスト成功!
スクリーンショット 2018-12-26 23.14.46.png

Tips

実行結果だけでなく、実行コマンドを残すために下記のスクリプトを使うことで実行コマンドと結果を同時にslackにpostできるようになります。

  • 数人でサーバ作業している時の実行コマンドや実行結果の共有やログ残しに使えます。
  • 1人作業でも後でまとめる為の作業ログ残しに使えます。
send2post.sh
# !/bin/bash

set -eu
echo \[$(whoami)@$(hostname -s):$([ $HOME = $PWD ] && echo "~" || echo $PWD)\] $* | slackcat -s -t -u "slackcat"
$* | slackcat -s -t -u "slackcat"

使い方

$ send2slack.sh date
slackcat connected to (ワークスペース名) as (ユーザ名)
slackcat starting stream
[pi@raspberrypi:~] date
slackcat flushing remaining messages to Slack...
slackcat flushing remaining messages to Slack...
slackcat posted 1 message lines to (チャンネル名)
slackcat connected to (ワークスペース名) as (ユーザ名)
slackcat starting stream
2018年 12月 26日 水曜日 23:19:12 JST
slackcat flushing remaining messages to Slack...
slackcat flushing remaining messages to Slack...
slackcat posted 1 message lines to (チャンネル名)
スクリーンショット 2018-12-26 23.19.38.png

※注意!
スクリプトは雑に作ったので"(ダブルクォーテーション)や'(シングルクォーテーション)を含むコマンドなどがうまく実行されないので自己責任で使用をお願いします。

おまけ

notify_slackという別ツールもありますのでお好みで。
https://github.com/catatsuy/notify_slack

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?