Posts

Showing posts from January, 2018

Install and configure Exim 4 mail server in Debian 8

One of friends ask me to configure mail in VPS to send messages via web form. He using Wordpress, and can't write custom PHP code to send mail directly via SMTP. Only one way is acceptable here: to use PHP mail() function. Okay, in this case we need a mail server on VPS. I like Exim. To install Exim, run: apt-get install exim4-base After this, run configuration tool: dpkg-reconfigure exim4-config Make selections with using arrow keys, select “Ok” with tab key (note again, server will setup only for send mail): Select: ‘internet site; mail is sent and received directly using SMTP’ Enter FQDN: yourdomain.tld SMTP Listener: enter 127.0.0.1; ::1 Mail destinations - yourdomain.tld, local hostname, localhost.localdomain, localhost Relay Options: Leave blank Follow up Screen to Relay Options: Leave blank DNS Queries: Keep DNS queries to a minimum? select “No” Delivery method: Select “Maildir format in home directory” Choose default unsplit config file by entering “No

Create custom Spinner on ActionBar

Image
Last days I start working on new Android app, working name is PassGen (Password generator) and plan to place spinner onto the ActionBar. Since API 21 ActionBar replaced to more flexible Toolbar with you can put widgets similar to regular layout. I plan to use spinner but by default it look not fine for me. Starting from modify colors, my mind moving me to add second line and icon to each item. Finally, it looks like this: Almost changes are in XML files. In MainActivity: < android.support.v7.widget.Toolbar android :id= "@+id/toolbar" android :layout_width= "match_parent" android :background= "@color/colorPrimaryDark" android :layout_height= "?attr/actionBarSize" > < Spinner android :id= "@+id/spinner" style= "@style/SpinnerTheme" android :layout_width= "wrap_content" android :layout_height= "wrap_content" android :layout_marginEnd= &q

One billion records table in Postgresql

I have a simple table in Postgersql database with character key. CREATE TABLE public.zero_hids ( hexblock character(32) COLLATE pg_catalog."default" NOT NULL, hid bigint, CONSTRAINT zero_hids_pkey PRIMARY KEY (hexblock) USING INDEX TABLESPACE alcal ) It is required to insert into this table 1000000000 unique records by loading from CSV file. We can split this file to 5 parts by 200 millions each and insert it from command line: !/bin/bash time psql --port=5433 -d db1 -c "COPY zero_hids FROM '/tmp/table0.csv' delimiter AS ',' HEADER CSV" ... time psql --port=5433 -d db1 -c "COPY zero_hids FROM '/tmp/table4.csv' delimiter AS ',' HEADER CSV" Size of each part is around 9 Gigabytes. Allowed memory limit to Postgresql is 32Gb. Here is results: COPY 200000000 real 55m16.461s user 0m0.040s sys 0m0.008s COPY 200000000 real 89m48.861s user 0m0.048s sys 0m0.016s COPY 200000000 real 202m21.283s user 0m0.04

Deploy multiple files to multiple servers with Python

In yesterday post I describe how to upload file to multiple servers with scp, sshpass and Python. But sometimes it is required to upload multiple files. For example, I have one hundred files from split one with split command: split file . txt -- additional-suffix = . txt -- number = l/100 --numeric-suffixes f In this case files are named from f00.txt to f99.txt. And, for example, we should put this files for parallel processing on 10 servers. Of course, it is possible to make 10 archives, upload and unpack it, but better way is improve yesterday script to do this by one job, especially if we have more files and/or servers. Let's do mm-scp.py like this ( logindata.py is used some as this ): import sys from subprocess import call from logindata import ip_list user = 'root' local_path = 'upload/in/' remote_path = '/root/in/' pfx = 'f' sfx = '.txt' d = 2 fcnt = 100 scnt = 10   def func ( x ) : for ( ip , pass

Deploy data to multiple servers with Python

One of easiest way to upload files into multiple servers via ssh is using a script that send data uses scp/sshpass and a list of connection data. For this purpose I like to use a Python language. Firstly, define an array with IP and pasword data in logindata.py file: ip_list = [( '5.1.1.2' , 'password1' ), ( '5.1.1.3' , 'password2 ' ), ... ( '5.1.1.N' , 'passwordN ' )] Using separate file for logina data is preferred because you can use multiple similar files with different data, and/or multiple scripts with uses that data to connect. Okay, now we can create upload script (e.g. multi-scp.py ) itself: from subprocess import call from logindata import ip_list user = 'root' local_path = 'uploads/*' remote_path = '/root/' for (ip, password) in ip_list: print ( "-------------- \n Moving to %s" % ip) call([ 'sshpass -p%s scp -r %s %s@%s:%s' % (password, local_

Postgres and JSON

Objective Last times I make research about Internet security. One of interesting things is SSL certificates. To analyze firstly I download RAW data from Censys with 443 port scan. It comes in JSON format. Onset Firstly, I processed file with script to extract IP from other data and skip all errors. In PHP it look like this (almost all PHP code is skipped because not matter in reviewed aspect): while ( $s = fgets( $fin )) { //skip errors like this //{"ip":"33.44.55.66","timestamp":"2017-11-15T18:38:30+01:00","data":{"http":{}},"error":"..."} if (strpos( $s ,'"http":{}},"error":') > 0) continue; //split JSON to separate IP field, ignore date and prepare all other to insert into DB as jsonb //{"ip":"22.33.44.55","timestamp":"2017-11-15T18:38:30+01:00","data":{"http":{"response":{"..."}}}

Installing and using a free GeoIP database

Today's article is about install and update GeoIP database on Debian-like systems, integrate it with PHP and Apache. To install GeoIP on Debian 8 or Ubuntu 16.04 simple run this command: apt-get install geoip-database geoip-database-extra Now you have a GeoIP installed. But databases are obsolete. To update GeoIP databases, find data files on your system: dpkg -L geoip-database | grep GeoIP.dat You get a path like /usr/share/GeoIP/ Go to that directory and download updates: wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz Unpack files with replace old: gzip -df *.gz Rename GeoLiteCity.dat to GeoIPCity.dat: mv -f GeoLiteCity.dat GeoIPCity.dat You can put all these commands into one bash file (e.g. geoipupd.sh ) to run as single command: #!/bin/bash cd /usr/share/GeoIP/ wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz wget

Android: Activity vs AppCompatActivity

By default, Android Studio create new Activity based by AppCompatActivity . In this way, additional modules, such as Android support library will added to the project. But in many cases you can use the basic Activity class. You can choose Activity insted of AppCompatActivity if: target API is 21 or higher (Android 5+)  target API is 17 or higher (Android 4.2+) and you do not use Matherial Design  target API is 15 or higher (Android 4.03+) and you do not use Nested Fragments and Matherial Design. In last case, if you want to use Nested Fragments but don't want Matherial Design, you can use FragmentActivity .

What is GPCL?

GPCL is an object-oriented library for PHP, developed by me and used last ears. Today I write an introduction about GPCL . In few words, with GPCL you can write: include("documents.php"); class MyDoc extends TDocument { function __construct($title) { parent::__construct($title); $this->body->AddHeading(1)->content = "Hello, World!"; } } $doc = new MyDoc("Welcome!"); $doc->PrintAll(); On output you get well-formatted HTML document with "Hello, World" heading and "Welcome" Title.