def setUp(self, mock_paramiko): self.ssh_conn = SSH(host='1.1.1.1', user='******', password='******', port=3007, timeout=30) self._ssh = mock_paramiko.invoke_shell
def setUp(self, mock_paramiko): self.ssh_conn = SSH(host="1.1.1.1", user="******", password="******", port=3007, timeout=30) self._ssh = mock_paramiko.invoke_shell
def test_tty_ssh_baud(self, mock_paramiko): self.ssh_conn = SSH(host='1.1.1.1', user='******', password='******', port=3007, timeout=30, baud=0) self.ssh_conn._tty_open() self.ssh_conn.rawwrite('<rpc>') self.ssh_conn._ssh.sendall.assert_called_with('<rpc>')
def test_tty_ssh_baud(self, mock_paramiko): self.ssh_conn = SSH( host="1.1.1.1", user="******", password="******", port=3007, timeout=30, baud=0, ) self.ssh_conn._tty_open() self.ssh_conn.rawwrite("<rpc>") self.ssh_conn._ssh.sendall.assert_called_with("<rpc>")
def _tty_login(self): tty_args = dict() tty_args['user'] = self._auth_user tty_args['passwd'] = self._auth_password tty_args['timeout'] = float(self._timeout) tty_args['attempts'] = int(self._attempts) tty_args['baud'] = self._baud tty_args['huge_tree'] = self._huge_tree if self._mode and self._mode.upper() == 'TELNET': tty_args['host'] = self._hostname tty_args['port'] = self._port tty_args['console_has_banner'] = self.console_has_banner self.console = ('telnet', self._hostname, self.port) self._tty = Telnet(**tty_args) elif self.cs_user is not None: tty_args['cs_user'] = self.cs_user tty_args['cs_passwd'] = self.cs_passwd tty_args['host'] = self._hostname tty_args['port'] = self._port tty_args['console_has_banner'] = self.console_has_banner tty_args['ssh_private_key_file'] = self._ssh_private_key_file self.console = ('ssh', self._hostname, self.port) self._tty = SSH(**tty_args) elif self._mode.upper() == 'SERIAL': tty_args['port'] = self._port self.console = ('serial', self._port) self._tty = Serial(**tty_args) else: logger.error('Mode should be either telnet or serial') raise AttributeError('Mode to be telnet/serial') self._tty.login()
def _tty_login(self): tty_args = dict() tty_args["user"] = self._auth_user tty_args["passwd"] = self._auth_password tty_args["timeout"] = float(self._timeout) tty_args["attempts"] = int(self._attempts) tty_args["baud"] = self._baud tty_args["huge_tree"] = self._huge_tree if self._mode and self._mode.upper() == "TELNET": tty_args["host"] = self._hostname tty_args["port"] = self._port tty_args["console_has_banner"] = self.console_has_banner self.console = ("telnet", self._hostname, self.port) self._tty = Telnet(**tty_args) elif self.cs_user is not None: tty_args["cs_user"] = self.cs_user tty_args["cs_passwd"] = self.cs_passwd tty_args["host"] = self._hostname tty_args["port"] = self._port tty_args["console_has_banner"] = self.console_has_banner tty_args["ssh_private_key_file"] = self._ssh_private_key_file self.console = ("ssh", self._hostname, self.port) self._tty = SSH(**tty_args) elif self._mode.upper() == "SERIAL": tty_args["port"] = self._port self.console = ("serial", self._port) self._tty = Serial(**tty_args) else: logger.error("Mode should be either telnet or serial") raise AttributeError("Mode to be telnet/serial") self._tty.login()
class TestTTYSSH(unittest.TestCase): @patch('jnpr.junos.transport.tty_ssh.paramiko') def setUp(self, mock_paramiko): self.ssh_conn = SSH(host='1.1.1.1', user='******', password='******', port=3007, timeout=30) self._ssh = mock_paramiko.invoke_shell def test_open(self): self.ssh_conn._tty_open() self.ssh_conn._ssh_pre.connect.assert_called() def test_open_exception(self): self.ssh_conn._ssh_pre.connect.side_effect = socket.error SSH.RETRY_OPEN = 1 SSH.RETRY_BACKOFF = 0.1 self.assertRaises(RuntimeError, self.ssh_conn._tty_open) # reset SSH.RETRY_OPEN = 3 SSH.RETRY_BACKOFF = 2 def test_read(self): self.ssh_conn._tty_open() self.assertRaises(ValueError, self.ssh_conn.read) def test_close(self): self.assertEqual(self.ssh_conn._tty_close(), None) @patch('jnpr.junos.transport.tty_ssh.paramiko') def test_tty_ssh_baud(self, mock_paramiko): self.ssh_conn = SSH(host='1.1.1.1', user='******', password='******', port=3007, timeout=30, baud=0) self.ssh_conn._tty_open() self.ssh_conn.rawwrite('<rpc>') self.ssh_conn._ssh.sendall.assert_called_with('<rpc>') def test_tty_ssh_rawwrite_sys_py3(self): with patch.object(sys.modules['sys'], 'version', '3.x') \ as mock_sys: self.ssh_conn._tty_open() content = MagicMock() self.ssh_conn.rawwrite(content) content.decode.assert_called_with('utf-8')