Monthly Archives: May 2016

Making myself a backdoor through the corporate firewall. Again.

Recently I’ve been fighting with corporate internet access policy again.
After recent updates Firefox and Palemoon (my browser of choice) stopped working with ForceBindIP utility. Period.
So I’ve starged using VMWare CentOS virtual machine with virtual network adapter bound to my WiFi physical adapter connected to cell phone hotspot. Not so convenient but at least it works.
ForceBindIP is still usefull for connecting to blocked servers with some utilities say WinSCP.

Python: implications while with applying regexp over UTF-16 file and the example of how to solve them.

This simple Python program helps to quickly extract distinct Essbase errors from given log file. It demonstrates the usage of ArgumentParser, the usage of dictionaries and sets, reading of UTF-16 file (with BOM) and applying unicode regex on it’s contents. Plus it parses itself to find all Essbase error codes. The actual file is pretty large, because it contains all error codes in the final section, but it was shortened for this blog.
For me the hard part was to actually read UTF-16 file and not to forget adding `re.UNICODE` to `re.compile` call.

# -*- coding: utf-8 -*-
# distinct_ess_errors.py

import sys
import os
import io
import re
import codecs
from argparse import ArgumentParser

usage = "usage: %prog "
parser = ArgumentParser()

arg_group = parser.add_mutually_exclusive_group()
parser.add_argument( "log_file"
	, help = "Error or log file" )
parser.add_argument( "pattern"
	, help = "Pattern" )

( args ) = parser.parse_args()

if not args.log_file  or not args.pattern:
	print "Not all parameters set"
	parser.print_help()
	sys.exit( 0 )

messageFinder = re.compile( r"%%(\d+?)\s(.*)$" )
fmsg = open( 'distinct_ess_errors.py' )
msg = {}
msgCount = 0
for line in fmsg:
	match = messageFinder.search( line )
	if None <> match:
		msg[ match.group(1) ] = match.group(2)
		msgCount = msgCount + 1
fmsg.close()
print "Parsed " + str( msgCount ) + " messages"

finder = re.compile( u"" + args.pattern, re.UNICODE )
distinct = set()

lineCount = 0
if args.log_file and os.path.isfile( args.log_file ):
	fin = io.open(args.log_file, encoding='utf-16')
	for line in fin:
		lineCount = lineCount + 1
		match = finder.search( line )
		if ( None <> match ):
			if not( match.group(1) in distinct ):
				distinct.add( match.group(1) )
	fin.close()

print "Searched " + str( lineCount ) + ' lines for "' + args.pattern + '"'
	
print "\nFound\n"
	
for el in distinct:
	print el + " " + msg[el]
	
sys.exit( 0 )

"""
#Created: Jan 13 2015 23:14:17
%%1001000 Unable to Open Report File [%s] on Server
%%1001001 Unknown Command [%s] in Report
%%1001002 Incorrect Syntax for Range Format in Report
...
"""