Skip to content

Instantly share code, notes, and snippets.

@whitequark
Created October 14, 2019 23:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whitequark/51f3ec6240f9a318bb9214ae04f2143a to your computer and use it in GitHub Desktop.
Save whitequark/51f3ec6240f9a318bb9214ae04f2143a to your computer and use it in GitHub Desktop.
file transfer via `echo -ne`
#!/usr/bin/env python3
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
"file", metavar="FILE", type=argparse.FileType("rb"),
help="source file")
parser.add_argument(
"output_name", metavar="OUTPUT-NAME", type=str,
help="destination filename")
parser.add_argument(
"-c", "--chunk-size", type=int, default=512,
help="maximum size of each command after encoding")
args = parser.parse_args()
data = args.file.read()
prefix1 = f"echo -ne >{args.output_name} '"
prefix2 = f"echo -ne >>{args.output_name} '"
suffix = f"'"
chunk_size = (args.chunk_size - len(prefix2) - len(suffix)) // 4
for start in range(0, len(data), chunk_size):
data_chunk = data[start:start + chunk_size]
encoded_chunk = "".join("\\x{:02X}".format(byte) for byte in data_chunk)
print((prefix1 if start == 0 else prefix2) + encoded_chunk + suffix)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment