Skip to content

Commit

Permalink
Update console connection tool (#2829)
Browse files Browse the repository at this point in the history
This PR updates console connection tool

Refactor code.
Move most code from __init__.py to console_host.py
Support interactive shell
Add a new interactice posix_shell for devutils
  • Loading branch information
bingwang-ms authored Mar 1, 2021
1 parent 6ef0848 commit c12a893
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 37 deletions.
34 changes: 0 additions & 34 deletions tests/common/connections/__init__.py
Original file line number Diff line number Diff line change
@@ -1,34 +0,0 @@
from tests.common.connections.base_console_conn import CONSOLE_SSH, CONSOLE_SSH_MENU_PORTS, CONSOLE_TELNET
from telnet_console_conn import TelnetConsoleConn
from ssh_console_conn import SSHConsoleConn

__all__ = ["TelnetConsoleConn", "SSHConsoleConn"]

ConsoleTypeMapper = {
CONSOLE_TELNET: TelnetConsoleConn,
CONSOLE_SSH: SSHConsoleConn,
CONSOLE_SSH_MENU_PORTS: SSHConsoleConn
}

def ConsoleHost(console_type,
console_host,
console_port,
sonic_username,
sonic_password,
console_username=None,
console_password=None,
timeout_s=100):
if not ConsoleTypeMapper.has_key(console_type):
raise ValueError("console type {} is not supported yet".format(console_type))
params = {
"console_host": console_host,
"console_port": console_port,
"console_type": console_type,
"sonic_username": sonic_username,
"sonic_password": sonic_password,
"console_username": console_username,
"console_password": console_password,
"timeout": timeout_s
}
return ConsoleTypeMapper[console_type](**params)

35 changes: 35 additions & 0 deletions tests/common/connections/base_console_conn.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
from netmiko.cisco_base_connection import CiscoBaseConnection
from netmiko.ssh_exception import NetMikoAuthenticationException

# For interactive shell
import sys
import socket
from paramiko.py3compat import u
import termios
import tty
import select

# All supported console types
# Console login via telnet (mad console)
CONSOLE_TELNET = "console_telnet"
Expand Down Expand Up @@ -81,3 +89,30 @@ def cleanup(self):
def disconnect(self):
super(BaseConsoleConn, self).disconnect()

def posix_shell(self):
oldtty = termios.tcgetattr(sys.stdin)
try:
tty.setraw(sys.stdin.fileno())
tty.setcbreak(sys.stdin.fileno())
self.remote_conn.settimeout(0.0)

while True:
r, w, e = select.select([self.remote_conn, sys.stdin], [], [])
if self.remote_conn in r:
try:
x = u(self.remote_conn.recv(1024))
if len(x) == 0:
sys.stdout.write("\r\n*** EOF\r\n")
break
sys.stdout.write(x)
sys.stdout.flush()
except socket.timeout:
pass
if sys.stdin in r:
x = sys.stdin.read(1)
if len(x) == 0:
break
self.remote_conn.send(x)

finally:
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)
32 changes: 32 additions & 0 deletions tests/common/connections/console_host.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from base_console_conn import CONSOLE_SSH, CONSOLE_SSH_MENU_PORTS, CONSOLE_TELNET
from telnet_console_conn import TelnetConsoleConn
from ssh_console_conn import SSHConsoleConn

ConsoleTypeMapper = {
CONSOLE_TELNET: TelnetConsoleConn,
CONSOLE_SSH: SSHConsoleConn,
CONSOLE_SSH_MENU_PORTS: SSHConsoleConn
}

def ConsoleHost(console_type,
console_host,
console_port,
sonic_username,
sonic_password,
console_username=None,
console_password=None,
timeout_s=100):
if not ConsoleTypeMapper.has_key(console_type):
raise ValueError("console type {} is not supported yet".format(console_type))
params = {
"console_host": console_host,
"console_port": console_port,
"console_type": console_type,
"sonic_username": sonic_username,
"sonic_password": sonic_password,
"console_username": console_username,
"console_password": console_password,
"timeout": timeout_s
}
return ConsoleTypeMapper[console_type](**params)

3 changes: 1 addition & 2 deletions tests/common/connections/ssh_console_conn.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from tests.common.connections.base_console_conn import CONSOLE_SSH
import time
import re
from base_console_conn import BaseConsoleConn
from base_console_conn import BaseConsoleConn, CONSOLE_SSH
from netmiko.ssh_exception import NetMikoAuthenticationException

class SSHConsoleConn(BaseConsoleConn):
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from tests.common.helpers.dut_utils import is_supervisor_node, is_frontend_node
from tests.common.cache import FactsCache

from tests.common.connections import ConsoleHost
from tests.common.connections.console_host import ConsoleHost


logger = logging.getLogger(__name__)
Expand Down

0 comments on commit c12a893

Please sign in to comment.