четверг, 13 июня 2013 г.

Screencast recording in Linux with ffmpeg

Screencast recording can be performed in HQ using ffmpeg x11grab and pulseaudio:
ffmpeg -f alsa -ac 2 -ab 160k -i pulse -acodec pcm_s16le \
    -f x11grab -r 25 -s 1280x1024 -i :0.0+0,0 -aspect 4:3 \
    -vcodec libx264 -vpre lossless_ultrafast -threads 0 \
    screencast.mkv
options:
-s - recording size
-i - display index + start coords (0,0 - upper left corner)

Run
$ x264 --help
to see all available -vpre options.

ffmpeg should be configured as follows:
./configure --prefix=/usr/local/share/ffmpeg/ --enable-x11grab \
    --enable-gpl --disable-ffplay --disable-ffserver \
    --enable-small --disable-debug --disable-yasm \
    --disable-ffprobe --enable-libx264 --enable-shared

cron + expect + interact

Be careful when you run your shell scripts from cron. cron doesn't have all terminal features. E.g. here's a script that does scp:
 spawn   scp file username@myserver:/home/user
  expect  "*assword: $"
  send    "mystrongpass\n"
  interact 
This example is widely spread. BUT. It won't work from cron. Look at the last directive 'interact'. It returns the process control to the user (there's no user) and redirects stdin and stdout to stdin/stdout of the current process. And it crashes. You should use 'expect' instead:
 spawn   scp file username@myserver:/home/user
  expect  "*assword: $"
  send    "mystrongpass\n"
  expect eof

среда, 12 июня 2013 г.

How to su properly in a shell script & find the original user through multiple sudo commands

When you need to perform several commands from the other user inside a script this will do the job:
#!/bin/bash -
su - user <<HERE
ls -al
# other commands run by 'user'
# ...
HERE
When you need to find out the original owner of the current shell always use
who am i | awk '{print $1}'
or
logname
as no other methods are guaranteed (via stackoverflow).
Another useful tool is environment variable
$SHLVL: Incremented by one each time an instance of bash is started.
It won't show the user but can show the shell 'depth'.

bash ssh hosts auto completion

  1. First of all you should have bash_completion module installed (usually it's installed by default in most Linux-based OS).
  2. Disable host hashing. Set
  3. "HashKnownHosts no"
    in
    /etc/ssh/ssh_config (or ~/.ssh/config just for your profile)
  4. Regenerate known_hosts:
    rm ~/.ssh/known_hosts
Now shell will 'remember' your ssh hosts and allow to use hosts auto completion.