About

Totietoot hosts a repository of scripts and patterns I have created over the course of time.

GNU/Linux Telegram/Bot Mail Alert Script (Procmail/Python)

Small script to send a telegram chat message to a user when new mail arrives. Script uses procmail and a small python script to process the message and to send the alert alert message through a telegram bot api.

.procmailrc file:

#LOGFILE=/path/to/pmlog
#VERBOSE=yes
#LOGABSTRACT=all
INCLUDERC=/etc/procmail-rc/filter-potentialproblem-rc
INCLUDERC=/etc/procmail-rc/filter-potentialproblem2-rc
INCLUDERC=/etc/procmail-rc/filter-spamassassin08-rc

:0Wc:
| env python ./.pmscript.py

:0
.mail/

.pmscript.py file:

import sys
import email
import httplib
import urllib

# Contstants...
email_addresses = ['test@example.com', 'info@example.com']
key = "telegram_botkey"
userid = "telegram_userid"
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
hostname = "api.telegram.org"
uri = "/bot" + key + "/sendMessage"

# Read from stdin...
full_msg = sys.stdin.read()
msg = email.message_from_string(full_msg)

# Get info from message...
mto = msg['to']
msubject = msg['subject']
mfrom = msg['from']

# Check if e-mail in list...
for mailaddr in email_addresses:
  if mailaddr in mto:
    mrow = "[S6:NewMailAlert] \nFrom: " + mfrom + " \nSubject: " + msubject
    params = urllib.urlencode({'chat_id': userid, 'disable_web_page_preview': '1', 'text': mrow})
    connectie = httplib.HTTPSConnection(host=hostname, port=443)
    connectie.request("POST", uri, params, headers)
    resultaat = connectie.getresponse()
    if resultaat.status <> 200:
      print resultaat.status, resultaat.reason

PowerShell Validate XML Message

Quick (raw) guide to validate a XML message using Powershell.

# Declare schemas opbject…
$schemas = New-Object System.Xml.Schema.XmlSchemaSet

# Read schema from file…
$schemaItem = Get-Item File.xsd
$fileStream = $schemaItem.OpenRead()
$xsd = [Xml.Schema.XmlSchema]::Read($fileStream, $null)
$fileStream.Close()

# Add XSD to schemas &amp; compile…
$schemas.Add($xsd)
$schemas.Compile()

# Read &amp; validate XML…
[xml]$xml = Get-Content .\File.xml
$xml.Schemas = $schemas
$xml.Validate($null)

PowerShell Cleanup Script

Cleanup logfiles when file change date is older than 30 days…

<#
    Description    : Cleanup logfiles when file change date is older than 30 days...
    Keywords       : Files, Change, Cleanup
    Majorversion   : 1
    Author         : Tim van Kooten Niekerk
    Date           : 2011-11-10
#>

<#      CHANGELOG:
        2013-12-12 => Add Headers (Tim van Kooten Niekerk)
#>

# Global config settings...
[string]$sDirName = "D:\LogFiles\W3SVC705438624"
[string]$sFileNameMask = "*.log"
[string]$sGCIMask = $sDirName + "\" + $sFileNameMask
[string]$sEventLogMessage = "### Logfile cleanup script started ###`r`n"

# Start Main script...
if (Test-Path $sDirName) {
get-childitem $sGCIMask | 
	% { 
		if ($_.LastWriteTime -lt (Get-date).AddDays(-30)) {
		remove-item $_.fullname
		$sEventLogMessage = $sEventLogMessage + "File " + $_.fullname + " (" + $dFileModTime.DateTime + ") removed...`r`n"
		}
	}
}

# Cleanup...
[string]$sEventLogMessage = $sEventLogMessage + "### Logfile cleanup script finished ###"
write-eventLog -LogName "Windows PowerShell" -Source "PowerShell" -EventID 30001 -Message $sEventLogMessage -EntryType Information