06 December 2008

Python subprocess Module example: using Popen()


#!/usr/bin/python

from subprocess import *

cmd1 = "tail"
cmd1Arg1 = "-n 3"
cmd1Arg2 = "/var/log/syslog"

p1 = Popen([cmd1, cmd1Arg1, cmd1Arg2], stdout=PIPE)
print p1.communicate()[0]

p1 = Popen(["tail", "-n 3", "/var/log/syslog"], stdout=PIPE)
print p1.communicate()[0]




And the output of this thing:



joe@adam:~/Documents$ python popen-example.py

Dec 6 14:38:27 adam hddtemp[6067]: /dev/sda: TOSHIBA MK8034GSX: 38 C
Dec 6 14:43:27 adam hddtemp[6067]: /dev/sda: TOSHIBA MK8034GSX: 38 C
Dec 6 14:48:27 adam hddtemp[6067]: /dev/sda: TOSHIBA MK8034GSX: 38 C

Dec 6 14:38:27 adam hddtemp[6067]: /dev/sda: TOSHIBA MK8034GSX: 38 C
Dec 6 14:43:27 adam hddtemp[6067]: /dev/sda: TOSHIBA MK8034GSX: 38 C
Dec 6 14:48:27 adam hddtemp[6067]: /dev/sda: TOSHIBA MK8034GSX: 38 C

joe@adam:~/Documents$

28 November 2008

python and md5 hashes - very easy

Using python2.5 and md5 hash is indeed very easy


>>> import hashlib
>>> m = hashlib.md5()
>>> m.update("Nobody inspects")
>>> dir(m)
['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', 'block_size', 'copy', 'digest', 'digest_size', 'digestsize', 'hexdigest', 'name', 'update']
>>> m.hexdigest()
'3ef729ccf0cc56079ca546d58083dc12'
>>> len(m.hexdigest())
32


Links:
http://www.python.org/doc/2.5.2/lib/module-hashlib.html

27 November 2008

cisco include command - the poor cousin of the unix grep

How to use the include command in the cisco routers IOS CLI?

A nice way to use it, is like this:


BB#sh int | inclu DLY|is up|is down
FastEthernet0/0 is administratively down, line protocol is down
MTU 1500 bytes, BW 100000 Kbit, DLY 100 usec,
Serial0/0 is up, line protocol is up
MTU 1500 bytes, BW 1544 Kbit, DLY 20000 usec,
FastEthernet0/1 is administratively down, line protocol is down
MTU 1500 bytes, BW 100000 Kbit, DLY 100 usec,
Serial0/1 is up, line protocol is up
MTU 1500 bytes, BW 1544 Kbit, DLY 20000 usec,
Loopback0 is up, line protocol is up
MTU 1514 bytes, BW 8000000 Kbit, DLY 5000 usec,
BB#
BB#sh ip int | inclu is up|is down|Internet address| access list
FastEthernet0/0 is administratively down, line protocol is down
Serial0/0 is up, line protocol is up
Internet address is 10.1.24.1/30
Outgoing access list is not set
Inbound access list is not set
FastEthernet0/1 is administratively down, line protocol is down
Serial0/1 is up, line protocol is up
Internet address is 10.1.34.1/30
Outgoing access list is not set
Inbound access list is not set
Loopback0 is up, line protocol is up
Internet address is 172.30.0.1/24
Outgoing access list is not set
Inbound access list is not set
BB#


13 October 2008

running vmware-server-console on amd64 kubuntu 8.04

For some library-related-reason (lrr) I was not able to start the vmware-server-console on my kubuntu machine. The error messages I was getting were:

