Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 304 lines (269 sloc) 10.014 kb
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
#!/usr/bin/env python
# Copyright 2011-2012 Avery Pennarun and port.py contributors.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
def assembler(splitter):
    import os, select, subprocess, sys, zlib

    zc = zlib.compressobj()
    zd = zlib.decompressobj()
    def encode(b):
        return ((zc.compress(b) + zc.flush(zlib.Z_SYNC_FLUSH))
                .encode("base64").replace("\n", ""))
    def decode(b):
        try:
            return zd.decompress(b.strip().decode("base64"))
        except Exception:
            sys.stderr.write("ERROR base64 decode: %r\n" % b)
            raise

    cmd = decode(sys.stdin.readline())
    print "%s-RUNNING" % splitter

    p = subprocess.Popen(cmd, shell=True,
                         stdin=subprocess.PIPE, stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE)

    fds = [sys.stdin, p.stdout, p.stderr]
    while p.stdout in fds or p.stderr in fds:
        r,w,x = select.select(fds, [], [])
        if p.stderr in r:
            b = os.read(p.stderr.fileno(), 4096)
            if b:
                print "2 %s" % encode(b)
            else:
                fds.remove(p.stderr)
        elif p.stdout in r:
            b = os.read(p.stdout.fileno(), 4096)
            if b:
                print "1 %s" % encode(b)
            else:
                fds.remove(p.stdout)
        elif sys.stdin in r:
            b = os.read(sys.stdin.fileno(), 4096)
            if b:
                p.stdin.write(decode(b))
            else:
                fds.remove(sys.stdin)
                p.stdin.close()
    rv = p.wait()
    print "%s-EXIT-%d" % (splitter, rv)
# END ASSEMBLER
# The above is the stage2 assembler that gets run on the remote
# system. To ensure that syntax errors and exceptions have useful line
# numbers, keep it at the top of the file.

import re, os, sys, tty, termios, fcntl, select, array, time, uuid, zlib
import options
import port

optspec = """
portsh [options...] <tty> <command string...>
--
t,trace show serial port trace on stderr
s,speed= the baud rate to use [115200]
u,user= response to 'login:' prompt [root]
p,password= response to 'Password:' prompt
"""


def log(s, *args):
    if args:
        ss = s % args
    else:
        ss = s
    sys.stdout.flush()
    sys.stderr.write(ss.replace('\n', '\r\n'))
    sys.stderr.flush()


_want_trace = False
def trace(s):
    if _want_trace:
        log('\x1b[35;1m%s\x1b[m' % re.sub(r'\x1b[[\d;]+[a-z]', '', s))


class Reader(object):
    def __init__(self, fd):
        self.fd = fd
        self.buf = ''

    def fill(self, timeout):
        r,w,x = select.select([self.fd], [], [], timeout)
        if r:
            nbuf = os.read(self.fd, 4096)
            if nbuf:
                trace('(%d)' % len(nbuf))
                self.buf += nbuf.replace('\r\n', '\n')
                return nbuf
        return ''

    def get(self, nbytes):
        out = self.buf[:nbytes]
        self.buf = self.buf[nbytes:]
        return out

    def get_until(self, sep):
        pos = self.buf.find(sep)
        if pos >= 0:
            return self.get(pos + len(sep))

    def get_all(self):
        return self.get(len(self.buf))

    def lines(self):
        while 1:
            line = self.get_until('\n')
            if not line:
                break
            yield line


def read_until_idle(fd, start_timeout):
    timeout = start_timeout
    buf = ''
    while 1:
        r,w,x = select.select([fd], [], [], timeout)
        if r:
            nbuf = os.read(fd, 4096)
            if nbuf:
                trace('(%d)' % len(nbuf))
            buf += nbuf
            timeout = 0.1
        else:
            break
    return buf


