I would like to generate a multipart mime-encoded document for use as user data on EC2 instances.

On Ubuntu, one is supposed to use write-mime-multipart to do this, as per these instructions.

Does anyone know what would the OSX command-line equivalent be?


2 Answers 2


I’ve found a simple solution. Ubuntu’s write-mime-multipart command is actually a Python script, available here (or on an Ubuntu distro I guess).

I’ve simply downloaded this script to use. Perhaps not ideal with respect to version control, but okay for a simple script I think.

Better approaches welcome!

  • Thanks! This is great. I changed it to #!/usr/local/bin/python (instead of specifying v2.6) and installed the script to /usr/local/Cellar/write-mime-multipart, chmod +x and symlink it into /usr/local/bin to be more like brew. Worked fine. You can also run a Docker instance of Ubuntu to get it. ;-)
    – phpguru
    Oct 16, 2015 at 23:59
  • Cheers. Sounds like a good approach!
    – Leo
    Oct 17, 2015 at 11:00

I couldn't get the script to work, so I put together a different one here

# Usage
# python utils/multi.py /path/to/cloud.cfg:text/cloud-config /path/to/script.sh:text/x-shellscript

import sys
import email
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

if len(sys.argv) == 1:
    print("%s input-file:type ..." % (sys.argv[0]))

combined_message = MIMEMultipart()
for i in sys.argv[1:]:
    (filename, format_type) = i.split(":", 1)
    with open(filename) as fh:
        contents = fh.read()
    sub_message = MIMEText(contents, format_type, sys.getdefaultencoding())
    sub_message.add_header('Content-Disposition', 'attachment; file    name="%s"' % (filename))

msg = email.message_from_string(combined_message.as_string())

for part in msg.walk():
    if part.get_content_maintype() == 'text':
        if part['Content-Transfer-Encoding'] in {'quoted-printable', 'base64'}:
            payload = part.get_payload(decode=True)
            del part['Content-Transfer-Encoding']

# Send the modified message to stdout

Sources: StackOverflow Question CloudInit Docks

  • Please answer with more tan a link. The answer should be understandable when thr links disappear
    – mmmmmm
    Nov 30, 2018 at 11:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .