2 Temmuz 2020 Perşembe

openssl pkcs12 aracı - PFX Dosyası

Giriş
PKCS#12 Dosya Formatı yazısına bakabilirsiniz. Bu dosyada private key + certificate bulunur. Açıklaması şöyle
When you use "OpenSSL" to generate private keys and certificates, they are stored as individual separate files. But "OpenSSL" does offer the "openssl pkcs12" command to merge private keys and certificates into a PKCS#12 file.

The "openssl pkcs12" command is very important if you want exchange private keys can certificates between "keytool" and "OpenSSL"
Örnek - PKCS Formatında Private Key Çıkartma
Şöyle yaparız.
openssl pkcs12 -in [yourfilename.pfx] -nocerts -out [keyfilename-encrypted.key]
Çıktı olarak 3 defa şifre sorulur. Import password PFX dosyasının şifresidir. Bu dosyayı okumak için gerekir.  PEM pasword ise private key dosyasını koruyan şifredir.
Enter Import Password:

Enter PEM pass phrase:
Verifying — Enter PEM pass phrase:
Daha sonra PKCS#8 formatındaki private key dosyasını RSA formatına çevirmek için şöyle yaparız.
openssl rsa -in [keyfilename-encrypted.key] -out [keyfilename-decrypted.key]
Örnek - Certificate Çıkartma
Şöyle yaparız.
openssl pkcs12 -in [yourfilename.pfx] -clcerts -nokeys -out [certificatename.crt]
Örnek - Birleştirme
PEM kodeği ile kaydedilmiş sertifika ve private key dosyalarını pkcs12 dosya formatında  birleştirmek için şöyle yaparız
openssl pkcs12 -export -in vsftpd.crt -inkey vsftpd.key > vsftpd.p12
Örnek - Birleştirme
PEM kodeği ile kaydedilmiş sertifika ve private key dosyalarını pkcs12 dosya formatında birleştirmek için şöyle yaparız.
openssl pkcs12 -export -in public.crt -inkey private.key -out mycert.p12 -name tomcat -CAfile intermediate.crt -caname intermediate -chain

bash kodlama - test operator