def get_shell_prompt(fd, user, password):
    # Send some ctrl-c (SIGINTR) and newlines as a basic terminal reset.
    os.write(fd, '\x03\x03\x03\r\n')
    last_was_sh = 0
    buf = read_until_idle(fd, 0.0)
    for tries in range(10):
        trace(buf.replace('\r', ''))
        bufclean = buf.lower().strip()
        if bufclean.endswith('login:'):
            os.write(fd, user + '\n')
        elif bufclean.endswith('password:'):
                os.write(fd, password + '\n')
                trace('(password)')
        elif ('%s%s' % ('MAGIC', 'STRING')) in buf.replace('\r', ''):
            # success!
            trace('(got a shell prompt)\n')
            return
        elif (not last_was_sh and
              (bufclean.endswith('#') or bufclean.endswith('$') or # sh
               bufclean.endswith('%') or bufclean.endswith('>') or # csh/tcsh
               '\x1b' in bufclean)): # fancy ansi characters
            # probably shell prompt
            os.write(fd, 'printf MAGIC; printf STRING\r')
            trace('(shelltest)\n')
            last_was_sh = 1
        else:
            last_was_sh = 0
            r,w,x = select.select([fd], [], [], 2.0)
            if not r:
                # Send some ctrl-c (SIGINTR), ctrl-d (EOF), and
                # ctrl-\ (SIGQUIT) to try to exit out of anything
                # already running.
                trace('(prodding)\n')
                os.write(fd, '\x03\x03\x03\r\n\x04\x04\x04\x1c\x1c\x1c\r\n')
        buf = read_until_idle(fd, 1.0)
    raise port.ModemError("couldn't get a shell prompt after 10 tries")


def wait_for_string(reader, s):
    timeout = 10.0
    for i in range(50):
        nbuf = reader.fill(timeout)
        timeout = 1.0
        trace(nbuf)
        got = reader.get_until(s)
        if got:
            trace('(got %s)' % s)
            return got[:-len(s)]
    raise port.ModemError("didn't find %r after 10 tries")


PY_SCRIPT1 = r"""
stty sane; stty -echo; python -Sc '
import sys, zlib
print "%s-READY\n" % "SPLITTER";
b = sys.stdin.readline().strip()
exec(zlib.decompress(b.decode("base64")))
assembler("SPLITTER")
'; printf %s-EXIT-97\\n SPLITTER; stty sane; cat
"""

def main():
    o = options.Options(optspec)
    (opt, flags, extra) = o.parse(sys.argv[1:])
    if len(extra) < 2:
        o.fatal("exactly one tty name and a command expected")
    if opt.trace:
        global _want_trace
        _want_trace = opt.trace
    filename = extra[0]
    cmd = ' '.join(extra[1:])

    modem = port.Modem(filename, opt.speed)
    get_shell_prompt(modem.fd, opt.user, opt.password or '')

    splitter = uuid.uuid4().hex
    reader = Reader(modem.fd)

    zc = zlib.compressobj()
    zd = zlib.decompressobj()
    def encode(b):
        return ((zc.compress(b) + zc.flush(zlib.Z_SYNC_FLUSH))
                .encode('base64').replace('\n', ''))
    def decode(b):
        return zd.decompress(b.strip().decode('base64'))

    os.write(modem.fd,
             "%s\r" % PY_SCRIPT1.strip().replace('SPLITTER', splitter))
    wait_for_string(reader, '%s-READY\n' % splitter)

    py_script, junk = open(__file__).read().split('# END ASSEMBLER\n', 1)
    assert junk
    cpy_script = zlib.compress(py_script).encode('base64').replace('\n', '')
    trace('(cpy_script=%d)' % len(cpy_script))
    os.write(modem.fd, "%s\r" % cpy_script)
    zc = zlib.compressobj()
    os.write(modem.fd, "%s\r" % encode(cmd))
    wait_for_string(reader, '%s-RUNNING\n' % splitter)
    split_end = '%s-EXIT-' % splitter

    fds = [0, modem.fd]
    while 1:
        r,w,x = select.select(fds, [], [])
        if 0 in r:
            buf = os.read(0, 128)
            if len(buf):
                trace('>>%s' % buf)
                os.write(modem.fd, encode(buf) + '\n')
            else:
                os.write(modem.fd, '\n\x04') # EOF signal
                fds.remove(0)
        if modem.fd in r:
            nbuf = 1
            while nbuf:
                nbuf = reader.fill(0.1)
                trace(nbuf)
                for line in reader.lines():
                    if split_end in line:
                        pre, rv = line.split(split_end, 1)
                        assert not pre
                        trace('(rv=%r)' % rv)
                        sys.exit(int(rv))
                    if line.startswith('1 '):
                        os.write(1, decode(line[2:]))
                    elif line.startswith('2 '):
                        os.write(2, decode(line[2:]))
                    elif (line.startswith('Traceback ') or
                          line.startswith('ERROR')):
                        log(line)
                        while nbuf:
                            nbuf = reader.fill(1)
                            log(nbuf)
                    else:
                        while nbuf:
                            nbuf = reader.fill(0.1)
                            trace(nbuf)
                        raise port.ModemError('unexpected prefix %r...'
                                              % line[:15])


if __name__ == '__main__':
    main()
Something went wrong with that request. Please try again.