forked from steadfasterX/lglaf
-
Notifications
You must be signed in to change notification settings - Fork 2
/
send-file.py
executable file
·62 lines (40 loc) · 1.33 KB
/
send-file.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python
import lglaf
import argparse
import sys
comm = lglaf.autodetect_device()
def make_exec_request(shell_command):
argv = b'sh -c eval\t"$*"\t -- '
argv += shell_command.encode('ascii') if isinstance(shell_command, str) else shell_command
if len(argv) > 255:
raise RuntimeError("Command length %d is larger than 255" % len(argv))
return lglaf.make_request(b'EXEC', body=argv + b'\0')
def send_command(cmd):
cmd = make_exec_request(cmd)
return comm.call(cmd)[1]
def send_file(src, dst):
with open(src, 'rb') as fp:
fp.seek(0, 2)
size = fp.tell()
fp.seek(0)
overhead = len('sh -c eval\t"$*"\t -- printf "">>')
block_size = (255 - (overhead + len(dst))) // 4
send_command(b'printf >' + dst.encode('ascii'))
written = 0
while True:
data = fp.read(block_size)
if not data:
break
dlen = len(data)
if isinstance(data, str):
data = [ord(ch) for ch in data]
hexstr = ''.join('\\x{:02x}'.format(ch) for ch in data)
send_command('printf "{0}">>{1}'.format(hexstr, dst))
written += dlen
sys.stderr.write('\rSending... %.2f%% ' % (100.0 * written / size))
sys.stderr.write('\n')
parser = lglaf.parser
parser.add_argument('local')
parser.add_argument('remote')
args = parser.parse_args()
send_file(args.local, args.remote)