Serialize Shell Script for Transfer

In March, I explored how to remotely execute a script with terminator. 3 months later, the process becomes maturer and I would like to share it here.

The encode/decode process becomes a standalone python script.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/usr/bin/env python
# encoding: utf-8

# usage: serialize-sh.py ~/loki/env/bash_aliases_shared
import zlib, sys, base64

f = open(sys.argv[1], 'r')
output = ''
for line in f:
    line = line.lstrip()
    if len(line) > 0 and line[0] != "#":
        output = output + line

output = base64.b64encode(output.encode('zlib'))
output = "echo " + output + " | base64 --decode | python -c 'import zlib,sys;sys.stdout.write(zlib.decompress(sys.stdin.read()))' > /tmp/source; source /tmp/source; rm /tmp/source"

sys.stdout.write(output)

It takes a file path as parameter and generate a one-liner for execution.

Then it would be easy to trigger it with tmux shortcut.

1
bind-key -n "M-o" run 'tmux send-keys " $(~/loki/env/bin/serialize-sh.py ~/loki/env/bash_aliases_shared)" Enter'

Now spin up your docker container, Alt-o to source your shortcuts and enjoy.

Comments