Geek as a platform

Embedding modules in Python scripts

I am a huge fan of pexpect. For those of you who don't know it, it is a module "based" on the same idea as Expect but in Python. As its author said:

I loved Expect, but I hated TCL, so I wrote this 100% pure Python module that does the same thing

Noah Spurrier

I think it's a great tool for ssh automatization whenever priv/pub keys are not an option.

In my case, since I usually work in isolated systems where I cannot even install local python mudules, and given that I am using just a small part of all the funcionalities provided by this module, I came up with the idea of embedding an old version of pexpect module into my scripts, so I don't have to worry about SCP'ing several files each time I need to run any of them.

The idea is to embed the base64 code of the module into the script, and then create the module on the fly whenever the script is executed (if it is not there already).

Simple stuff.... This is the procedure for Python 2.x:

1) Get the base64 code of the module:

LOCAL $ gzip -c | base64

2) Get MD5 checksum:

LOCAL $ md5sum
1d9643479e2bf16939fcdf007f4bf9f9 *

3) Add the necessary modules to the script:

import sys
from hashlib import md5 
from cStringIO import StringIO    
from base64 import b64decode 
from gzip import GzipFile

4) Import the module, or create it if it doesn't exist:

    import pexpect
except ImportError:  
    #Copy and paste the base64 code from step 1
    pexpect_mod = """   

    #MD5 sum from step 2
    pexpect_mod_md5 = "1d9643479e2bf16939fcdf007f4bf9f9"

    #Decode the module stored in pexpect_mod and load it in a variable
    with GzipFile(mode='r', fileobj=StringIO(b64decode(pexpect_mod))) as pexpect_mod_fd:
        pexpect_mod_data =

    #Dump the variable into a file
    with open("","w+b") as pexpect_fd:

    #Double-check is the same file you have in your local machine    
    with open("", 'rb') as pexpect_fd:
        if pexpect_mod_md5 == md5(
            #Import the module
            import pexpect
            #Exit if the file is not identical 
            print("Error creating module. MD5 checksum incorrect. Exiting")

And voila, just you run your script in your remote machine, and there you have the module in your current directory:

REMOTE $ ls -1**

Needless to say you could follow this same approach for any other file you would need to "attach" to your scripts...

Have fun!

Quick Thought I - IPv4 vs TV

Watching Mr Robot Episode 1 (I know, I am kind of late, but I have a lot of TV series in my "backlog") I noticed again they used a wrong IPv4 address... Yes, they talk about rootkits, KDE, DDos, etc, etc, but one of the key details of this first episode it's this IP address (not the address itself, but what Malek does with it, if you know what I mean...). How come it's so difficult to remember that only values from 0 and 255 are allowed??? (we could discuss whether they are valid IP addresses for hosts or networks but let's keep it simple)


And unfortunately this simple error is pretty common in TV shows and movies... The same thing happened several months ago in Blackhat, another "hacking" movie:


I cannot wait to see what's going to happen when IPv6 finally kicks in :)

Sharing images like a "Pr0"

Disclaimer: it's summer, we are under a really long heat wave in Spain, so I didn't feel like moving from the couch (you will understand later :D)

Yesterday evening I was laying on the couch surfing Amazon website using an old laptop (very old) that I barely use, and at some point, I took an screenshot I needed to share with a friend via corporate email... (my idea was to send it to my email account, and this morning, send it from my corporate account)

Anyway, after taking the screenshot I realized:

  • I was no logged in gmail, dropbox, github, etc , and in order to login, I would need a token from my cellphone (and my smartphone was in a different room!!!)
  • No USB drives around
  • The image sharing alternatives that I know of, requires you to create an user and login… Too much for a onetime thing

So I decided to use pastebin to store the image in text format:

  1. Take the screenshot (I used scrot, feel free to use any other tool…) : scrot -d 5 ~/amazon.jpg

  2. Gzip the file: gzip ~/amazon.jpg

  3. Convert to base64: base64 ~/amazon.jpg.gz > ~/amazon.b64

  4. Cat the file and paste the (long) result into pastebin and write down the url

This morning from my corporate laptop:

  1. Download the file from pastebin (save it as amazon.b64)

  2. Set file format UNIX... For instance, using vi: ESC:set ff=unix

  3. Decode it:base64 -d amazon.b64 > amazon.jpg.gz

  4. Unzip it: gunzip amazon.jpg.gz

Et voila, we have the same picture….


I know this doesn't seem very useful (or yes, who knows?) but you might follow the same approach for other things… For instance, I use pretty much the same thing in Python for embedding an old version of pexpect module in my scripts


Issues with Synology 2FA

Hi there

After a few months in the shelf, today I reconnected my Synology DS215j, and to my surprise, I wasn't able to login via DiskStation GUI due to a problem with my autenthication:

'DSM Error'

It was weird because:

a) (you are going to love this one...) "It was working before"
b) I could login to my NAS via SSH with the same user/pass, so it was clearly a problem with the 2 Factor Authethication

Once in SYNOLOGY CLI, I realized the system time was wrong:

'ntp mismatch'

As you can (hopefully) see in the picture, the time in the SYNOLOGY CLI (23:58:13) differs from the one in web GUI (23:54) (and the system time in my MBP as well)

So I connected to the NAS with root password (rather sooner than later I would need to "enable" sudo and block root user in SSH) and updated the date by running:

ntpdate -u

(If this isn't working, try killing ntpd and run ntpdate -u again)

After that, I managed to login using my 2FA code as expected...


PS: First post using github as text editor... Let's see how this goes.

Problems installing IPython Notebook in Cygwin (W7)

While trying to "pip install ipython[notebook]" in my Cygwin installation (W7), I got this error due to libzmq:

 Collecting ipython[notebook]


 Running install for pyzmq


gcc -Wno-unused-result -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -ggdb -O2 -pipe -Wimplicit-function-declaration 
-DZMQ_USE_POLL=1 -DHAVE_LIBSODIUM=1 -Ibundled/zeromq/include -Ibundled -Ibundled/libsodium/src/libsodium/include 
-Ibundled/libsodium/src/libsodium/include/sodium -I/usr/include/python3.4m -c bundled/zeromq/src/address.cpp 
-o build/temp.cygwin-2.0.4-i686-3.4/bundled/zeromq/src/address.o

gcc: error: spawn: No such file or directory
error: command 'gcc' failed with exit status 1


Command "/usr/bin/python3 -c "import setuptools, tokenize;__file__='/tmp/pip-build-l4nkh9zq/pyzmq/';
exec(compile(getattr(tokenize,'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" 
install --record /tmp/pip-jicpjqak-record/install-record.txt --single-version-externally-managed --compile" 
failed with error code 1 in /tmp/pip-build-l4nkh9zq/pyzmq

It took me a while to figure it out... In case you were in a similar situation, this is what I did:

1) Install some tools (via apt-cyg or setup.exe)

apt-cyg install libtool automake autoconf   

(you will need the usual tools for compiling C/C++ code, such as gcc,g++,make, etc,etc)


export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig

3) Download and compile libsodium

make && make install

4) Download and compile libzmq

make && make install

5) Finally, try to install ipython[notebook] again:

pip install ipython[notebook]

It worked for me... Hope it helps you.