TP-Link AC1200 up and running on ubuntu 18.xx

Finally I got from Amazon this fast wifi dongle that’s working also with Ubuntu 18.04. The TP-link AC1200(T4UH) doesn’t come up with plug and play functionality but the driver is very easy to install on Ubuntu and Debian distributions.

The dongle is based on the RealteK RTL8812AU chipset, and luckly this driver it’s already present on the official repository. All you need to do it’s launch this command from you terminal to install it:

sudo apt install rtl8812au-dkms

plug in your device and it should be ready to connect to the WIFI!

Check your ssh key passphrase

How can we check the passphrase of a private key?

Is it possible to do that using the following command:

ssh-keygen -y
  • If the input is the correct passphrase, it will show you the associated public key.
  • the wrong passphrase, it will display load failed.
  • If the key has no passphrase, it will not prompt you for a passphrase and will immediately show you the associated public key.

How to use multiple SSH private keys on a client

To use multiple shh private keys on a client you need to create the file config inside the folder .ssh and use the following entries:

Host myshortname realname.example.com
HostName realname.example.com
IdentityFile ~/.ssh/realname_rsa
User remoteusername

Host myother realname2.example.org
HostName realname2.example.org
IdentityFile ~/.ssh/realname2_rsa
User remoteusername

where IdentityFile is the private key for realname.

Create Autoloader using Composer

How to create an autoloader using composer?

  • Create a new file composer.json in the main folder and insert the just curly brackets
{

}
  • Run, to generate the autoload structure :
$ composer install
{
        "autoload" : {
            "psr-4": {
                    "Acme\\" : "src"
            }
        }
}

Acme is a example of Company/Project name, and src is the directory that contains all the files we want include in our project.

  • Run, to generate the psr-4 autoload file, the command:
$ composer dump-autoload

The content of the file will be the following one:

<?php

// autoload_psr4.php @generated by Composer

$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);

return array(
    'Acme\\' => array($baseDir . '/src'),
);
  • Require the file autoload.php in your main project file (e.g.: index.php):
require 'vendor/autoload.php';
  • Use namespaces Acme in your project for the file not in subfolder but just in src: So for example if you have the class Test.php in src you just need to use this.
//Test.php
namespace Acme; //in your class called Test

class Test
{

}

//index.php
use Acme\Test; //to use the class Test

You don’t need to use src in your namespace because the autoload has the $basedir on src.

The general rule is >>> what is inside the namespace + Class Name

So for example if you have the folder User in src for the class Person:

You must have the following code structure:

Class Person.php in src/User/:

<?php

namespace Acme\User; //Use Acme + User

class Person
{
    protected $name;

    /**
     * @return name
     */
    public function getName(): string
    {
        return $this->name;
    }

    public function setName(string $name)
    {
        $this->name = $name;
        return $this->name;
    }
}
  • index.php:
<?php

    require 'vendor/autoload.php';

    use Acme\User\Person; //Use the namespace + the class name

    $person1 = new Person();
    $person1->setName('John Doe');
    echo $person1->getName();

Read a JSON file with php

Json file, how to read it using php?

Most importantly, get the JSON content using file_get_contents() and to not avoid warnings specify the sources:

To read the file with an http request specify http/https:

$jsonData = file_get_contents('http://www.sitename.io/data/jsondata.JSON');

To read the file form the filesystem:

$config = file_get_contents(__DIR__ . '/config.json');

decode the JSON using json_decode():

$data = json_decode($config, true);

Use var_dump or print_r to understand the data structure. Having this data:

{
  "databaseOne": {
    "dbhost": "localhost:8889",
    "dbname": "dev_db_1",
    "dbusername": "root",
    "dbpassword": "root"
  },
  "databaseTwo": {
    "dbhost": "localhost:8889",
    "dbname": "dev_db_2",
    "dbusername": "root",
    "dbpassword": "root"
  }
}

To print the database name of databaseOne:

echo $data['databaseOne']['dbname'];

Use this code to loop through the array:

foreach ($data['databaseOne'] as $field => $value) {
    echo $field . ':' . $value . '</br>';
}

This is an example to show how to print the database name for each entries:

foreach ($$data as $content) {
        if($content) {
            foreach ($content as $key => $value) {
                if ($key === 'dbname'){
                    echo ($value . '</br>');
                }
            }
        }
    }

Composer basics

Here a basics list of Composer commands to retrieve the most used one.

Start new php project:

composer init

Install a new packages with:

composer require packageName

Use https://packagist.org/ to find the exact component name

In order to get the latest versions:

composer update

Uninstall packages from composer:

composer remove packageName

Search package from composer:

composer search packageName

Shows a list of installed packages that have updates available:

composer outdated

You should always run the validate command before you commit your composer.json file:

composer validate

WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!

When you try to connect to a remote server via ssh and you receive the message:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
SHA256:shacode.
Please contact your system administrator.
Add correct host key in /Users/username/.ssh/known_hosts to get rid of this message.
Offending RSA key in /Users/username/.ssh/known_hosts:21
RSA host key for IP_ADDRESS has changed and you have requested strict checking.
Host key verification failed.

you can reset the known_host for the ip_address with the command:

ssh-keygen -R <hostname or ip_address>

the -R hostname Removes all keys belonging to hostname from a known_hosts file.