This blog is created to keep things which I always need to remember in Python and Linux scripting. ... and few other things.
Friday, November 17, 2017
Wednesday, September 6, 2017
Secure HTML server as python script
from twisted.internet import protocol, reactor
from twisted.web import static, server, twcgi
def create_SSL_context(privkey_file, cacert_file):
if privkey_file is None or cacert_file is None:
raise AttributteError('To start SSL server privkey_file and cacert_file need to be specified')
from twisted.internet import ssl as twistedssl
ssl_context = twistedssl.DefaultOpenSSLContextFactory(privkey_file, cacert_file,)
return ssl_context
port = 4444
secure = True
root = static.File("/var/www")
root.putChild("pswd", twcgi.CGIDirectory("/var/www/pswd"))
root.putChild("filter", twcgi.CGIDirectory("/var/www/filter"))
root.putChild("abc", twcgi.CGIDirectory("/var/www/abc"))
if secure:
privkey_file='privkey_pp_vm.pem'
cacert_file='cacert_pp_vm.pem'
ssl_context = create_SSL_context(privkey_file, cacert_file)
reactor.listenSSL(port, server.Site(root), ssl_context)
print "Secure Server started on port: %s"%port
else:
reactor.listenSSL(port, server.Site(root))
print "Non-secure Server started on porti: %s"%port
reactor.run()
from twisted.web import static, server, twcgi
def create_SSL_context(privkey_file, cacert_file):
if privkey_file is None or cacert_file is None:
raise AttributteError('To start SSL server privkey_file and cacert_file need to be specified')
from twisted.internet import ssl as twistedssl
ssl_context = twistedssl.DefaultOpenSSLContextFactory(privkey_file, cacert_file,)
return ssl_context
port = 4444
secure = True
root = static.File("/var/www")
root.putChild("pswd", twcgi.CGIDirectory("/var/www/pswd"))
root.putChild("filter", twcgi.CGIDirectory("/var/www/filter"))
root.putChild("abc", twcgi.CGIDirectory("/var/www/abc"))
if secure:
privkey_file='privkey_pp_vm.pem'
cacert_file='cacert_pp_vm.pem'
ssl_context = create_SSL_context(privkey_file, cacert_file)
reactor.listenSSL(port, server.Site(root), ssl_context)
print "Secure Server started on port: %s"%port
else:
reactor.listenSSL(port, server.Site(root))
print "Non-secure Server started on porti: %s"%port
reactor.run()
Tuesday, August 15, 2017
Friday, July 7, 2017
Testing enum interface using dig command
MDN number 44 186 533 2244
SERVER IP 2001:470:c2d7:917::54
SERVER PORT 2254
dig -t naptr 4.4.2.2.3.3.5.6.8.1.4.4.e164.arpa @2001:470:c2d7:917::54 -p 2254
How to block IPv6 address to be used as originator address when interface has 2 addreses.
sudo ip -f inet6 addrlabel add prefix label 45
sudo ip -f inet6 addrlabel add prefix 2001:470:c2d7:917:20c:29ff:fe30:1a71 label 45
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
Subscribe to:
Posts (Atom)
Followers
About Me
- Krzysztof Pakuła
- Znak zodiaku - Lew