Showing posts with label cli. Show all posts
Showing posts with label cli. Show all posts

Dec 19, 2019

AWS Lambda Functions

Create Lambda function

Go to AWS Lambda homepage and login to console.
Press "Create Function" button.
Enter function name.
Select a language (environment) from Runtime dropdown. I will use node.js 8.10.
For the first time you can create a new role for permissions. Select "Create a custom role" from Role dropdown box.
A new page will be shown. Select "Create a new IAM Role" from IMA Role dropdown and then give a name. Press "Allow" button.
This will create a new role and browser will return back to "Create function" page.
Press "Create function" button.
It will redirect you to function page.
Initially the page adds a template code which returns success code and a hello message. We will continue with this code.
 

exports.handler = async (event) => {
    // TODO implement
    const response = {
        statusCode: 200,
        body: JSON.stringify('Hello from Lambda!'),
    };
    return response;
};

2. AWS CLI to call the function

Download AWS CLI setup to your computer. For Windows download link is here. For other operating systems you can find links at the same page.
After installation go to aws cli folder and run following command to start configuration.
aws configure
We need following info.
AWS Access Key ID [None]: "Your Access Key"
AWS Secret Access Key [None]: "Your Secret Key"
Default region name [None]: 
Default output format [None]: 
To get keys go to AWS console
Click to your name and select "My Security Credentials".
On "Access keys" section click to "Create New Access Key" button to create new keys. You have to save your secret key because it will displayed only once.
Now return back to aws cli console and enter Access Key ID and Secret Access Key.
Enter region name like "us-west-2".
Enter json for default output format.
Now you can run your functions.
We need our function name as a parameter. In lambda function page copy arn.
Run aws command to run function. The lines between "{ }" is the response from our lambda function. You can see hello message inside txt file which we passed as a parameter.
aws lambda invoke --function-name arn:aws:lambda:us-west-2:123456780315:function:FirstLambda "c:\aws.txt"

{
    "StatusCode": 200,
    "ExecutedVersion": "$LATEST"
}
aws.txt file content
{"statusCode":200,"body":"\"Hello from Lambda!\""}

Nov 12, 2018

Install MySQL Docker (Windows)

I assume Docker already installed and running.

Pull mysql container

You can specify version (5.7). If version is not given it will download latest version.
C:\>docker pull mysql/mysql-server:5.7
5.7: Pulling from mysql/mysql-server
e64f6e679e1a: Already exists
478c78606b7e: Pull complete
81ab104e859d: Pull complete
d3565df0a804: Pull complete
Digest: sha256:79d65bf4360056b0709b4a1c4996f7ef8265ae6ca67462a8570ac1fa0855758b
Status: Downloaded newer image for mysql/mysql-server:5.7
Check docker image
C:\>docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
mysql/mysql-server         5.7                 76ac6291d3cf        2 weeks ago         234MB
Run mysql instance
docker run --name mysql1 -e MYSQL_USER=root -e MYSQL_PASSWORD=root -e MYSQL_DATABASE=homedb -p 3306:3306 -d mysql/mysql-server:5.7 
Check docker processes
C:\>docker ps
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS                             PORTS                 NAMES
44f6c176bb23        mysql/mysql-server:5.7   "/entrypoint.sh mysq…"   14 seconds ago      Up 13 seconds (health: starting)   3306/tcp, 33060/tcp   mysql1

Connect MySQL on docker

docker exec -it mysql1 mysql -uroot -p
If you did not provide a password in docker run command then system will generate a password. To change password and give some permissions:
C:\>docker logs mysql1 2>&1 | FindStr GENERATED
[Entrypoint] GENERATED ROOT PASSWORD: yBiG5ynmyvVurAw93HDok3buGhi

docker exec -it mysql1 mysql -uroot -p

ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';

CREATE USER 'root'@'%' IDENTIFIED BY 'root';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;

SHOW GRANTS FOR 'root'@'%';

Connect MySQL on client

C:\Program Files\MySQL\MySQL Workbench 8.0 CE>mysql -h localhost -P 3306 -u root -p
mysql> select @@hostname;
+--------------+
| @@hostname   |
+--------------+
| 44f6c176bb23 |
+--------------+
1 row in set (0.00 sec)

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