Giriş
Aslında test komutunu çalıştırır. test komutunun man sayfası burada. Açıklaması şöyle
File operators:
    
      -a FILE        True if file exists.
      -b FILE        True if file is block special.
      -c FILE        True if file is character special.
      -d FILE        True if file is a directory.
      -e FILE        True if file exists.
      -f FILE         True if file exists and is a regular file.
      -g FILE        True if file is set-group-id.
      -h FILE        True if file is a symbolic link.
      -L FILE        True if file is a symbolic link.
      -k FILE        True if file has its `sticky' bit set.
      -p FILE        True if file is a named pipe.
      -r FILE         True if file is readable by you.
      -s FILE        True if file exists and is not empty.
      -S FILE        True if file is a socket.
      -t FD            True if FD is opened on a terminal.
      -u FILE        True if the file is set-user-id.
      -w FILE        True if the file is writable by you.
      -x FILE         True if the file is executable by you.
      -O FILE        True if the file is effectively owned by you.
      -G FILE        True if the file is effectively owned by your group.
      -N FILE        True if the file has been modified since it was last read.

String operators:
    
      -z STRING      True if string is empty.
      -n STRING      True if string is not empty.

 Other operators:
    
      -o OPTION      True if the shell option OPTION is enabled.
      -v VAR         True if the shell variable VAR is set.
      -R VAR         True if the shell variable VAR is set and is a name reference.
Shortcut Syntax
Şöyle yaparız. Burada hem test komutu hem de parantez içindeki komutların if/else veya shortcut syntax ile kullanımı gösteriliyor.
if [ -e /tmp/signal.txt ]; then /opt/myscript.sh; fi
# or
if test -e /tmp/signal.txt; then /opt/myscript.sh; fi

# or shortcut syntax
[ -e /tmp/signal.txt ] && /opt/myscript.sh
# or
test -e /tmp/signal.txt && /opt/myscript.sh

-eq seçeneği - Eşitlik kontrolü
Örnek
Şöyle yaparız.
if [ "$#" -eq 1 ] && [ -f "$1" ]; then

-n seçeneği - String boş değilse - True if string is not empty
String boş değilse 1 döner, boşsa 0 döner
Örnek
Şöyle yaparız. If/else yazmanın bir  başka yolu
# Check existence of JAVA_HOME
[ -n "${JAVA_HOME:-}" ] &&
{ echo "JAVA_HOME check: ADDED"; export PATH="${JAVA_HOME}/bin:$PATH"; } ||
{ echo "JAVA_HOME check: MISSING"; }
Örnek
Şöyle yaparız. Bir komut çalıştırıp çıktısını alırız ve eğer çıktı boş değilse dosyaya yazdırırız
var=$(mycommand)
[[ -n $var ]] && printf '%s\n' "$var" > myfile
Örnek
Şöyle yaparız
if [ -n "${_CE_CONDA}" ] && [ -n "${WINDIR+x}" ]; then
    ...
else
  ...
fi
Şöyle yaparız. Burada "command substitution" yapılıyor. Sonra -n ile string'in boş olmadığı kontrol ediliyor.
if [ "$(id -u)" -eq 0 ]
then
    if [ -n "$SUDO_USER" ] #String boş değilse 1 döner
    then
        printf "This script has to run as root (not sudo)\n" >&2
        exit 1
    fi
    printf "OK, script run as root (not sudo)\n"
else
    printf "This script has to run as root\n" >&2
    exit 1
fi
-v seçeneği - Değer Atanmış Değişken Kontrolü  -True if the shell variable VAR is set.

Örnek 
Elimizde şöyle bir kod olsun. USR1 signal gelirse ve sleep_pid değişkenine değer atanmışsa kontrolü yapılabiliyor.
#!/usr/bin/env bash

kill_the_sleeper () {
   
  if [ -v sleep_pid ]; then
    kill "$sleep_pid"
  fi
}

trap kill_the_sleeper USR1 EXIT

while true; do
  # Signals don't interrupt foreground jobs,
  # but they do interrupt "wait"
  # so we "sleep" as a background job
  sleep 5m &
  # We remember its PID so we can clean it up
  sleep_pid="$!"
  # Wait for the sleep to finish or someone to interrupt us
  wait
  # At this point, the sleep process is dead (either it finished, or we killed it)
  unset sleep_pid

  # PART X: code that executes every 5 mins
done
Bu script çalışırken USR signal gönderilir. Şöyle yaparız
kill -USR1 "$pid_of_the_script"
veya
pkill -USR1 -f my_fancy_script
-z seçeneği - True if string is empty
Örnek
Şöyle yaparız
if [ -z ${CONDITION} ]; then
Şu çalışmaz. CONDITION değişkenine değer atanmadığı halde hep true döner
if $CONDITION; then echo "True" else echo "False" fi


1 Temmuz 2020 Çarşamba

time komutu

Giriş
time komutu belirtilen uygulamanın çalışırken ne kadar zaman tuttuğunu gösterir. Aslında iki tane time komutu var
1. Kabuk içindeki time (bash veya zsh)
2. /usr/bin/time (GNU Time)

Hepsinin çıktısı farklı. Çıktıları şöyle. Eğer bash kullanıyorsak real satırı önemli. Zsh kullanıyorsak en sondaki total önemli. Gnu kullanıyorsak en baştaki değer önemli.
# Bash
real 0m33.961s
user 0m0.340s
sys 0m0.940s

# Zsh
0.34s user 0.94s system 4% cpu 33.961 total

# GNU time (sh)
0.34user 0.94system 0:33.96elapsed 4%CPU (0avgtext+0avgdata 6060maxresident)k
0inputs+201456outputs (0major+315minor)pagefaults 0swaps

if Kullanımı
Örnek
Şöyle yaparız.
$ time if true; then sleep 1; fi

real    0m1.004s
user    0m0.000s
sys     0m0.000s
if/else Kullanımı
Örnek

Şöyle yaparız
$ time if [[ $dir2 -ot $dir1 ]] ; then open $dir2 ; else open $dir1 ; fi
real    0m0.055s
user    0m0.023s
sys     0m0.018s
pipe İle Kullanımı
bash kullanırken time komutu tek başına çalışır ama pipeline içinde çalışmaz. Bunu görmek için 
Örnek
şöyle yaparız
# echo foo | time sleep 1
bash: time: command not found
Şöyle yaparız. En son pipe işlemini ölçer.
# example of pipeline separated with |:
$ time true | sleep 1

real    0m1.004s
user    0m0.000s
sys     0m0.000s
Komut Dizisi Kullanımı
Örnek

Komut dizisini süslü parantez ile kullanmak gerekir. Şu kod yanlış
$ time true && sleep 1

real    0m0.000s
user    0m0.000s
sys     0m0.000s
Şöyle yaparız
$ time { true && sleep 1; }

real    0m1.003s
user    0m0.000s
sys     0m0.000s
-f seçeneği
format anlamına gelir.

%M Format Belirteci
Açıklaması şöyle
Maximum resident set size of the process during its lifetime, in Kbytes.
Örnek
Şöyle yaparız. KB cinsinden maksimum (peak) kullanılan belleği verir.
/usr/bin/time -f "%M" command
-v seçeneği
Şöyle yaparız.
/usr/bin/time -v ./program_name enter_password