Chapter 7

Linux

General

checking if port is open with timeout and bash

Success:
$ timeout 2 bash -c "</dev/tcp/canyouseeme.org/80"; echo $?
0
Failure:
$ timeout 2 bash -c "</dev/tcp/canyouseeme.org/81"; echo $?
124
If you must preserve the exit status of bash,

$ timeout --preserve-status 2 bash -c "</dev/tcp/canyouseeme.org/81"; echo $?
143

checking if port is open with nc

$ nc -w 1 --send-only <IP> <PORT> </dev/null

split string to XXX chars

sed -e 's/.\{XXX\}/&\n/g' <file>

login to screen of different user

Solve screen error "Cannot open your terminal '/dev/pts/0' - please check"
When using the screen tool you may be unable to start a screen session but instead encounter an error:

COPY
Cannot open your terminal '/dev/pts/0' - please check.
This is because another user (you) initiated the current terminal – you probably did a sudo su into the user you are now trying to run screen as, right?

There are two ways to resolve this:

Sign out and properly connect / sign in as the user you wish to use.
Run script /dev/null to own the shell (more info over at Server Fault); then try screen again.

To write the output of a command to a file, there are basically 10 commonly used ways

          || visible in terminal ||   visible in file   || existing
  Syntax  ||  StdOut  |  StdErr  ||  StdOut  |  StdErr  ||   file
==========++==========+==========++==========+==========++===========
    >     ||    no    |   yes    ||   yes    |    no    || overwrite
    >>    ||    no    |   yes    ||   yes    |    no    ||  append
          ||          |          ||          |          ||
   2>     ||   yes    |    no    ||    no    |   yes    || overwrite
   2>>    ||   yes    |    no    ||    no    |   yes    ||  append
          ||          |          ||          |          ||
   &>     ||    no    |    no    ||   yes    |   yes    || overwrite
   &>>    ||    no    |    no    ||   yes    |   yes    ||  append
          ||          |          ||          |          ||
 | tee    ||   yes    |   yes    ||   yes    |    no    || overwrite
 | tee -a ||   yes    |   yes    ||   yes    |    no    ||  append
          ||          |          ||          |          ||
 n.e. (*) ||   yes    |   yes    ||    no    |   yes    || overwrite
 n.e. (*) ||   yes    |   yes    ||    no    |   yes    ||  append
          ||          |          ||          |          ||
|& tee    ||   yes    |   yes    ||   yes    |   yes    || overwrite
|& tee -a ||   yes    |   yes    ||   yes    |   yes    ||  append

use password in cli without passing info to bash history

read -s PASS
<do something with PASS>
unset PASS

how to get the command line args passed to a running process on unix/linux

tr \\0 ' ' < /proc/<pid>/cmdline

how to increase speed via tar and ssh and mbuffer

tar zcf - bigfile.tar | mbuffer -s 1K -m 512 | ssh otherhost "tar zxf -"

using named pipe

mkfifo mysql_pipe
gzip -9 -c < mysql_pipe > name_of_dump.gz &amp;
mysqldump database &gt; mysql_pipe
rm mysql_pipe

References

http://en.wikipedia.org/wiki/Named_pipe

GPG import private key and trust

import gpg key

gpg --import private.key

trust ultimate one-liner

export KEY=XXXX
expect -c "spawn gpg --edit-key ${KEY} trust quit; send \"5\ry\r\"; expect eof"

USEFUL LINKS