Pular para o conteúdo principal

Generatin wildcard cert win SAN's with letsencrypt

· Leitura de um minuto

Step 1

Generate the wildcard cert

certbot-auto certonly --manual \
--preferred-challenges=dns \
--email myemail@myemail.com \
--server https://acme-v02.api.letsencrypt.org/directory \
--agree-tos \
-d *.mydomain.com

Step 2

After adding TXT entry into your DNS server and confirming the previous operation worked, add the SAN's with the following command:

certbot-auto certonly --manual --server https://acme-v02.api.letsencrypt.org/directory --expand -d mydomain.com,myanotherdomain.com,mythirdmydomain.com

Bash strict mode

· Leitura de 2 minutos

All contents are from this address: http://redsymbol.net/articles/unofficial-bash-strict-mode/
Consider reading the original article instead.

Headline bash script:

#!/bin/bash
set -euo pipefail
IFS=$'\n\t'

Reference: http://redsymbol.net/articles/unofficial-bash-strict-mode/

Explanation

set -e

The set -e option instructs bash to immediately exit if any command [1] has a non-zero exit status. You wouldn't want to set this for your command-line shell, but in a script it's massively helpful. In all widely used general-purpose programming languages, an unhandled runtime error - whether that's a thrown exception in Java, or a segmentation fault in C, or a syntax error in Python - immediately halts execution of the program; subsequent lines are not executed.

set -e

set -u

set -u affects variables. When set, a reference to any variable you haven't previously defined - with the exceptions of $* and $@ - is an error, and causes the program to immediately exit. Languages like Python, C, Java and more all behave the same way, for all sorts of good reasons. One is so typos don't create new variables without you realizing it.

set -u

set -o pipefail

This setting prevents errors in a pipeline from being masked. If any command in a pipeline fails, that return code will be used as the return code of the whole pipeline. By default, the pipeline's return code is that of the last command - even if it succeeds.

set -o pipefail

The IFS variable

The IFS variable - which stands for Internal Field Separator - controls what Bash calls word splitting. When set to a string, each character in the string is considered by Bash to separate words. This governs how bash will iterate through a sequence.

IFS=$'\n\t'

Examples:

3 useful commands to backup and restore an MySQL database

· Leitura de um minuto
#dump
mysqldump -u root -p root > /var/lib/mysql/bkp/dump.sql

#restore after creating empty database
mysql -u root -p db < /var/lib/mysql/bkp/dump.sql

#Grant privileges to user
GRANT ALL ON db.* TO 'root@%' IDENTIFIED BY 'dbpassword';

Inhibit SSH from closing /crashing when using remote desktop (vnc)

· Leitura de um minuto
  1. Put the following script in /usr/bin/inhibit-sleep-for-ssh and make it executable
#!/bin/bash
sudo systemd-inhibit --what sleep --why "Do not sleep while $SSH_TTY session is active" sleep infinity &
INHIBIT_PID=$!

if [ -z "$SSH_ORIGINAL_COMMAND" ]; then
$SHELL
else
$SSH_ORIGINAL_COMMAND
fi
sudo kill $INHIBIT_PID
  1. Edit the file /etc/ssh/sshd_config and insert the following line
ForceCommand /usr/bin/inhibit-sleep-for-ssh

POSTGRESQL: Get connection usage

· Leitura de um minuto
select max_conn,used,res_for_super,max_conn-used-res_for_super res_for_normal 
from
(select count(*) used from pg_stat_activity) t1,
(select setting::int res_for_super from pg_settings where name=$$superuser_reserved_connections$$) t2,
(select setting::int max_conn from pg_settings where name=$$max_connections$$) t3

Reference: https://dba.stackexchange.com/a/161761

Uploading multipart/form-data in Java: The right way to do it

· Leitura de um minuto

With Unirest:


import kong.unirest.ContentType;
import kong.unirest.HttpResponse;
import kong.unirest.Unirest;

import java.io.File;
import java.io.FileInputStream;
import java.nio.charset.StandardCharsets;

public class UnirestTest {

public static void main(String[] args) throws Exception {

final File file = new File("/home/rafael/tmp/file-sample.pdf");
final FileInputStream input = new FileInputStream(file);
HttpResponse<String> response = Unirest.post("https://httpbin.org:80/post")
.header("Authorization", "Bearer my_token")
.header("Accept","*/*")
.field("file", input, ContentType.create("application/pdf"), "file")
.asString();

System.out.println("response = " + response.getStatus());
System.out.println(response.getBody());
}


}

Dependency used

 <dependency>
<groupId>com.konghq</groupId>
<artifactId>unirest-java</artifactId>
<version>3.12.0</version>
</dependency>

With OkHttp

import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

public class OKTest {

static Logger logger = LoggerFactory.getLogger(OKTest.class);

public static void main(String[] args) throws Exception{


OkHttpClient client = new OkHttpClient().newBuilder().build();

RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", "file.pdf",
RequestBody.create(MediaType.parse("application/pdf"),
new File("/home/rafael/tmp/file-sample.pdf")))
.build();

Request request = new Request.Builder()
.url("https://httpbin.org/post")
.post(requestBody)
.addHeader("Authorization", "Bearer my_token")
.addHeader("Accept","*/*")
.addHeader("Accept-Encoding","gzip, deflate")
.build();

Call call = client.newCall(request);
Response response = call.execute();
final String body = new String(response.body().bytes());
System.out.printf("response code %s body %s\n",response.code(), body);



}

}

Dependency used:

 <dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.2.0</version>
</dependency>

Listing Windows processes with command line arguments

· Leitura de um minuto
WMIC path win32_process get Caption,Processid,Commandline

If using powershell you can use with |grep like this example:

WMIC path win32_process get Caption,Processid,Commandline | grep googledrive

Finding out which docker containers are the most consuming disk resources ones

· Leitura de um minuto

Step 1 - Install ncdu

# Centos
sudo yum install -y ncdu
# Arch/Manjaro
sudo pacman -S ncdu
# Debian/Ubuntu
sudo apt install ncdu

Step 2 - Gererate report

Use the ncdu to generate a zipped report, by limiting to the same file system of the desired folder with a single line output feedback:

ncdu -1xo - / | gzip >root_fs_report.gz

Final step- Read the report

Read the zipped report with ncdu and make the proper analysis

zcat root_fs_report.gz | ncdu -f-

Extra mile step

If the resource high consuming is caused by logging(big log file), you will find out that a specific container log file is the real culprit. You can confirm this by executing the following command

# Get the log file dir of your container 
docker inspect --format='{{.LogPath}}'

So you can compare the above command output the report given by ncdu. I'll want to clear the log file if it's the culprit with the following command:

 echo "" > $(docker inspect --format='{{.LogPath}}' )

References:

https://sempreupdate.com.br/ncdu-verifica-o-uso-do-espaco-em-disco-no-linux-unix

Using mvn to set pom.xml version and afterwards show the current project version

· Leitura de um minuto

Setting a new version

mvn versions:set -DnewVersion=1.0.3-SNAPSHOT

Showing current project version

MVN_VERSION=$(mvn -q -Dexec.executable=echo -Dexec.args='${project.version}' --non-recursive  exec:exec); echo $MVN_VERSION

References:

  1. https://www.mojohaus.org/versions-maven-plugin/examples/set.html
  2. https://stackoverflow.com/a/26514030
  3. https://www.mojohaus.org/exec-maven-plugin/