Scott Baker | 5f6ec0c | 2014-09-26 12:13:21 -0700 | [diff] [blame] | 1 | #! /usr/bin/python |
| 2 | |
| 3 | import base64 |
| 4 | import os |
| 5 | import sys |
| 6 | import subprocess |
| 7 | import StringIO |
| 8 | |
| 9 | if len(sys.argv)<3: |
| 10 | print >> sys.stderr, "syntax: copy-opencloud <srcfn> <desthost:destfn>" |
| 11 | sys.exit(-1) |
| 12 | |
| 13 | srcfn = sys.argv[1] |
| 14 | dest = sys.argv[2] |
| 15 | |
| 16 | if not ":" in dest: |
| 17 | print >> sys.stderr, "malformed desthost:destfn" |
| 18 | sys.exit(-1) |
| 19 | |
| 20 | (hostname,destfn) = dest.split(":",1) |
| 21 | |
| 22 | if destfn.endswith("/"): |
| 23 | destfn = destfn + os.path.basename(srcfn) |
| 24 | |
| 25 | enctext = base64.b64encode(open(srcfn).read()) |
Scott Baker | 4646bf6 | 2014-09-26 12:23:27 -0700 | [diff] [blame] | 26 | #script = 'sudo bash -C "base64 -d -i > %s <<EOF\n%s\nEOF\n"' % (destfn, enctext) |
| 27 | script = 'base64 -d -i > %s <<EOF\n%s\nEOF\n' % (destfn, enctext) |
| 28 | |
Scott Baker | 5f6ec0c | 2014-09-26 12:13:21 -0700 | [diff] [blame] | 29 | |
| 30 | file("/tmp/script","w").write(script) |
| 31 | |
| 32 | p = subprocess.Popen(["ssh", "-A", hostname], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 33 | print p.communicate(input=script)[0] |
| 34 | |
| 35 | |
| 36 | """ |
| 37 | SRCPATHNAME=$1 |
| 38 | DESTHOSTNAME=$2 |
| 39 | DESTPATHNAME=$3 |
| 40 | echo "base64 -d -i > $DESTPATHNAME <<EOF" > /tmp/ssh-up |
| 41 | base64 $SRCPATHNAME >> /tmp/ssh-up |
| 42 | echo "EOF" >> /tmp/ssh-up |
| 43 | ssh -A $DESTHOSTNAME < /tmp/ssh-up |
| 44 | """ |