Monday, September 22, 2008

sed: substitute text examples

En example

Substitute text1 with text2 in file /etc/apt/sources.list:
cat /etc/apt/sources.list | sed 's/text1/text2/g' > out.txt
or with different 'slash':cat /etc/apt/sources.list | sed 's|text1|text2|g' > out.txt
Multiple substitution: cat /etc/apt/sources.list | sed -e 's/text1/text2/g' -e 's/text3/text4/g' > out.txt

Delete lines containing 'blabla' string:cat some.txt | sed -e '/blabla/d' > out.txt

Sunday, August 24, 2008

VirtualBox: backup VDI using clonevdi tool

To list virtual drives:
VBoxManage list hdds
You must get UUID of virtual drive you want to clone. As an egzamples lets assume that UUID is 973b3243-8168-41c3-bb29-1c9865eaec7c. Having this, one executes clonevdi command as follows:
VBoxManage clonevdi 973b3243-8168-41c3-bb29-1c9865eaec7c outfilename.vdi I noticed that when coping VDI to new Vbox, networking does not work good.
I managed to repair this by restarting it (my guest is Linux):
/etc/init.d/networking restart
Also sometimes this failed, because the command was restarting wrong network interface, e.g. eth4 instead of eth2. This can be change in /etc/network/interfaces

Tuesday, August 12, 2008

ubuntu-server: How to enabling public_html folder

To enable public_html folder in users home directory use the following:sudo a2enmod userdir

Saturday, June 21, 2008

Python: Isotropic fractal surface generator

#! /usr/bin/env python
from __future__ import division
import Image
from scipy import *

class FractalSurface(object):
'''Generate isotropic fractal surface image using
spectral synthesis method [1, p.]
References:
1. Yuval Fisher, Michael McGuire,
The Science of Fractal Images, 1988
'''

def __init__(self,fd=2.5, N=256):
self.N=N
self.H=1-(fd-2);
self.X=zeros((self.N,self.N),complex)
self.A=zeros((self.N,self.N),complex)
self.img=Image.Image()

def genSurface(self):
'''Spectral synthesis method
'''
N=self.N; A=self.A
powerr=-(self.H+1.0)/2.0

for i in range(int(N/2)+1):
for j in range(int(N/2)+1):

phase=2*pi*rand()

if i is not 0 or j is not 0:
rad=(i*i+j*j)**powerr*random.normal()
else:
rad=0.0

self.A[i,j]=complex(rad*cos(phase),rad*sin(phase))

if i is 0:
i0=0.0
else:
i0=N-i
if j is 0:
j0=0.0
else:
j0=N-j

self.A[i0,j0]=complex(rad*cos(phase),-rad*sin(phase))


self.A.imag[N/2][0]=0.0
self.A.imag[0,N/2]=0.0
self.A.imag[N/2][N/2]=0.0

for i in range(1,int(N/2)):
for j in range(1,int(N/2)):
phase=2*pi*rand()
rad=(i*i+j*j)**powerr*random.normal()
self.A[i,N-j]=complex(rad*cos(phase),rad*sin(phase))
self.A[N-i,j]=complex(rad*cos(phase),-rad*sin(phase))

itemp=fftpack.ifft2(self.A)
itemp=itemp-itemp.min()
self.X=itemp


def genImage(self):
#Aa=abs(Aa)
Aa=self.X
im=Aa.real/Aa.real.max()*255.0
self.img=Image.fromarray(uint8(im))
#img2=Image.fromstring("L",(N,N),uint8(im).tostring())

def showImg(self):
self.img.show()

def saveImg(self,fname="fs.tiff"):
self.img.save(fname)

def getFSimg(self):
return self.img

def main():
fs=FractalSurface()
fs.genSurface()
fs.genImage()
fs.saveImg()
fs.showImg()

if __name__ == '__main__':
main()

Example


FD=2.1




FD=2.5




FD=2.9

Tuesday, June 03, 2008

ubuntu: iptables port redirect

I want to redirect all incoming requests on port 80 to 8080. I did it using the following command:sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
Following this operation my iptables -L wasChain INPUT (policy ACCEPT)
target prot opt source destination

Chain FORWARD (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

The iptables -t nat -L was:Chain PREROUTING (policy ACCEPT)
target prot opt source destination
REDIRECT tcp -- anywhere anywhere tcp dpt:www redir ports 8080

Chain POSTROUTING (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

I saved these setings using sudo iptables-save.

Note:
This works only when some other computer tries to connect to port 80. If I tried to connect from the same server (i.e. localhost) it did not work. The reason for now is unknown, but it works, an this is good.

Thursday, April 10, 2008

bash: simple loop through files

for f in *.tiff ; do echo $f; done
Example with ImageMagick's convert program: for f in *.tiff; do convert -crop 384x384+48+35 $f out/$f; done

Wednesday, March 26, 2008

Two buttons and scroll in mouse in mac os 8.6

Please install this soft:

GameSprockets_1.7.5.smi.bin
usb-overdrive-14.hqx
Mac_OS_ROM_Update_1.0.smi.bin

Of course StuffIt Expander is requred to run this files.

On my Mac OS 8.6 installing these files allowed for using two button Microsoft USB mouse with with two buttons and a scroll! The work is much more comfortable now.

Sunday, March 09, 2008

Python: script for sending email

Function for sending emails using Python script. It requires local SMTP server (e.g. Postfix) or one can provide address to the external SMTP server.

def mail(From,To,subject,msg):
import smtplib

smtpserver = 'localhost' # SMTP server
AUTHREQUIRED = 0 # if you need to use SMTP AUTH set to 1
smtpuser = '' # for SMTP AUTH, set SMTP username here
smtppass = '' # for SMTP AUTH, set SMTP password here

RECIPIENTS=To
SENDER=From

mssg = """"\From: %s
Subject: %s
%s
""" %(SENDER,subject,msg)

session = smtplib.SMTP(smtpserver)
if AUTHREQUIRED:
session.login(smtpuser, smtppass)
smtpresult = session.sendmail(SENDER, RECIPIENTS, mssg)
session.quit()


if __name__ == '__main__':
subject="This is some email subject"
mssg='This is content of the mail.'
mail("return@address.com","destination@address.com",
subject,mssg)
Note: this code is based on the this article.

Wednesday, February 27, 2008

Zope: Zope page template to display image object

Lets add an image object named test.png. Now, how to display it?
Simply, create new Page Tempalte. Replace to default contents by:


This will be replace by html tag: