| 1 |
import serial |
| 2 |
import re |
| 3 |
import six |
| 4 |
from time import sleep |
| 5 |
from datetime import datetime, timedelta |
| 6 |
|
| 7 |
from jnpr.junos.transport.tty import Terminal |
| 8 |
|
| 9 |
# ------------------------------------------------------------------------- |
| 10 |
# Terminal connection over SERIAL CONSOLE |
| 11 |
# ------------------------------------------------------------------------- |
| 12 |
|
| 13 |
_PROMPT = re.compile(six.b("|").join([six.b(i) for i in Terminal._RE_PAT])) |
| 14 |
|
| 15 |
|
| 16 |
class Serial(Terminal): |
| 17 |
def __init__(self, port="/dev/ttyUSB0", **kvargs): |
| 18 |
""" |
| 19 |
:port: |
| 20 |
the serial port, defaults to USB0 since this |
| 21 |
|
| 22 |
:kvargs['timeout']: |
| 23 |
this is the tty read polling timeout. |
| 24 |
generally you should not have to tweak this. |
| 25 |
""" |
| 26 |
# initialize the underlying TTY device |
| 27 |
|
| 28 |
self.port = port |
| 29 |
self._ser = serial.Serial() |
| 30 |
self._rx = self._ser |
| 31 |
self._ser.port = port |
| 32 |
self._ser.timeout = kvargs.get("timeout", self.TIMEOUT) |
| 33 |
|
| 34 |
self._tty_name = self.port |
| 35 |
|
| 36 |
Terminal.__init__(self, **kvargs) |
| 37 |
|
| 38 |
# ------------------------------------------------------------------------- |
| 39 |
# I/O open close called from Terminal class |
| 40 |
# ------------------------------------------------------------------------- |
| 41 |
|
| 42 |
def _tty_open(self): |
| 43 |
try: |
| 44 |
self._ser.open() |
| 45 |
except OSError as err: |
| 46 |
raise RuntimeError("open_failed:{}".format(err.strerror)) |
| 47 |
self.write("\n\n\n") # hit <ENTER> a few times, yo! |
| 48 |
|
| 49 |
def _tty_close(self): |
| 50 |
self._ser.flush() |
| 51 |
self._ser.close() |
| 52 |
|
| 53 |
# ------------------------------------------------------------------------- |
| 54 |
# I/O read and write called from Terminal class |
| 55 |
# ------------------------------------------------------------------------- |
| 56 |
|
| 57 |
def write(self, content): |
| 58 |
"""write content + <RETURN>""" |
| 59 |
self._ser.write(six.b(content + "\n")) |
| 60 |
self._ser.flush() |
| 61 |
|
| 62 |
def rawwrite(self, content): |
| 63 |
self._ser.write(content) |
| 64 |
|
| 65 |
def read(self): |
| 66 |
"""read a single line""" |
| 67 |
return self._ser.readline() |
| 68 |
|
| 69 |
def read_prompt(self): |
| 70 |
""" |
| 71 |
reads text from the serial console (using readline) until |
| 72 |
a match is found against the :expect: regular-expression object. |
| 73 |
When a match is found, return a tuple(<text>,<found>) where |
| 74 |
<text> is the complete text and <found> is the name of the |
| 75 |
regular-expression group. If a timeout occurs, then return |
| 76 |
the tuple(None,None). |
| 77 |
""" |
| 78 |
rxb = six.b("") |
| 79 |
mark_start = datetime.now() |
| 80 |
mark_end = mark_start + timedelta(seconds=self.EXPECT_TIMEOUT) |
| 81 |
|
| 82 |
while datetime.now() < mark_end: |
| 83 |
sleep(0.1) # do not remove |
| 84 |
line = self._ser.readline() |
| 85 |
if not line: |
| 86 |
continue |
| 87 |
rxb += line |
| 88 |
found = _PROMPT.search(rxb) |
| 89 |
if found is not None: |
| 90 |
break # done reading |
| 91 |
else: |
| 92 |
# exceeded the while loop timeout |
| 93 |
return (None, None) |
| 94 |
|
| 95 |
return (rxb, found.lastgroup) |