/usr/lib/vmware-server-console/bin/vmware-server-console: /usr/lib/vmware-server-console/lib/libgcc_s.so.1/libgcc_s.so.1: version `GCC_3.4' not found (required by /usr/lib32/libcairo.so.2)
/usr/lib/vmware-server-console/bin/vmware-server-console: /usr/lib/vmware-server-console/lib/libgcc_s.so.1/libgcc_s.so.1: version `GCC_4.2.0' not found (required by /usr/lib32/libstdc++.so.6)
/usr/lib/vmware-server-console/bin/vmware-server-console: /usr/lib/vmware-server-console/lib/libgcc_s.so.1/libgcc_s.so.1: version `GCC_3.4' not found (required by /usr/lib32/libcairo.so.2)
/usr/lib/vmware-server-console/bin/vmware-server-console: /usr/lib/vmware-server-console/lib/libgcc_s.so.1/libgcc_s.so.1: version `GCC_4.2.0' not found (required by /usr/lib32/libstdc++.so.6)
/usr/lib/vmware-server-console/bin/vmware-server-console: /usr/lib/vmware-server-console/lib/libgcc_s.so.1/libgcc_s.so.1: version `GCC_3.4' not found (required by /usr/lib32/libcairo.so.2)
/usr/lib/vmware-server-console/bin/vmware-server-console: /usr/lib/vmware-server-console/lib/libgcc_s.so.1/libgcc_s.so.1: version `GCC_4.2.0' not found (required by /usr/lib32/libstdc++.so.6)

vmware-server-console -v
VMware Server Console 1.0.7 build-108231

The solution from whocares.de was reproduced and worked smoothly.

Linux kubuntu 2.6.24-19-generic #1 SMP Wed Aug 20 17:53:40 UTC 2008 x86_64 GNU/Linux

cd /usr/lib/vmware-server-console/lib
cd libgcc_s.so.1/
ll
sudo /bin/bash
mv libgcc_s.so.1 libgcc_s.so.1.org
ll
cd ../libpng12.so.0/
mv libpng12.so.0 libpng12.so.0.org
vmware-server-console &

Thanks!

23 September 2008

Security ROI

This is another series of interesting texts. This time about return-on-investment for IT Security.

http://communities.intel.com/openport/blogs/it/2007/09/04/the-four-dirty-questions-of-measuring-information-security
http://communities.intel.com/openport/blogs/it/2007/09/10/practical-aspects-of-measuring-security
http://chuvakin.blogspot.com/2007/07/security-roi-pile-up.html
http://taosecurity.blogspot.com/2007/07/are-questions-sound.html
http://www.schneier.com/crypto-gram-0809.html

17 September 2008

escaping xml/html with python

escaping xml/html with python is very simple. It is useful to post code excepts on the blog...

>>> from xml.sax.saxutils import escape
>>> s = """<Directory /var/www/py>
... SetHandler mod_python
... PythonHandler mod_python.publisher
... PythonDebug On
... <Files ~ "\.(gif|html|jpg|png)$">
... SetHandler default-handler
... </Files>
... </Directory>
...
... """
>>> print escape(s)
<Directory /var/www/py>
SetHandler mod_python
PythonHandler mod_python.publisher
PythonDebug On
<Files ~ "\.(gif|html|jpg|png)$">
SetHandler default-handler
</Files>
</Directory>

apache2 and mod_python configuration

The useful code snipet that can be included in the .htaccess file or in the apache conf files, so that .py files and other files can be placed in the same directory.

<Directory /var/www/py>
SetHandler mod_python

PythonHandler mod_python.publisher

PythonDebug On
<Files ~ "\.(gif|html|jpg|png)$">

SetHandler default-handler
</Files>

</Directory>


Links
http://webpython.codepoint.net/mod_python_publisher_apache_configuration
http://www.phpbb.com/community/viewtopic.php?f=46&t=1139555
http://httpd.apache.org/docs/2.0/mod/core.html#sethandler

05 September 2008

nice article: linux backup/reinstall

Linux Get list of installed software for reinstallation / restore software
http://www.cyberciti.biz/tips/linux-get-list-installed-software-reinstallation-restore.html

03 September 2008

EU - VIES VAT number validation

You can verify the validity of a VAT number issued by any Member State by selecting that Member State from the drop-down menu provided, and entering the number to be validated.

http://ec.europa.eu/taxation_customs/vies/vieshome.do?selectedLanguage=EN

02 September 2008

my nice .vimrc

syntax on
set number
colorscheme delek

au FileType python source ~/.vim/scripts/python.vim

"highlight RedundantSpaces ctermbg=red guibg=red"
""match RedundantSpaces /\s\+$\| \+\ze\t\|\t/"
" line numbers on/off ctrl+n ctrl+n"
:nmap :set invnumber
" "autoindent on/off"
:nmap :set invautoindent
" " Suche NICHT case-senitiv, außer ein Grossbuchstabe steht im Suchmuster"
" " SmartCase, SMARTcase, smartCASE, SmArTcAsE, sMaRtCaSe, smartcase"
set ignorecase
set smartcase
" " Während Eingabe eines Suchmusters bereits suchen (inkrementell)"
" " Alle zu einem Suchmuster passenden Textstücke markieren (highlight)"
set incsearch
set hlsearch
"
:map :nohl
:imap :nohl i
":map :w!:!ispell -d ngerman -w ÖÄÜöäüß %:e! %

29 August 2008

linux/unix: list files with full path

what to do if you need to list files with their full paths?

ls won't do the job at all. The solution: use the find command.

find reservation -type f -name "*.py"

28 August 2008

SQL select exercise 1

How to select the lowest value for each day that has a value? For instance, for the 2008-04-10, the lowest 'time' value is "09:15:00". What would be the SELECT statement that would produce a list of the lowest values per day, one value for each date?

Date; time
"2008-04-04";"16:00:00"
"2008-04-09";"08:45:00"
"2008-04-09";"12:45:00"
"2008-04-09";"17:45:00"
"2008-04-10";"09:15:00"
"2008-04-10";"12:55:00"
"2008-04-11";"09:15:00"
"2008-04-11";"12:50:00"
"2008-04-14";"10:15:00"
"2008-04-14";"12:45:00"
"2008-04-14";"14:55:00"
"2008-04-16";"08:10:00"
"2008-04-16";"10:10:00"
"2008-04-16";"10:50:00"
"2008-04-16";"14:20:00"
"2008-04-16";"15:50:00"
"2008-04-17";"10:00:00"
"2008-04-17";"13:30:00"
"2008-04-21";"09:45:00"

25 August 2008

It would look strange to have the first paragraph of a section be indented

Did you happen to notice that the first paragraph of each section is
not indented? This paragraph is indented, but it's not the first one
in its section.

\section{It would look strange to have the first paragraph of a
section be indented}

\hspace{\parindent}You probably had never really noticed this
phenomenon before. But if I'm doing my job correctly, you'll never
again accept an indented paragraph at the beginning of a section.

http://www.cs.dartmouth.edu/~thc/CS110/latex-examples/basic2.txt

13 August 2008

useful commands in the unix world part 1

This list will be sporadically updated...

history | awk '{print $2}' | sort | uniq -c | sort -rn | head

file /path/to/file

Some links:

05 August 2008

grep and regular expressions

How to use grep with a resonably simple regexp, but which enables me to use OR? I mean, matching A OR B??? Got it... see the example:


cat /var/log/syslog | grep 'vpnc\|wlan0'


See the grep man page (type man grep at the prompt)


