Created
May 9, 2024 13:07
-
-
Save s0meguy1/6d3a140129500fafbbe3aa0f4d3e8050 to your computer and use it in GitHub Desktop.
Get slack alerts with arpwatch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
global $config; | |
require_once("notices.inc"); | |
$fd = fopen('php://stdin','r'); | |
$message = stream_get_contents($fd); | |
fclose($fd); | |
if ((false !== $message) && ((false === strpos($message, ': Cron ')) || | |
($config['installedpackages']['arpwatch']['config'][0]['disable_cron'] != 'on'))) { | |
$subject = array(); | |
preg_match('/^Subject: (.*)$/m', $message, $subject); | |
init_config_arr(array('installedpackages', 'arpwatch', 'config', 0, 'row')); | |
foreach ($config['installedpackages']['arpwatch']['config'][0]['row'] as $sup) { | |
if (!empty($sup['mac']) && strpos($message, strtolower($sup['mac'])) && | |
(($sup['notification_type'] == 'all') || ($sup['notification_type'] == $subject[1]))) { | |
return; | |
} | |
} | |
// Check if the message contains "flip flop" | |
if (strpos(strtolower($message), 'flip flop') !== false) { | |
// Skip sending the notification if the message contains "flip flop" | |
return; | |
} | |
// Extract the desired parts of the message using regular expressions | |
preg_match('/^Subject: (.*)$/m', $message, $matches); | |
$subject = $matches[1]; | |
preg_match('/ip address: (.*)$/m', $message, $matches); | |
$ip_address = $matches[1]; | |
preg_match('/ethernet address: (.*)$/m', $message, $matches); | |
$ethernet_address = $matches[1]; | |
preg_match('/ethernet vendor: (.*)$/m', $message, $matches); | |
$ethernet_vendor = $matches[1]; | |
preg_match('/timestamp: (.*)$/m', $message, $matches); | |
$timestamp = $matches[1]; | |
// Reconstruct the message in the desired format | |
$formatted_message = "$subject\n\n"; | |
$formatted_message .= "ip address: $ip_address\n"; | |
$formatted_message .= "ethernet address: $ethernet_address\n"; | |
$formatted_message .= "ethernet vendor: $ethernet_vendor\n"; | |
$formatted_message .= "timestamp: $timestamp"; | |
$send_subject = "{$config['system']['hostname']}.{$config['system']['domain']} - Arpwatch Notification : {$subject}"; | |
$ch = curl_init("https://slack.com/api/chat.postMessage"); | |
$channel = '#CHANNEL'; | |
$data = http_build_query([ | |
"token" => "xoxb-SLACK-TOKEN", | |
"channel" => $channel, | |
"text" => $formatted_message, | |
"username" => "USERNAME", | |
]); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
curl_exec($ch); | |
curl_close($ch); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment