Aug 10, 2018

Apt-get install error

You may get following error when you use apt-get install command.
E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)
E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?
Run ps command to see which processes are using apt.
# ps aux | grep apt
root       3701  0.0  0.0   4628   772 ?        Ss   01:17   0:00 /bin/sh /usr/lib/apt/apt.systemd.daily install
root       3705  0.0  0.0   4628  1680 ?        S    01:17   0:00 /bin/sh /usr/lib/apt/apt.systemd.daily lock_is_held install
_apt       3744  0.5  0.4  88960  8560 ?        S    01:18   0:00 /usr/lib/apt/methods/http
root       3755  0.0  0.0  21536  1000 pts/0    S+   01:18   0:00 grep --color=auto apt
Kill processes
# kill -9 3701
# kill -9 3705
# kill -9 3744
Check ps
# ps aux | grep apt
root       3757  0.0  0.0  21536  1028 pts/0    S+   01:18   0:00 grep --color=auto apt
After killing processes and restart apt-get install command I could install packages.

If you have interrupted apt-get session when you install a package you may see an error.
E: dpkg was interrupted, you must manually run 'sudo dpkg --configure -a' to correct the problem.
In this case to continue setup run:
sudo dpkg --configure -a

Aug 4, 2018

Serve transmission torrent client on Nginx

Transmission web runs on localhost.
http://127.0.0.1:9091/transmission/web/
To make web interface reachable from WAN you can use nginx.

Edit nginx default settings file

/etc/nginx/sites-available$ sudo nano default

server {
       listen 9092;
       listen [::]:9092;

       server_name _;

 add_header Strict-Transport-Security max-age=31536000;

       location / {
         proxy_read_timeout 300;
         proxy_pass_header  X-Transmission-Session-Id;
         proxy_set_header   X-Forwarded-Host   $host;
         proxy_set_header   X-Forwarded-Server $host;
         proxy_set_header   X-Forwarded-For    $proxy_add_x_forwarded_for;       
         proxy_pass         http://127.0.0.1:9091/transmission/web/;
     }
       
     location /rpc {
         proxy_pass         http://127.0.0.1:9091/transmission/rpc;
     }
       
     location /upload {
         proxy_pass         http://127.0.0.1:9091/transmission/upload;
     }
}
We opened port 9092 to internet. Don't forget give permission on firewall for that port.
http://your_website:9092/

Aug 2, 2018

Linux torrent client - transmission-remote

This article shows installing, configure transmission remote package. Some configurations passed to see errors and how to fix them. If you already installed transmission-cli package you can use remote package:
$ sudo transmission-remote --download-dir "/home/ubuntu/Downloads" -a magnet:?xt=urn:btih:2c83f573753d9e987a5bf0fcf8979c4aa137e869&dn=Linux All-In-One For Dummies, 6th Edition&tr=udp://tracker.leechers-paradise.org:6969&tr=udp://tracker.coppersurfer.tk:6969&tr=udp://tracker.opentrackr.org:1337&tr=udp://tracker.pirateparty.gr:6969&tr=udp://eddie4.nl:6969 
Unlike cli package, remote package works on background. It does not refresh status on terminal. Use following parameter to see status of download.
$ transmission-remote -l

Error#1: (http://localhost:9091/transmission/rpc/) Couldn't connect to server

If you see following error then you may not installed daemon or not started it.
[2018-07-29 12:53:33.664] transmission-remote:  (http://localhost:9091/transmission/rpc/) Couldn't connect to server
[2]+  Exit 127                dn=Linux All-In-One For Dummies, 6th Edition
You can install all services from beginning.
$ sudo apt-get update
$ sudo apt-get install transmission-cli transmission-common transmission-daemon
After installation, stop daemon and check settings.
$ sudo service transmission-daemon stop

$ cat /var/lib/transmission-daemon/info/settings.json
Settings not included in this article.
I only changed following parameter to give full access to files/folders created by transmission.
"umask": 2,
Start and check service.
$ sudo service transmission-daemon start

$ ps aux | grep transmission
debian-+ 12324  0.0  0.7 282064  7664 ?        Ssl  13:06   0:00 /usr/bin/transmission-daemon -f --log-error
root     12371  0.0  0.1  14856  1100 pts/0    S+   13:08   0:00 grep --color=auto transmission

$ netstat -tapen | grep transmission
tcp        0      0 0.0.0.0:9091            0.0.0.0:*               LISTEN      113        137537     12324transmission- 
tcp        0      0 0.0.0.0:51413           0.0.0.0:*               LISTEN      113        137538     12324transmission- 
tcp6       0      0 :::51413                :::*                    LISTEN      113        137540     12324transmission-  
Now service is working.

Error#2: Unauthorized User


Start torrent again.
$ sudo transmission-remote --download-dir "/home/ubuntu/Downloads" -a magnet:?xt=urn:btih:2c83f573753d9e987a5bf0fcf8979c4aa137e869&dn=Linux All-In-One For Dummies, 6th Edition&tr=udp://tracker.leechers-paradise.org:6969&tr=udp://tracker.coppersurfer.tk:6969&tr=udp://tracker.opentrackr.org:1337&tr=udp://tracker.pirateparty.gr:6969&tr=udp://eddie4.nl:6969 
[1] 12380
[2] 12381
[3] 12382
[4] 12383
[5] 12384
[6] 12385
$ Unexpected response: <h1> 401: Unauthorized</h1> Unauthorized User
Unexpected response: <h1> 401: Unauthorized</h1>Unauthorized User
All-In-One: command not found
^C
[1]   Exit 1                  sudo transmission-remote --download-dir "/home/ubuntu/Downloads" -a magnet:?xt=urn:btih:2c83f573753d9e987a5bf0fcf8979c4aa137e869
[2]   Exit 127                dn=Linux All-In-One For Dummies, 6th Edition
[3]   Done                    tr=udp://tracker.leechers-paradise.org:6969
[4]   Done                    tr=udp://tracker.coppersurfer.tk:6969
[5]-  Done                    tr=udp://tracker.opentrackr.org:1337
[6]+  Done                    tr=udp://tracker.pirateparty.gr:6969
Service has username and password defined in settings file.
"rpc-password": "{95a4599e6748c68a07fbdfb7a49e37cb27d1c9b8NjXpGV6G",
"rpc-username": "transmission",
Default username and password: transmission
To use remote commands we need to add auth parameter.
Start torrent again.
$ transmission-remote --auth transmission:transmission --download-dir "/home/ubuntu/Downloads" -a magnet:?xt=urn:btih:2c83f573753d9e987a5bf0fcf8979c4aa137e869&dn=Linux All-In-One For Dummies, 6th Edition&tr=udp://tracker.leechers-paradise.org:6969&tr=udp://tracker.coppersurfer.tk:6969&tr=udp://tracker.opentrackr.org:1337&tr=udp://tracker.pirateparty.gr:6969&tr=udp://eddie4.nl:6969 
[1] 12422
[2] 12423
[3] 12424
[4] 12425
[5] 12426
[6] 12427
$ localhost:9091/transmission/rpc/ responded: "success"
localhost:9091/transmission/rpc/ responded: "success"
All-In-One: command not found

[1]   Done                    transmission-remote --auth transmission:transmission --download-dir "/home/ubuntu/Downloads" -a magnet:?xt=urn:btih:2c83f573753d9e987a5bf0fcf8979c4aa137e869
[2]   Exit 127                dn=Linux All-In-One For Dummies, 6th Edition
[3]   Done                    tr=udp://tracker.leechers-paradise.org:6969
[4]   Done                    tr=udp://tracker.coppersurfer.tk:6969
[5]-  Done                    tr=udp://tracker.opentrackr.org:1337
[6]+  Done                    tr=udp://tracker.pirateparty.gr:6969
$ transmission-remote -l
Unexpected response: <h1> 401: Unauthorized</h1>Unauthorized User
Check torrent status.
$ transmission-remote --auth transmission:transmission -l
ID     Done       Have  ETA           Up    Down  Ratio  Status       Name
   1    n/a       None  Unknown      0.0     0.0   None  Idle         2c83f573753d9e987a5bf0fcf8979c4aa137e869
Sum:              None               0.0     0.0

Error#3: Couldn't create folder


Check service status.
$ sudo service transmission-daemon status
● transmission-daemon.service - Transmission BitTorrent Daemon
   Loaded: loaded (/lib/systemd/system/transmission-daemon.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2018-07-29 13:06:31 UTC; 20min ago
  Process: 12279 ExecStop=/bin/kill -s STOP $MAINPID (code=exited, status=0/SUCCESS)
 Main PID: 12324 (transmission-da)
   Status: "Idle."
    Tasks: 3 (limit: 1152)
   CGroup: /system.slice/transmission-daemon.service
           └─12324 /usr/bin/transmission-daemon -f --log-error

Jul 29 13:17:12 ip-172 transmission-daemon[12324]: [2018-07-29 13:17:12.138] Couldn't create "/home/ubuntu/Downloads/Linux All-In-One For
...
We need to give some permissions
$ sudo usermod -a -G debian-transmission ubuntu

$ sudo chgrp -R debian-transmission /home/ubuntu/Downloads/

$ sudo chmod -R 775 /home/ubuntu/Downloads/
Before starting torrent again, check torrent.
$ transmission-remote --auth transmission:transmission -l
ID     Done       Have  ETA           Up    Down  Ratio  Status       Name
   1*    0%   49.15 kB  Unknown      0.0     0.0    0.0  Stopped      Linux All-In-One For Dummies, 6th Edition
Sum:          49.15 kB               0.0     0.0
We can remove a torrent from the list.
$ sudo transmission-remote --auth transmission:transmission -t 1 -r
localhost:9091/transmission/rpc/ responded: "success"
$ transmission-remote --auth transmission:transmission -l
ID     Done       Have  ETA           Up    Down  Ratio  Status       Name
Sum:              None               0.0     0.0
Start torrent again and watch for status.
$ transmission-remote --auth transmission:transmission --download-dir "/home/ubuntu/Downloads" -a magnet:?xt=urn:btih:2c83f573753d9e987a5bf0fcf8979c4aa137e869&dn=Linux All-In-One For Dummies, 6th Edition&tr=udp://tracker.leechers-paradise.org:6969&tr=udp://tracker.coppersurfer.tk:6969&tr=udp://tracker.opentrackr.org:1337&tr=udp://tracker.pirateparty.gr:6969&tr=udp://eddie4.nl:6969 
[1] 12563
[2] 12564
[3] 12565
[4] 12566
[5] 12567
[6] 12568
$ localhost:9091/transmission/rpc/ responded: "success"
localhost:9091/transmission/rpc/ responded: "success"
All-In-One: command not found

[1]   Done                    transmission-remote --auth transmission:transmission --download-dir "/home/ubuntu/Downloads" -a magnet:?xt=urn:btih:2c83f573753d9e987a5bf0fcf8979c4aa137e869
[2]   Exit 127                dn=Linux All-In-One For Dummies, 6th Edition
[3]   Done                    tr=udp://tracker.leechers-paradise.org:6969
[4]   Done                    tr=udp://tracker.coppersurfer.tk:6969
[5]-  Done                    tr=udp://tracker.opentrackr.org:1337
[6]+  Done                    tr=udp://tracker.pirateparty.gr:6969
$ transmission-remote --auth transmission:transmission -l
ID     Done       Have  ETA           Up    Down  Ratio  Status       Name
   2     0%       None  Unknown      0.0     0.0   None  Idle         Linux All-In-One For Dummies, 6th Edition
Sum:              None               0.0     0.0
$ transmission-remote --auth transmission:transmission -l
ID     Done       Have  ETA           Up    Down  Ratio  Status       Name
   2     3%   507.9 kB  4 min        0.0   258.0    0.0  Downloading  Linux All-In-One For Dummies, 6th Edition
Sum:          507.9 kB               0.0   258.0
$ transmission-remote --auth transmission:transmission -l
ID     Done       Have  ETA           Up    Down  Ratio  Status       Name
   2   100%   13.37 MB  Done         0.0     0.0    0.0  Idle         Linux All-In-One For Dummies, 6th Edition
Sum:          13.37 MB               0.0     0.0

Jul 31, 2018

Ubuntu torrent client - Transmission-cli

If you need a torrent client on Ubuntu server you can use transmission package. This tutorial explains only command line interface.

Install
sudo apt-get install transmission-cli
Cli package has no setting file! You have to give settings with parameters.

Get help
$ transmission-cli --help
transmission-cli 2.92 (14714)
A fast and easy BitTorrent client

Usage: transmission-cli [options] <file|url|magnet>

Options:
 -h  --help                          Display this help page and exit
 -b  --blocklist                     Enable peer blocklists
 -B  --no-blocklist                  Disable peer blocklists
 -d  --downlimit            <speed>  Set max download speed in kB/s
 -D  --no-downlimit                  Don't limit the download speed
 -er --encryption-required           Encrypt all peer connections
 -ep --encryption-preferred          Prefer encrypted peer connections
 -et --encryption-tolerated          Prefer unencrypted peer connections
 -f  --finish               <script> Run a script when the torrent finishes
 -g  --config-dir           <path>   Where to find configuration files
 -m  --portmap                       Enable portmapping via NAT-PMP or UPnP
 -M  --no-portmap                    Disable portmapping
 -p  --port                 <port>   Port for incoming peers (Default: 51413)
 -t  --tos                  <tos>    Peer socket TOS (0 to 255,
                                     default=default)
 -u  --uplimit              <speed>  Set max upload speed in kB/s
 -U  --no-uplimit                    Don't limit the upload speed
 -v  --verify                        Verify the specified torrent
 -V  --version                       Show version number and exit
 -w  --download-dir         <path>   Where to save downloaded data
Usage: transmission-cli [options]
Download a torrent with download directory parameter.
sudo transmission-cli  http://torrent.ubuntu.com:6969/file?info_hash=D%C0%809M3%5B%03%0B%13%FA%F1%3F%A8%84%CD%22%15%028 --download-dir "/home/ubuntu/Downloads"
You can use magnet link instead of torrent file url.
If you are using AWS or any firewall, don't forget to give permission for "Port for incoming peers (Default: 51413)"
After download finishes you will see transmission will continue to run for seeding. It may be annoying if you are working on terminal because it interrupts terminal window (even you press to Ctrl-C to quit).
To kill the process type ps to see process id.
  PID TTY          TIME CMD
11659 pts/0    00:00:00 sudo
11668 pts/0    00:00:00 transmission-cl
Then kill process.
$ kill 11668

Jul 29, 2018

Linux commands - Process

List running processes
$ ps -A
  PID TTY          TIME CMD
    1 ?        00:00:03 systemd
    2 ?        00:00:00 kthreadd
    4 ?        00:00:00 kworker/0:0H
    6 ?        00:00:00 mm_percpu_wq
    7 ?        00:00:00 ksoftirqd/0
    8 ?        00:00:00 rcu_sched
    9 ?        00:00:00 rcu_bh
   10 ?        00:00:00 migration/0
   11 ?        00:00:00 watchdog/0
   12 ?        00:00:00 cpuhp/0
 4712 ?        00:00:00 nginx
Kill a process
$ kill [signal] PID(s)

$ kill -9 3139

Signal Name  Signal Value  Behaviour
SIGHUP   1   Hangup
SIGKILL   9   Kill Signal
SIGTERM   15   Terminate
Filter process by name
$ ps aux | grep nginx

root      4712  0.0  0.1 140644  1452 ?        Ss   Jul27   0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data  4714  0.0  0.4 143260  4212 ?        S    Jul27   0:00 nginx: worker process
ubuntu   11494  0.0  0.1  14856  1056 pts/0    S+   10:50   0:00 grep --color=auto nginx

$ pgrep nginx

4712
4714
Kill process by name
$ pkill nginx

$ killall nginx

Jul 6, 2018

Anonymous level security token - Windows Server

During a software installation if setup fails and shows similar logs like below then it is related with Windows Component system.

MSI (c) (48:B0) SOFTWARE RESTRICTION POLICY: Verifying package --> '...\NC.x64.msi' against software restriction policy 
MSI (c) (48:B0)  Note: 1: 2262 2: DigitalSignature 3: -2147287038  
MSI (c) (48:B0)  SOFTWARE RESTRICTION POLICY: ...\NC.x64.msi is not digitally signed 
MSI (c) (48:B0) SOFTWARE RESTRICTION POLICY: ...\NC.x64.msi is permitted to run at the 'unrestricted' authorization level.
The software which I tried to install had prerequisites (Dotnet Framework 3.5 etc).
After error, I tried to install Dotnet 3.5 manually from "Server Manager" by clicking "Add roles and features" link. But it failed and showed error below:

Anonymous level security token
To solve I followed these steps:

  1. Click Start > Run, type dcomcnfg.exe
  2. Click OK if you see the UAC warning
  3. Go to Component Services> Computers in the tree
  4. Right click My Computer > select Properties.
  5. Click Default Properties tab
  6. Select Connect in the Default Authentication Level list 
  7. Select Identify in the Default Impersonation Level list
  8. Click OK, and confirm the selection
  9. Close Component Services console




Feb 5, 2018

Python code to exe (Windows)

PyInstaller helps you convert your python code to executable software package.

Our test environment: Windows 7, Python 3.6

Create your python code file (hello.py)
print("Hello World!")
Download PyInstaller via pip
pip install pyinstaller
Run PyInstaller with python code file parameter on console
"C:\Program Files\Python36\Scripts\pyinstaller.exe" hello.py
You will see some commands on the screen. A few seconds later it will finish building. Build time is dependent your code (referenced packages).

Check dist folder for distribution copy of your application (hello.exe)

Size of the folder for hello.py is around 11mb.

I tried also py file with reference of cv2 library. Build time is around 30seconds and folder size becomes 157mb.

Oct 19, 2017

How to free up disk space on Windows

If you have a small disk size and have to open free space frequently on Windows then this post is for you.

You deleted some files, emptied recycle bin, moved some files to another drive but still you need free space. Because Windows is living, getting updates, creating cache files also you install new applications. First we will give a chance to Windows for disk cleanup then we will try to find more files to delete.

  1. Windows Cleanup

  2. Right click to C:\ drive and select Properties. You will see Disk Cleanup  button in General tab.
     

    This will give you some space. You can see many unnecessary items here like temporary files, Windows Update files etc. If you are not happy with that space then continue.

  3. Find more files

  4. WinDirStat is a utility that shows size statistics about your files and folders in your drive. With this tool you can see which folders and files have the most size. I love this tool because its graphical interface shows file types in colors, sizes as blocks. You can select a big block and see which file it is.  For example I ordered folders and files by size. In my computer the Installer folder is very huge. It occupies nearly 30% size in Windows folder. When you explore in Installer folder you will see many setup and patch files. We can't know which file is used by the system or necessary by looking their names. File names are like "1a88ba.msp".

  5. Analyze Installer folder

  6. You shouldn't delete files from Installer folder because uninstall process will search for installation files in this folder. Alternatively you can backup this folder content in somewhere else or zip the folder. This may also can create workload because you need to put right file again to start uninstall process.

    Patch Cleaner is an application that searchs Installer folder for unnecesarry files -namely orphaned files. 

    You can see necessary files with their explanation.
     

    Orphaned file list shows files that are not connected to any application.



    You can move orphaned files to another drive like a USB disk by pressing Move button.

  7. Continue to investigate files with WinDirStat

Oct 16, 2017

Install opencv package in Python

Opencv is very popular library on all platforms. This article explains how to install opencv package for Python. This will give you a general idea about installing any library in Python environment.

In code samples you will see cv2 library is imported.
import cv2
If you run sample code without installing opencv you will get following error.
Traceback (most recent call last):
  File "pythonface.py", line 2, in
    import cv2
ImportError: No module named cv2

Installing python package from online repository is very easy in python. You can search for a package in PyPI (Python Package Index) interface.

Type opencv to search box in pypi webpage (https://pypi.python.org/pypi)

This will give you a long list which contains your keyword.
List cropped for blog post.

Finding right package may be hard in this list. You can find package names from original sources of specific libraries.

If you click "opencv-python 3.3.0.10" from the list (https://pypi.python.org/pypi/opencv-python/3.3.0.10) you will see installation and usage guide.

On docs.opencv.org you can see library name as opencv-python:
OpenCV-Python is the Python API for OpenCV, combining the best qualities of the OpenCV C++ API and the Python language.
Use following command to install opencv library to your computer from PyPI.
pip install opencv-python
Now you are ready to use opencv functions.

If you can't find opencv-python package in your installation you can download source codes and run commands according to your environment. This link will give you an idea about sequence of this work.

Aug 15, 2017

Split video into frames

You can split video into frames with AForge libraries.

Add reference following libraries:
  • AForge.Video
  • AForge.Video.FFMPEG

// create instance of video reader
VideoFileReader reader = new VideoFileReader();
// open video file
reader.Open("C:\\test.mp4");
long framecount = reader.FrameCount; 

// read video frames 
for (int i = 0; i < framecount; i++)
{
 Bitmap videoFrame = reader.ReadVideoFrame();
 
 videoFrame.Save(Environment.CurrentDirectory + "\\" + i + ".bmp");

 // dispose the frame when it is no longer required
 videoFrame.Dispose();
}
reader.Close();

Quickwatch reader object
ReadVideoFrame: Read next video frame of the currently opened video file.