| 1 |
from time import sleep |
| 2 |
import logging |
| 3 |
import sys |
| 4 |
import six |
| 5 |
|
| 6 |
from jnpr.junos.transport._telnetlib import telnetlib |
| 7 |
from jnpr.junos.transport.tty import Terminal |
| 8 |
|
| 9 |
logger = logging.getLogger("jnpr.junos.tty_telnet") |
| 10 |
|
| 11 |
# ------------------------------------------------------------------------- |
| 12 |
# Terminal connection over TELNET CONSOLE |
| 13 |
# ------------------------------------------------------------------------- |
| 14 |
|
| 15 |
|
| 16 |
class PY6: |
| 17 |
NEW_LINE = six.b("\n") |
| 18 |
EMPTY_STR = six.b("") |
| 19 |
NETCONF_EOM = six.b("]]>]]>") |
| 20 |
IN_USE = six.b("in use") |
| 21 |
|
| 22 |
|
| 23 |
class Telnet(Terminal): |
| 24 |
RETRY_OPEN = 3 # number of attempts to open TTY |
| 25 |
RETRY_BACKOFF = 2 # seconds to wait between retries |
| 26 |
|
| 27 |
def __init__(self, host, port, **kvargs): |
| 28 |
""" |
| 29 |
:host: |
| 30 |
The hostname or ip-addr of the terminal server |
| 31 |
|
| 32 |
:port: |
| 33 |
The TCP port that maps to the TTY device on the |
| 34 |
console server |
| 35 |
|
| 36 |
:kvargs['timeout']: |
| 37 |
this is the tty read polling timeout. |
| 38 |
generally you should not have to tweak this. |
| 39 |
""" |
| 40 |
# initialize the underlying TTY device |
| 41 |
|
| 42 |
self._tn = telnetlib.Telnet() |
| 43 |
self._rx = self._tn |
| 44 |
self.host = host |
| 45 |
self.port = port |
| 46 |
self.timeout = kvargs.get("timeout", self.TIMEOUT) |
| 47 |
self.baud = kvargs.get("baud", 9600) |
| 48 |
self._tty_name = "{}:{}".format(host, port) |
| 49 |
|
| 50 |
Terminal.__init__(self, **kvargs) |
| 51 |
|
| 52 |
# ------------------------------------------------------------------------- |
| 53 |
# I/O open close called from Terminal class |
| 54 |
# ------------------------------------------------------------------------- |
| 55 |
|
| 56 |
def _tty_open(self): |
| 57 |
retry = self.RETRY_OPEN |
| 58 |
while retry > 0: |
| 59 |
try: |
| 60 |
self._tn.open(self.host, self.port, self.timeout) |
| 61 |
break |
| 62 |
except Exception: |
| 63 |
retry -= 1 |
| 64 |
logger.info( |
| 65 |
"TTY busy: checking back in {} ...".format(self.RETRY_BACKOFF) |
| 66 |
) |
| 67 |
sleep(self.RETRY_BACKOFF) |
| 68 |
else: |
| 69 |
raise RuntimeError("open_fail: port not ready") |
| 70 |
# the below line was added for console based telnet connection as it needs newline to work. |
| 71 |
# it causes issue in EVO devices. Keeping it as a comment for future enhancement. |
| 72 |
# self.write("\n") |
| 73 |
|
| 74 |
def _tty_close(self): |
| 75 |
self._tn.close() |
| 76 |
|
| 77 |
# ------------------------------------------------------------------------- |
| 78 |
# I/O read and write called from Terminal class |
| 79 |
# ------------------------------------------------------------------------- |
| 80 |
|
| 81 |
def write(self, content): |
| 82 |
"""write content + <ENTER>""" |
| 83 |
logger.debug("Write: %s" % content) |
| 84 |
self._tn.write(six.b((content + "\n"))) |
| 85 |
|
| 86 |
def rawwrite(self, content): |
| 87 |
"""write content as-is""" |
| 88 |
logger.debug("rawwrite: %s" % content) |
| 89 |
# If baud set to 0 write full speed |
| 90 |
if int(self.baud) == 0: |
| 91 |
self._tn.write(content) |
| 92 |
return None |
| 93 |
|
| 94 |
# Write data according to defined baud |
| 95 |
# per 1 byte of data there are 2 additional bits on the line |
| 96 |
# (parity and stop bits) |
| 97 |
if sys.version >= "3": |
| 98 |
content = content.decode("utf-8") |
| 99 |
for char in content: |
| 100 |
self._tn.write(six.b(char)) |
| 101 |
wtime = 10 / float(self.baud) |
| 102 |
sleep(wtime) # do not remove |
| 103 |
|
| 104 |
def read(self): |
| 105 |
"""read a single line""" |
| 106 |
return self._tn.read_until(PY6.NEW_LINE, self.EXPECT_TIMEOUT) |
| 107 |
|
| 108 |
def read_prompt(self): |
| 109 |
_RE_PAT = [six.b(i) for i in Terminal._RE_PAT] |
| 110 |
got = self._tn.expect(_RE_PAT, self.EXPECT_TIMEOUT) |
| 111 |
if PY6.IN_USE in got[2]: |
| 112 |
raise RuntimeError("open_fail: port already in use") |
| 113 |
if len(got) >= 3: |
| 114 |
logger.debug("Got: %s" % got[2]) |
| 115 |
return (None, None) if not got[1] else (got[2], got[1].lastgroup) |