Python or perl base64 from the command line

homeblogmastodonblueskythingiverse



Trying to upload a binary file to a machine hidden behind layers of paranoid firewalls, and for which you only have SSH access?

I spent about half an hour this morning working out how to do this. Assuming the machine has python,

python -c "import base64,sys; base64.encode(sys.stdin,sys.stdout)" <original >base64

Copy and paste the base64 file into the SSH session, eg using cat - >base64.

python -c "import base64,sys; base64.decode(sys.stdin,sys.stdout)" <base64 >original

Voila!


Update: Michael Stillwell sent me these Perl equivalents:

perl -MMIME::Base64 -e 'print encode_base64(join("", <>))' <original >base64

perl -MMIME::Base64 -e 'print decode_base64(join("", <>))' <base64 >original

and also points out that the uuencode and uudecode utilities can be used on most unicies. Typical unix, there's a way to do anything if you know the magic word.


Update 2: Jens Persson notes that it is perfectly fine to squirt binary over SSH directly:

cat original |ssh remote.host "cat >original"

I was worried about escape characters, but SSH only uses escape characters when it allocates a pseudo-terminal, and by default it does not allocate a pseudo-terminal when stdin is not itself a terminal.




[æ]