Skip to content

Instantly share code, notes, and snippets.

@grambas
Forked from devStepsize/slack_webhook_post.py
Last active April 11, 2019 11:55
Show Gist options
  • Save grambas/67ef18335aa4fb8576bd9a89092b0a18 to your computer and use it in GitHub Desktop.
Save grambas/67ef18335aa4fb8576bd9a89092b0a18 to your computer and use it in GitHub Desktop.
Slack
<?php
/**
Send message to slack channel.
$hook can be created at https://mindau.slack.com/apps/A0F7XDUAZ-incoming-webhook
*/
function sendLog($title, $msg)
{
$hook = "https://hooks.slack.com/services/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$data = array(
'channel' => '#logging',
'username' => 'Venipak API Logger',
'mrkdown' => true,
'icon_emoji' => ':ghost:',
);
if (is_array($msg)) {
$data['text'] = $title . ' </br></br>' . json_encode($msg, JSON_PRETTY_PRINT);
} else {
$data['text'] = $title . ' </br></br>' . $msg;
}
$data_string = json_encode($data);
$ch = curl_init($hook);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
//Execute CURL
$result = curl_exec($ch);
}
#!/bin/sh
# Display usage information
# send notifications from Linux fail2ban, ssh auth and others actions to Slack
function show_usage {
echo "Default Usage: $0 [subject] [action] [msg]"
echo "Example: $0 DATABASE backup succefully"
echo "Custom actions: $0 fail2brain start|stop|ban (ip)|unban (ip)"
echo "Custom actions: $0 sshauth (use variables from pam_exec to generate msg)."
exit
}
# Check for script arguments
if [ $# -lt 1 ]
then
show_usage
fi
# Custom reporting
if [ "$1" = 'fail2ban' ]
then
#slack.conf start and stop not set
if [ "$2" = 'start' ]
then
message='Fail2ban just started.'
echo $message | path/to/slackpost.sh
elif [ "$2" = 'stop' ]
then
message='Fail2ban just stopped.'
echo $message | path/to/slackpost.sh
elif [ "$2" = 'ban' ]
then
message=$([ "$2" != '' ] && echo "[$1] just banned $3" || echo 'Fail2ban just banned an ip.' )
echo $message | path/to/slackpost.sh
elif [ "$2" = 'unban' ]
then
message=$([ "$2" != '' ] && echo "[$1] just unbanned $3" || echo "Fail2ban just unbanned an ip." )
echo $message | path/to/slackpost.sh
else
show_usage
fi
# Extra check if not logout (close_session)
elif [ "$1" = "sshauth" ]
then
#slack.conf start and stop not set
if [ "$PAM_TYPE" != "close_session" ]
then
#env is last cmd variables
#subject="SSH Login: $PAM_USER from $PAM_RHOST on $host"
# Message to send, e.g. the current environment variables.
message="$PAM_RHOST has just connected on $HOSTNAME with user $PAM_USER (PAM_TYPE=$PAM_TYPE)"
# message="`env`"
echo $message | path/to/slackpost.sh
fi
# Default
else
echo "[$1] action: $2 msg: $3" | path/to/slackpost.sh
fi
'''
This is an example of how to send data to Slack webhooks in Python with the
requests module.
Detailed documentation of Slack Incoming Webhooks:
https://api.slack.com/incoming-webhooks
'''
import json
import requests
# Set the webhook_url to the one provided by Slack when you create the webhook at https://my.slack.com/services/new/incoming-webhook/
webhook_url = 'https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX'
slack_data = {'text': "Sup! We're hacking shit together @HackSussex :spaghetti:"}
response = requests.post(
webhook_url, data=json.dumps(slack_data),
headers={'Content-Type': 'application/json'}
)
if response.status_code != 200:
raise ValueError(
'Request to slack returned an error %s, the response is:\n%s'
% (response.status_code, response.text)
)
#!/bin/sh
# Usage: date | sh slackpost.sh
# Usage: echo "Hello Word" | sh slackpost.sh
webhook_url=https://hooks.slack.com/services/..................
username="PI"
channel=logs
icon_emoji=":ghost:" # https://slackmojis.com/
#load all message to var
while read LINE; do
text="$text\n$LINE"
done
#Default validation
if [ "$webhook_url" = "" ]; then
echo "No webhook_url specified"
exit 1
fi
if [ "$channel" = "" ]; then
echo "No channel specified"
exit 1
fi
if [ "$text" = "" ]; then
echo "No text specified"
exit 1
fi
escapedText=$(echo $text | sed 's/"/\"/g' | sed "s/'/\'/g" )
json="{\"channel\": \"$channel\",\"username\": \"$username\",\"icon_emoji\": \"$icon_emoji\",\"text\": \"$escapedText\"}"
#by success returned 'ok'
status="$(curl -s -d -X POST --silent --data-urlencode "payload=$json" "$webhook_url")"
if [ "$status" != "ok" ]; then
# there were problems at curl request
# place to log error
echo "curl error"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment