This blog is created to keep things which I always need to remember in Python and Linux scripting. ... and few other things.
Thursday, June 15, 2017
TCP echo server using ncat
ncat -v -l -c 'while true; do read i && echo [echo] $i; done' 10.50.2.104 3000
UDP Client and Echo Server using netcat. for IPv6.
Client
nc -zuv6 2001:470:c2d7:917::38 3000
Server
nc -6lu 2001:470:c2d7:917::38 3000
Monday, March 13, 2017
Make Apache2 server HTTPS secure
1. Create key and cerificate files.
cd /etc/apache2/
mkdir -p ssl/crt
mkdir -p ssl/key
cd ssl
openssl req -newkey rsa:2048 -nodes -keyout key/vhost2.key -x509 -days 365 -out crt/vhost2.crt
cd /etc/apache2/
mkdir -p ssl/crt
mkdir -p ssl/key
cd ssl
openssl req -newkey rsa:2048 -nodes -keyout key/vhost2.key -x509 -days 365 -out crt/vhost2.crt
2. Configure Virtual host in the file /etc/apache2/sites-available/000-default.conf
...
<VirtualHost *.443>
<VirtualHost *.443>
DocumentRoot /var/www/secure
ServerName vhost1.yourdomain.com
# DirectoryIndex index.php
ErrorLog /var/log/apache2/vhost1-error.log
CustomLog /var/log/apache2/vhost1-access.log combined
SSLEngine On
SSLCertificateFile /etc/apache2/ssl/crt/vhost2.crt
SSLCertificateKeyFile /etc/apache2/ssl/key/vhost2.key
<Location />
SSLRequireSSL On
SSLVerifyClient optional
SSLVerifyDepth 1
SSLOptions +StdEnvVars +StrictRequire
</Location>
<Directory "/var/www/vhost1">
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
AddHandler cgi-script .py
AddHandler default-handler .html .htm
Allow from all
</Directory>
</VirtualHost>
</VirtualHost>
3. Install modes
sudo a2enmod rewrite
sudo a2enmod ssl
3. Restart Apache2
sudo service apache2 restart
Thursday, March 2, 2017
Installing new partition for swap space
1. Add new disk in VMWare. In the example it is 3rd disk.
a. Power off machine
b. Edit Settings
c. Add ... Hard Disk , Next
d. Create a new virtual disk, Next
e. Capacity 2GB , Thick provision Lazy Zeroed, Next , Next
f. Finish
2. Create MS-DOS partition table.
> fsdisk /dev/sdc
> n (new)
> p (primary)
> 1 (first)
>w (write)
3. Install and used GUI program gparted
> sudo apt-get install gparted
4. Start gparted from Ubuntu GUI.
a. Select device GParted / Devices / dev/sdc1
b. Device / Create Partition Table , , Apply
c. Create partition: Patrition / New /
d. Create as: Primary Patrition, File system: linux-swap, Label: swap / Add
e. Edit / Apply All Operations
f. Partition / Swapon
g. GParted / Quit
5. Make your changes permanent by adding entry to /etc/fstab file:
mpug@testtool13:/$ vi /etc/fstab
# /etc/fstab: static file system information.
## Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices# that works even if disks are added and removed. See fstab(5).##
/dev/mapper/testtool97--vg-root / ext4 errors=remount-ro 0 1
# /boot was on
/dev/sda1 during installationUUID=b36b850c-0a22-4f02-9516-83b80d5a6c1f
/boot ext2 defaults 0 2
/dev/mapper/testtool97--vg-swap_1 none swap sw 0 0
/dev/sdc1 none swap sw 0 0
/dev/fd0 /media/floppy0 auto rw,user,noauto,exec,utf8 0 0
/dev/testtool97-disk/disk /disk ext4 errors=remount-ro 0 1
a. Power off machine
b. Edit Settings
c. Add ... Hard Disk , Next
d. Create a new virtual disk, Next
e. Capacity 2GB , Thick provision Lazy Zeroed, Next , Next
f. Finish
2. Create MS-DOS partition table.
> fsdisk /dev/sdc
> n (new)
> p (primary)
> 1 (first)
>
> sudo apt-get install gparted
d. Create as: Primary Patrition, File system: linux-swap, Label: swap / Add
# /etc/fstab: static file system information.
## Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices# that works even if disks are added and removed. See fstab(5).##
/dev/mapper/testtool97--vg-root / ext4 errors=remount-ro 0 1
# /boot was on
/dev/sda1 during installationUUID=b36b850c-0a22-4f02-9516-83b80d5a6c1f
/boot ext2 defaults 0 2
/dev/mapper/testtool97--vg-swap_1 none swap sw 0 0
/dev/sdc1 none swap sw 0 0
/dev/fd0 /media/floppy0 auto rw,user,noauto,exec,utf8 0 0
/dev/testtool97-disk/disk /disk ext4 errors=remount-ro 0 1
Friday, February 24, 2017
Installation of package ExifRead
ExifRead is the package to read pictures attribute from files.
1. Download the file ExifRead-2.1.2.tar.gz
2. >gunzip ExifRead-2.1.2.tar.gz
3. >tar -xvf ExifRead-2.1.2.tar
4. >cd ExifRead-2.1.2
5. >sudo python setup.py install
1. Download the file ExifRead-2.1.2.tar.gz
2. >gunzip ExifRead-2.1.2.tar.gz
3. >tar -xvf ExifRead-2.1.2.tar
4. >cd ExifRead-2.1.2
5. >sudo python setup.py install
Thursday, February 23, 2017
Using MySQLdb with Python
To be able to connect python scripting with MySqlDB install packages.
sudo apt-get install python-mysqldb
sudo apt-get install python-htmlgen
sudo apt-get install phpmyadmin
use in python script
import MySQLdb as mdb
import base64
SCRIPT = sys.argv[0].split("/")[-1:][0]
MYSQL_DATABASE_NAME='links'
MYSQL_USER='root'
MYSQL_PASSWORD=base64.b64decode('cm9vdDEyMw==')
MYSQL_PASSWORD='root123'
SQL_LIMIT = 500
database_name=MYSQL_DATABASE_NAME
con = mdb.connect('localhost',MYSQL_USER,MYSQL_PASSWORD,MYSQL_DATABASE_NAME)
cur = con.cursor()
sudo apt-get install python-mysqldb
sudo apt-get install python-htmlgen
sudo apt-get install phpmyadmin
use in python script
import MySQLdb as mdb
import base64
SCRIPT = sys.argv[0].split("/")[-1:][0]
MYSQL_DATABASE_NAME='links'
MYSQL_USER='root'
MYSQL_PASSWORD=base64.b64decode('cm9vdDEyMw==')
MYSQL_PASSWORD='root123'
SQL_LIMIT = 500
database_name=MYSQL_DATABASE_NAME
con = mdb.connect('localhost',MYSQL_USER,MYSQL_PASSWORD,MYSQL_DATABASE_NAME)
cur = con.cursor()
Run Python as CGI in Apache2
1. Install apache2
sudo apt-get install apache2
2. Install python
sudo apt-get install python
3. Enable CGI processing in apache2
sudo a2enmod cgi
4. Modify file /etc/apache2/sites-available/000-default.conf to include:
5. Restart apache2
sudo service apache2 restart
https://www.linux.com/blog/configuring-apache2-run-python-scripts
sudo apt-get install apache2
2. Install python
sudo apt-get install python
3. Enable CGI processing in apache2
sudo a2enmod cgi
4. Modify file /etc/apache2/sites-available/000-default.conf to include:
#ServerName www.example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /var/www/cgi-bin/
<Directory "/var/www/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
AddHandler cgi-script .py
AddHandler default-handler .html .htm
Allow from all
</Directory>
<Directory "/var/www/run">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
AddHandler cgi-script .py
AddHandler default-handler .html .htm
Allow from all
</Directory>
<Directory "/var/www/tracedecode">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
AddHandler cgi-script .py
AddHandler default-handler .html .htm
Allow from all
</Directory>
# AddHandler mod_python .py
5. Restart apache2
sudo service apache2 restart
https://www.linux.com/blog/configuring-apache2-run-python-scripts
Allow remote SSHas root login
Become root
sudo su
Create password as root
passwd
Modify file /etc/ssh/sshd_config
# Authentication:
LoginGraceTime 120
# PermitRootLogin prohibit-password
PermitRootLogin yes
StrictModes yes
Restart SSH service
sudo su
Create password as root
passwd
Modify file /etc/ssh/sshd_config
# Authentication:
LoginGraceTime 120
# PermitRootLogin prohibit-password
PermitRootLogin yes
StrictModes yes
Restart SSH service
sudo service sshd restart
Wednesday, February 22, 2017
Friday, January 27, 2017
Adding new disk to Ubuntu 14.04 system.
1. Add new hardware disk to Ubuntu 14.04 system using LVM ( Logical Volume Manager)
- fdisk /dev/sdb
- select new partition "n"
- select primary partition "p"
- select one partition "1"
- "cr"
- "cr"
- write changes to disk "w"
- Make sure LVM is installed on your computer.
sudo apt-get install lvm2 - Create physical volume "/dev/sdb1"
pvcreate /dev/sdb1 - Create volume group named "testtool97-disk" and include physical volume /dev/sdb1
vgcreate testtool97-disk /dev/sdb1 - Create logical volume named "disk" of size 5.9G and add it to volume group testtool97-disk
lvcreate -n disk -L 5.9 testtool97-disk
3. Make file system
- mkfs.ext4 /dev/testtool97-disk/disk
- mkdir /disk
- vi /etc/fstab
- add following line to mount file system on "disk" mount point
- /dev/testtool97-disk/disk /disk ext4 errors=remount-ro 0 1
- save and quit
- mount /disk
Friday, December 9, 2016
Rename all JPG MP4 files in the current directory to date_time file name format.
Rename all files in the currect directory to date_time file name format
sudo exiftool '-filename<CreateDate' -d %y-%m-%d_%H-%M-%S.%%ue -r -ext JPG .
Rename files with MOV extention to MP4 extention.
Rename all files with .MOV extention to .MP4 extention.
sudo rename -v 's/\.MOV$/\.MP4/' *.MOV
sudo rename -v 's/\.MOV$/\.MP4/' *.MOV
Extracting images from video file
Get image from input.flv file at 14.435 sec and save to out.png file
avconv -i input.flv -ss 00:00:14.435 -f image2 -vframes 1 out.png
avconv -i input.flv -ss 00:00:14.435 -f image2 -vframes 1 out.png
Adding text annotations to JPG image.
Adding annotation "Copyright" at lower right corner of image at font size 20, filled with white color and black shadow, saved to file IMG_ANNOTATED.JPG
sudo convert IMG.JPG -font Arial -pointsize 20 -draw "gravity SouthEast fill black text 0,12 'Copyright' fill white text 1,11 'Copyright'" IMG_ANNOTATED.JPG
sudo convert IMG.JPG -font Arial -pointsize 20 -draw "gravity SouthEast fill black text 0,12 'Copyright' fill white text 1,11 'Copyright'" IMG_ANNOTATED.JPG
Adding GIF image to JPG image with transparent area.
1. Install ImageMagick:
sudo apt-get install imagemagick
2. Using convert command from ImageMagick make white part of Arrow.gif transparent and save in file arrow_transparent.gif :
sudo convert Arrow.gif -fuzz 10% -transparent white arrow_transparent.gif
3. Merge arrow transparent into IMG.JPG at affset 300,300 and save file in RESULT.JPG file:
sudo convert IMG.JPG -page +300+300 arrow_transparent.gif -flatten RESULT.JPG
sudo apt-get install imagemagick
sudo convert Arrow.gif -fuzz 10% -transparent white arrow_transparent.gif
3. Merge arrow transparent into IMG.JPG at affset 300,300 and save file in RESULT.JPG file:
sudo convert IMG.JPG -page +300+300 arrow_transparent.gif -flatten RESULT.JPG
Monday, May 9, 2016
How to remove all files which are older than 5 days
How to list all files older than 5 days ?
> find /path/to/files* -mtime +5
How to delete all files older than 5 days ?
> find /path/to/files* -mtime +5 -exec rm {} \;
> find /path/to/files* -mtime +5
How to delete all files older than 5 days ?
> find /path/to/files* -mtime +5 -exec rm {} \;
Tuesday, April 22, 2014
When Ubuntu does not start interfaces from eth0
When Ubuntu after starting does not have eth0, but instead created eth2 etc.
Go to edit file etc/udev/rules.d/70-persistent-net.rules and assign proper eth names to mac adresses.
# PCI device 0x8086:0x100f (e1000)
Go to edit file etc/udev/rules.d/70-persistent-net.rules and assign proper eth names to mac adresses.
# PCI device 0x8086:0x100f (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:aa:80:d7", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"
# This file was automatically generated by the /lib/udev/write_net_rules
# program, run by the persistent-net-generator.rules rules file.
#
# You can modify it, as long as you keep each rule on a single
# line, and change only the value of the NAME= key.
# PCI device 0x8086:0x100f (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:aa:80:e1", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"
Thursday, February 13, 2014
Remote python debugging using Eclipse
1. Untar file pysrc.tar into directory p2t
2. Start Debugger Server in Eclipse
3. Insert following code into python source code and execute.
2. Start Debugger Server in Eclipse
import sys
sys.path.append(r'../pysrc')
import pydevd
pydevd.settrace('10.51.2.91', port=5678) # IP of your Eclipse machine
or
pydevd.settrace('10.51.2.91', port=5678, stdoutToServer=True, stderrorToServer=True)
where IP is adress of machine running Eclipse program
Wednesday, January 15, 2014
Ubuntu : Bonding interfaces eth1 eth2 eth3 into one logical bond0
Bonding interfaces
# Bonding
up /sbin/modprobe -o bonding_ext1 bonding mode=4 miimon=500 updelay=0 xmit_hash_policy=layer3+4
up /sbin/ifenslave bond0 eth1 eth2 eth3
Checking if/which interfaces were bonded
cat /proc/net/bonding/bond0
In the file /etc/network/interfaces
# Bonding
up /sbin/modprobe -o bonding_ext1 bonding mode=4 miimon=500 updelay=0 xmit_hash_policy=layer3+4
up /sbin/ifenslave bond0 eth1 eth2 eth3
Checking if/which interfaces were bonded
cat /proc/net/bonding/bond0
Tuesday, September 13, 2011
How to install python as Web executable in Ubuntu 8.04
1. Install Apache2
> sudo apt-get install apache2
2. Install python apache module
> sudo apt-get install libapache2-mod-python
3. Modify Apache2 configuration in the file /etc/apache2/sites-available/default
adding
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
AddHandler mod_python .py
PythonHandler mod_python.publisher
PythonDebug On
4. Restart apache
> sudo /etc/init.d/apache2 restart
5. Create file hello.py in directory /var/www
def index():
c= "Hello World"
return c;
6. Start Web browser at URL http://localhost/hello.py
> sudo apt-get install apache2
2. Install python apache module
> sudo apt-get install libapache2-mod-python
3. Modify Apache2 configuration in the file /etc/apache2/sites-available/default
adding
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
AddHandler mod_python .py
PythonHandler mod_python.publisher
PythonDebug On
4. Restart apache
> sudo /etc/init.d/apache2 restart
5. Create file hello.py in directory /var/www
def index():
c= "Hello World"
return c;
6. Start Web browser at URL http://localhost/hello.py
Sunday, October 24, 2010
How to create animation with python and PyODE package.
Falling Boxes in HD. PyODE animation. from Krzysztof J Pakula on Vimeo.
Instruction for Linux Ubuntu users.
1. You already should have version of Python installed by default on your machine.
2. Install packages PyODE and cgkit
> sudo apt-get install python-pyode
> sudo apt-get install python-cgkit
3. Download python script pyode_ex_hd.py .
4. By executing this python script you will create pyode_ex_hd.rib file
> python pyode_ex_hd.py
5. Install Pixie renderman program. ( If you have problem finding renderman install from this deb file)
6. Download and compile surface shader file occsurf2.sl
> sdrc occsurf2.sl
7. Render the rib file
> rndr pyode_ex_hd.rib
Rendering the rib file will create series of picture files in pyode_ex_hd.001.tif , ..., pyode_ex_hd.300.tif
It will take few hours depending on the speed of your computer.
8. Install ImageMagick on your Ubuntu system
> sudo apt-get install imagemagick
9. Using ImageMagick Convert all tif files to png format.
> convert pyode_ex_hd.001.tif pyode_ex_hd.001.png
........
> convert pyode_ex_hd.300.tif pyode_ex_hd.300.png
It will take few hours depending on the speed of your computer.
8. Install ImageMagick on your Ubuntu system
> sudo apt-get install imagemagick
9. Using ImageMagick Convert all tif files to png format.
> convert pyode_ex_hd.001.tif pyode_ex_hd.001.png
........
> convert pyode_ex_hd.300.tif pyode_ex_hd.300.png
10. Using VirtualDubMod program create single uncompressed avi file from tif files.
This step shall be done on Windows machine.
You must use vcf configuration file pyode_ex_hd.vcf
>VirtualDubMod /spyode_ex_hd.vcd /x
11. Compress avi file to mp4 format using H.264 encoder.
Thursday, May 13, 2010
Testing SOAPVASP interafce Python performance using "timeit"
1. Start SOAPVASP interface on port 7000
$ python interfaceSOAPVASP.py -p 7000
2. Run on the same machine
$ python -m timeit "import http_sender; http_sender.SimpleHTTPSender.send(host
='127.0.0.1', port ='7000', method='GET')"
100 loops, best of 3: 11.5 msec per loop
$ python interfaceSOAPVASP.py -p 7000
2. Run on the same machine
$ python -m timeit "import http_sender; http_sender.SimpleHTTPSender.send(host
='127.0.0.1', port ='7000', method='GET')"
100 loops, best of 3: 11.5 msec per loop
Wednesday, February 24, 2010
Password free ssh logging to remote computer
1. Create rsa id
> cd
> ssh-keygen -t rsa
2. Copy rsa id to remote computer as user me
> ssh-copy-id -i ~/.ssh/id_rsa.pub me@10.19.169.5
3. Test by
> ssh me@10.19.169.5
The computer at 10.19.169.5 shall NOT ask you for password
> cd
> ssh-keygen -t rsa
2. Copy rsa id to remote computer as user me
> ssh-copy-id -i ~/.ssh/id_rsa.pub me@10.19.169.5
3. Test by
> ssh me@10.19.169.5
The computer at 10.19.169.5 shall NOT ask you for password
Shell script to execute command on the list of computers
The list of computers is given as a text list of IP adresses named "all.list". Each IP in saparate line
To execute commands on each computer create file all.sh with following text
for i in `cat all.list | awk '{ print $i}'` ; do echo $i; ssh $i $1 ; done
Execute commands by
> source ./all.sh "ls -la"
To execute commands on each computer create file all.sh with following text
for i in `cat all.list | awk '{ print $i}'` ; do echo $i; ssh $i $1 ; done
Execute commands by
> source ./all.sh "ls -la"
To execute command as root on remote computer with ip 10.19.148.65 using ssh
To execute command as root on remote computer with ip 10.19.168.65
> ssh root@10.19.148.65 "ls -la"
To execute text replacement on remote computer using perl script
> ssh root@10.19.148.65 "perl -p -e 's/oldtext/newtext/g' oldfile >newfile"
> ssh root@10.19.148.65 "ls -la"
To execute text replacement on remote computer using perl script
> ssh root@10.19.148.65 "perl -p -e 's/oldtext/newtext/g' oldfile >newfile"
How to replace text in many files using single perl command
To replace text "localhost" to "localnet" in all files with extension text execute following
> perl -p -e "s/localhost/localnet/g" *.txt
To add new line after new text "localnet" use new line code \x0a
> perl -p -e "s/localhost/localnet\x0anew line/g" *.txt
To insert TAB in replaced text use code \x09
> perl -p -e "s/localhost/localnet\x0anew line\x09rest of line after tab/g" *.txt
> perl -p -e "s/localhost/localnet/g" *.txt
To add new line after new text "localnet" use new line code \x0a
> perl -p -e "s/localhost/localnet\x0anew line/g" *.txt
To insert TAB in replaced text use code \x09
> perl -p -e "s/localhost/localnet\x0anew line\x09rest of line after tab/g" *.txt
Subscribe to:
Posts (Atom)
Followers
About Me
- Krzysztof Pakuła
- Znak zodiaku - Lew
