Skip to content

Instantly share code, notes, and snippets.

@maglub
Last active November 14, 2018 09:53
Show Gist options
  • Save maglub/3b5f4656b873669c0206aeb4a9b8b3e2 to your computer and use it in GitHub Desktop.
Save maglub/3b5f4656b873669c0206aeb4a9b8b3e2 to your computer and use it in GitHub Desktop.
slack_pipe

Introduction

Use slack_pipe to just send quoted text through a pipe to slack.

Example:

echo arne | ./slack_pipe

slack.conf

SLACK_HOSTNAME="myslackgroup.slack.com"
SLACK_CHANNEL="#ops"
SLACK_USERNAME="icinga2"
SLACK_URLs="your slack hook url"
#example:
#SLACK_URLs="https://hooks.slack.com/services/SOMETHING/SECRET/TOKEN"

slack_pipe

#!/bin/bash

this_dir=$(cd `dirname $0`;pwd)
. $this_dir/slack.conf

#================================
# MAIN
#================================ 
output=$(cat | perl -pe 's/\e\[.*?m//g' | perl -pe 's/\n/\\n/g' )

for SLACK_URL in $SLACK_URLs
do
  [ -n "$debug" ] && set -x
  curl -X POST --data "{\"channel\": \"${SLACK_CHANNEL}\", \"username\": \"${SLACK_USERNAME}\", \"text\": \"\`\`\`${output}\`\`\`\"}" "$SLACK_URL"
  [ -n "$debug" ] && set +x
cat<<EOT
EOT
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment