Skip to content

Instantly share code, notes, and snippets.

@DepthDeluxe
Last active April 18, 2022 15:33
Show Gist options
  • Save DepthDeluxe/0c0ebc71eca38fd2c38db4b4a37744d0 to your computer and use it in GitHub Desktop.
Save DepthDeluxe/0c0ebc71eca38fd2c38db4b4a37744d0 to your computer and use it in GitHub Desktop.
TeraStation Copy.py
#!/usr/bin/python3
'''
This script leverages the command execution mode of acp-commander to dump files from the TeraStation to a user's local machine.
'''
import argparse
import subprocess
import sys
from os.path import exists
parser = argparse.ArgumentParser(description='Copy files from a Buffalo TeraStation using acp-commander.')
parser.add_argument('src', type=str, help='The file to copy locally.')
parser.add_argument('-t', '--terastation', type=str, help='The TeraStation to copy files from, can either be a hostname or IP address.')
parser.add_argument('-p', '--password', type=str, help='Admin password of the TeraStation.')
parser.add_argument('--block-size', type=int, default=64, help='The block size to copy over, if you exceed the command window size, you will only get partial data.')
parser.add_argument('-a', '--acp-commander', type=str, default='acp_commander.jar', help='The path to ACP commander JAR')
args = parser.parse_args()
if not exists(args.acp_commander):
print('ACP commander jar not found in path "%s"' % (args.acp_commander))
sys.exit(1)
cmd_base = ['java', '-jar', args.acp_commander, '-t', args.terastation, '-pw', args.password, '-q', '-c']
block_size = args.block_size
remote_file_in = args.src
num_bytes_left = int(list(filter(lambda txt: txt != '', subprocess.check_output(cmd_base + ['ls -l %s' % (remote_file_in)]).decode().split(' ')))[4])
remote_file_contents = ''
while num_bytes_left > block_size:
print('num bytes left: %s' % num_bytes_left, file=sys.stderr)
remote_cmd = 'cat %s | tail -c %s | head -c %s' % (remote_file_in, num_bytes_left, block_size)
remote_file_contents += subprocess.check_output(cmd_base + [remote_cmd]).decode()
num_bytes_left -= block_size
remote_cmd = 'cat %s | tail -c %s' % (remote_file_in, num_bytes_left)
remote_file_contents += subprocess.check_output(cmd_base + [remote_cmd]).decode()
print(remote_file_contents, end='')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment