About

Totietoot hosts a repository of scripts and patterns I have created over the course of time. When I suspect code can be reused in some way I’ll publish it here. – Tim van Kooten Niekerk

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

Small script to send a telegram chat message to a user when new mail arives. 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:#LOGFILE=/path/to/pmlog
.procmailrc:#VERBOSE=yes
.procmailrc:#LOGABSTRACT=all
.procmailrc:INCLUDERC=/etc/procmail-rc/filter-potentialproblem-rc
.procmailrc:INCLUDERC=/etc/procmail-rc/filter-potentialproblem2-rc
.procmailrc:INCLUDERC=/etc/procmail-rc/filter-spamassassin08-rc
.procmailrc:
.procmailrc::0Wc:
.procmailrc:| env python ./.pmscript.py
.procmailrc:
.procmailrc::0
.procmailrc:.mail/

.pmscript.py:import sys
.pmscript.py:import email
.pmscript.py:import httplib
.pmscript.py:import urllib
.pmscript.py:
.pmscript.py:# Contstants...
.pmscript.py:email_addresses = ['test@example.com', 'info@example.com']
.pmscript.py:key = "telegram_botkey"
.pmscript.py:userid = "telegram_userid"
.pmscript.py:headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
.pmscript.py:hostname = "api.telegram.org"
.pmscript.py:uri = "/bot" + key + "/sendMessage"
.pmscript.py:
.pmscript.py:# Read from stdin...
.pmscript.py:full_msg = sys.stdin.read()
.pmscript.py:msg = email.message_from_string(full_msg)
.pmscript.py:
.pmscript.py:# Get info from message...
.pmscript.py:mto = msg['to']
.pmscript.py:msubject = msg['subject']
.pmscript.py:mfrom = msg['from']
.pmscript.py:
.pmscript.py:# Check if e-mail in list...
.pmscript.py:for mailaddr in email_addresses:
.pmscript.py:  if mailaddr in mto:
.pmscript.py:    mrow = "[S6:NewMailAlert] \nFrom: " + mfrom + " \nSubject: " + msubject
.pmscript.py:    params = urllib.urlencode({'chat_id': userid, 'disable_web_page_preview': '1', 'text': mrow})
.pmscript.py:    connectie = httplib.HTTPSConnection(host=hostname, port=443)
.pmscript.py:    connectie.request("POST", uri, params, headers)
.pmscript.py:    resultaat = connectie.getresponse()
.pmscript.py:    if resultaat.status <> 200:
.pmscript.py:      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