Monday 22 May 2017

Using uuencode to embed binaries in scripts and copy/paste transfer files between servers

The uuendiv utility converts binary to text, and uudediv converts the text back to binary.   Wikipedia has a great article on the details of how this proces works (http://en.wikipedia.org/wiki/Uuencoding) 

These utilities create some interesting possibilities.    In this posting I'll cover embeding binary files in scripts and cover how to copy/paste transfer files between servers.

Embeding binary files in a script

If you have a script that also needs to include a binary file, you can uuendiv it and include it within the script itself.  So if you are writing a script to install/setup a binary you could actually also embed the binary within the script itself.   

Here is an example of what this would look like:
#!/usr/bin/ksh
echo this script will extract a binary file
echo the script could also do any other normal scripting stuff here

uudediv -o /tmp/testbinaryfile << 'ENDOFFILE'
<insert uuendiv output of the binary here>
ENDOFFILE

echo the /tmp/testbinaryfile has been extracted
To get the uuendiv output of the binary run a command like this:   uuendiv /usr/bin/ls /dev/stdout .  Take the output and replace the "<insert uuendiv output of the binary here>" text in the example with the uuendiv output.  
When the script runs, the file will be extracted.

Copy/Paste transfer files across servers

If you have a relatively small binary file that you need to transfer between servers, you can easily transfer it by copying and pasting it using uuendiv/uudediv.  This can be a time saver in some circumstances.   It also might be helpful if you have a server that isn't connected to the network but for which you can get a console on through something like the HMC. 

In this example, we will copy and paste the /usr/bin/ls binary between servers. 

On the source server, type:
uuendiv /usr/bin/ls /dev/stdout
Then copy all of the output in to the clipboard.

On the destination server, type:
uudediv -o /tmp/ls
Then press enter, and then paste in the uuendiv output from the source server.   The copy/pasted "ls" binary will be saved to /tmp/ls.   You can verify the source and destination "ls" files are identical by comparing the checksum of the files with the "csum" command.

No comments:

Post a Comment