Basic vs Extended Regular Expressions
In basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their special meaning; instead use the backslashed versions \?, \+, \{, \|, \(, and \).

Links:
http://www.amk.ca/python/howto/regex/regex.html
http://analyser.oli.tudelft.nl/regex/index.html.en

14 July 2008

kubuntu on the notebook hp nx6325

Well, after one month running Kubuntu 8.04 on my hp nx6325 i am very happy with the results.

Finding out the hardware:

joe@adam:~$ uname -rs;less /proc/meminfo | grep MemTotal;less /proc/cpuinfo | grep "model name"
Linux 2.6.24-19-generic
MemTotal: 899656 kB
model name : Mobile AMD Sempron(tm) Processor 3500+

joe@adam:~$ sudo lshw | grep 6325
product: HP Compaq nx6325 (RU370ES#ABD)
joe@adam:~$

28 May 2008

Add (append) unique items to a list

>>> a = ['jmendonca', 'joe', 'ann', 'maya']
>>> b = ['jmendonca', 'joe', 'ann', 'maya', 'rod', 'tom', 'tobi']
>>> def addUnique( item ):
... if item not in a:
... a.append(item)
...
>>> def addUnique( item ):
... if item not in a:
... a.append(item)
... print a
...
>>> for item in b:
... addUnique(item)
...
['jmendonca', 'joe', 'ann', 'maya']
['jmendonca', 'joe', 'ann', 'maya']
['jmendonca', 'joe', 'ann', 'maya']
['jmendonca', 'joe', 'ann', 'maya']
['jmendonca', 'joe', 'ann', 'maya', 'rod']
['jmendonca', 'joe', 'ann', 'maya', 'rod', 'tom']
['jmendonca', 'joe', 'ann', 'maya', 'rod', 'tom', 'tobi']

Getting mod_python.publisher work via .htaccess

Being able to use a ".htaccess" file, was however dependent on your directory at least allowing overriding of "FileInfo" aspects of the configuration. If however an Apache administrator will not allow this, you will not have any choice but to have the ability to use mod_python in a directory enabled in the global configuration."
http://www.dscpl.com.au/wiki/ModPython/Articles/GettingModPythonWorking

So, add AT LEAST the following lines to your httpd.conf

AllowOverride FileInfo

And to your .htaccess:

SetHandler mod_python

PythonHandler mod_python.publisher


This was tested in the following environment:
Apache/2.2.3 (testvw) mod_python/3.2.10 Python/2.4.4 PHP/5.2.0-8+etch7.56.200712100724 mod_ssl/2.2.3 OpenSSL/0.9.8c Server at 10.200.20.10 Port 80

Some useful links:
http://httpd.apache.org/docs/2.2/mod/core.html#allowoverride
http://httpd.apache.org/docs/1.3/mod/mod_mime.html#sethandler
http://missysadmin.com/2007/11/21/installing-mod_python/
http://www.dscpl.com.au/wiki/ModPython/Articles/SetHandlerVersusAddHandler
http://www.dscpl.com.au/wiki/ModPython/Articles/GettingModPythonWorking

Some useful commands:
tail -f /var/log/apache2/error.log


27 May 2008

Running multiple Firefox processes concurrently

start the second firefox with the following switches:
firefox -P -no-remote

If profilemanager should be started with the second instance, use instead:
firefox -profilemanager -no-remote

Dictionaries and list comprehension


>>> di = {'joao':['blue', '009']}
>>> di
{'joao': ['blue', '009']}

>>> di = {'jan':['white', '001']}
>>> di
{'jan': ['white', '001']}

>>> di['joao'] = ['blue', '009']
>>> di
{'jan': ['white', '001'], 'joao': ['blue', '009']}

>>> print di
{'jan': ['white', '001'], 'joao': ['blue', '009']}
>>> for item in di:
... print item
...
jan
joao



>>> for i,j in di.iteritems():
... print i,j
...
jan ['white', '001']
joao ['blue', '009']
>>> for i,j in di.iteritems():
... print j
...
['white', '001']
['blue', '009']
>>> for i,j in di.iteritems():
... print j[1]
...
001
009
>>>



######## list comprehension
>>> for person, (color, code) in di.iteritems():
... print "%s likes %s and has code %s" % (person, color, code)
...
jan likes white and has code 001
joao likes blue and has code 009


>>> for person, (color, code) in di.iteritems():
... if color == "blue":
... value = True
...
>>> print value
True
>>> for x, bla in di.iteritems():
... print x
...
jan
joao
>>> for x, bla in di.iteritems():
... print x, bla
...
jan ['white', '001']
joao ['blue', '009']
>>> di
{'jan': ['white', '001'], 'joao': ['blue', '009']}
>>> if "joao" in di:
... print "hi"
...
hi
>>> print "hi5"
File "", line 1
print "hi5"
^
SyntaxError: invalid syntax
>>> if "joao" in di:
... print "hi5"
...
hi5
>>> if "joao" not in di:
... print "hi5"
...
>>> if "joo" not in di:
...
File "", line 2

^
IndentationError: expected an indented block
>>> if "joo" not in di:
... print "hi5"
...
hi5
>>>


adding and removing elements from a dictionary
keys = ['orders','users','textfilter','datefilter']

myData = {}
for key in keys:
myData[key] = None

>>> myData
{'datefilter': None, 'textfilter': None, 'orders': None, 'users': None}

>>> for key in keys:
... myData[key] = [key, key.upper()]
...
>>> myData
{'datefilter': ['datefilter', 'DATEFILTER'], 'textfilter': ['textfilter', 'TEXTFILTER'], 'orders': ['orders', 'ORDERS'], 'users': ['users', 'USERS']}

Using dates on your filenames

A nice way to use the output of the date command to name your filenames:

cp test.sh tesh.sh.$(date +%Y%m%d-%H%M%S)

now it should be ok!