Linux SCREEN vs SSH Keys and forwarding an agent

screen+SSHIn this post I describe how I configured my SCREEN and SSH tools to be more friendly than what it was by default. This is  just a minor improvement but hey, every little helps 😉

My day to day work environment looks like this:

Windows (company) laptop + Cygwin + SSH with agent forwarding enabled -> Start SCREEN session -> SSH to remote administration host -> SSH to further hosts to carry out various tasks

There is a couple of problems with this setup:

– firstly the laptop runs Windows – as much as I hate it I can’t do much about it now,
– and also if my SCREEN session detaches for any reason I loose my SSH agent and logging in to further hosts requires again providing credentials “by hand”.


To mitigate these issues I decided to change the process to the following:

Windows (company) laptop + Cygwin + SSH -> SSH to remote administration host -> start SSH agent + add private keys + enable SSH agent forwarding -> Start SCREEN session -> -> SSH to further hosts to carry out various tasks

Now if my screen detaches I still can get back to it and finish off whatever I was doing but this doesn’t prevent me from loosing the SSH agent connectivity within the SCREEN session.

The remaining problem is due to the fact that when a ssh-agent is started it creates a socket/keyring file of which path/name is changing every time new agent is started. Path to the current keyring is saved to an environment  variable – see output of:


The problem is that after your session detaches value of this variable will change as new socket will be created whilst within the SCREEN session the value will remain the same = pointing to invalid location.

Some people write one liners to update the variable in SCREEN to set it to correct path but this is not much of an help if within your SCREEN session you are logged into further hosts.

I decided that on deploy of a new agent I will make sure the keyring/socket is created under same path – to achieve this I added following line to my .bash_profile file on the administration host:

ssh-agent -k
rm -f $SSHKEY
eval $(ssh-agent -a $SSHKEY)

We try to kill any, still running agent (just in case) and then additionally we make sure that the socket file doesn’t exist either (other way next step would fail). Next line gives us PID of the new agent that was created. Last line adds any default keys to the agent – if your keys do have password protection enabled then you will be prompted to provide such passwords.

Now upon logging to the host I get following messages:

SSH_AGENT_PID not set, cannot kill agent
Agent pid 8147

Enjoy never interrupted sessions!

Posted in Uncategorised | Leave a comment

Transcoding video files with FFMPEG to save space

FFMpeg image

So recently I almost run out of free disk space on my Google Drive – free 15G was mostly used by some video recorded with my mobile phone. Before purchasing any further space I decided to take a look on how much I could save on the space if I transcoded the video files that other way were not touched by anything since recorded. After while I learned on how to use the ffmpeg tool to achieve this – the results overcame my expectations.

The command:

$ffmpeg -i [input video file] -crf 28 -vcodec libx264 -preset veryslow [output video file].mp4

Then to tackle all the files in current directory I wrapped this in a simple FOR LOOP (BASH):

$for F in $(find ./ -maxdepth 1 -type f -iname '*.mp4'); do P=$(basename $F); echo $P; ls -lah $F; ffmpeg -i $F -crf 28 -vcodec libx264 -preset veryslow encoded/$P; done

The command above finds all video files in current directory that have extension *.mp4 and is transcoding all of them using the H.264 library with output being written to “encoded” directory under current location (the directory must exist!).

The transcoding takes significant amount of time so for large files it’s best to leave it to run over the night. Saved space vs. quality loss is brilliant – my files shrieked between 4-8 times and I was not able to observer any quality loss (most of the video files in question was taken with my Nexus 5 at HD quality).

Explanation of the used arguments:

-crf 28

Constant Rate Factor – The range of the quantizer scale is 0-51: where 0 is lossless, 23 is default, and 51 is worst possible. A lower value is a higher quality and a subjectively sane range is 18-28. Consider 18 to be visually lossless or nearly so: it should look the same or nearly the same as the input but it isn’t technically lossless.

-preset veryslow

The general guideline is to use the slowest preset that you have patience for. Current presets in descending order of speed are: ultrafast,superfast, veryfast, faster, fast, medium, slow, slower, veryslow, placebo. A slower preset will provide better compression (compression is quality per filesize).

The explanation is taken from here where more information is available.

Posted in The actual scratchpad | Tagged , , , , , , , , , | Leave a comment

A text based text enlarging tool ;)

So recently I have been playing with PIL in Python as I attempted to convert on the fly picture from my web camera to text-graphic… Yeah, I know, it doesn’t even sound right! This project is working but I havent called it yet done – when it is I certainly will publish it here.

Meanwhile (out of boredom) I wrote this tiny script that takes a string that then gets converted to a black&white picture which then gets interpreted a pixel by pixel and printed to the terminal with different character where a pixel belonged to black colour and different where it was white.

It’s easier to explain with an example so this is an output of the following command:

$ python ./ "" 15

The font can be specified within the script – I went for the TTF type of fonts.

I had no purpose for this script when writing it but I imagine it could be used to generate some MOTD (Message Of The Day) content or whatever else…

The script can be cloned from it’s repository here:
Hope you enjoy it.

Posted in Programming | Tagged , , , | Leave a comment

A bluetooth started song on a MPD server

This is a very simple Python script that I put together some time ago. I had only two goals on my mind when writing this: see if I can and fun. Both goals were achieved.

The script uses bluetooth module to scan for bluetooth device in surroundings and compares it with those provided in script itself. If it encounters a matching device then it will attempt to connect to the MPD server and play selected song. If the song is not on the current play list then it will try to find it in the library, add i to the play list and play.

I ran this script at my work desktop (Ubuntu with bluetooth dongle) and triggered it with my phone’s bluetooth (by enabling it and making visible) for couple of days when entering the office I felt like the Lord Vader with imperial march was started upon my arrival.

Full script can be obtained from my Bitbucket Git repository, below are only few “configuration” lines:

Define device and music track:

user3 = {
'title':"The Empire Strikes Back",

The “bt_addr” is of course the address of the bluetooth device that will trigger the action. The “song” holds a full path to the selected track from point of view of the MPD’s media library where “title” is a piece of title of the song by which we would be able to locate this one specific song in the whole MPD library – it will be used to find and add the track to the current playlist shouldn’t it be there already. “start_second” tells the MPD where to start the track – our track my start from specific moment for greater effect.

All that’s left is to ensure that it will be loud enough to bring an attention:


And start playing at selected moment:


The only issue I had with this is that it usually takes some time for the bluetooth device to be discovered and therefore it’s hard to synchronise with the music. Nevertheless it was fun for me and I hope it will be some use to you also.

Full script can be cloned from with following repository:

Posted in Programming | Tagged | Leave a comment