Created
October 14, 2019 23:06
-
-
Save whitequark/51f3ec6240f9a318bb9214ae04f2143a to your computer and use it in GitHub Desktop.
file transfer via `echo -ne`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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