當前位置: 首頁>>代碼示例>>Python>>正文


Python ParamikoSSHClient.run方法代碼示例

本文整理匯總了Python中st2common.runners.paramiko_ssh.ParamikoSSHClient.run方法的典型用法代碼示例。如果您正苦於以下問題:Python ParamikoSSHClient.run方法的具體用法?Python ParamikoSSHClient.run怎麽用?Python ParamikoSSHClient.run使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在st2common.runners.paramiko_ssh.ParamikoSSHClient的用法示例。


在下文中一共展示了ParamikoSSHClient.run方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_sftp_connection_is_only_established_if_required

# 需要導入模塊: from st2common.runners.paramiko_ssh import ParamikoSSHClient [as 別名]
# 或者: from st2common.runners.paramiko_ssh.ParamikoSSHClient import run [as 別名]
    def test_sftp_connection_is_only_established_if_required(self):
        # Verify that SFTP connection is lazily established only if and when needed.
        conn_params = {'hostname': 'dummy.host.org',
                       'username': 'ubuntu'}

        # Verify sftp connection and client hasn't been established yet
        client = ParamikoSSHClient(**conn_params)
        client.connect()

        self.assertTrue(client.sftp_client is None)

        # run method doesn't require sftp access so it shouldn't establish connection
        client = ParamikoSSHClient(**conn_params)
        client.connect()
        client.run(cmd='whoami')

        self.assertTrue(client.sftp_client is None)

        # Methods bellow require SFTP access so they should cause SFTP connection to be established
        # put
        client = ParamikoSSHClient(**conn_params)
        client.connect()
        path = '/root/random_script.sh'
        client.put(path, path, mirror_local_mode=False)

        self.assertTrue(client.sftp_client is not None)

        # exists
        client = ParamikoSSHClient(**conn_params)
        client.connect()
        client.exists('/root/somepath.txt')

        self.assertTrue(client.sftp_client is not None)

        # mkdir
        client = ParamikoSSHClient(**conn_params)
        client.connect()
        client.mkdir('/root/somedirfoo')

        self.assertTrue(client.sftp_client is not None)

        # Verify close doesn't throw if SFTP connection is not established
        client = ParamikoSSHClient(**conn_params)
        client.connect()

        self.assertTrue(client.sftp_client is None)
        client.close()

        # Verify SFTP connection is closed if it's opened
        client = ParamikoSSHClient(**conn_params)
        client.connect()
        client.mkdir('/root/somedirfoo')

        self.assertTrue(client.sftp_client is not None)
        client.close()

        self.assertEqual(client.sftp_client.close.call_count, 1)
開發者ID:LindsayHill,項目名稱:st2,代碼行數:59,代碼來源:test_paramiko_ssh.py

示例2: test_handle_stdout_and_stderr_line_funcs

# 需要導入模塊: from st2common.runners.paramiko_ssh import ParamikoSSHClient [as 別名]
# 或者: from st2common.runners.paramiko_ssh.ParamikoSSHClient import run [as 別名]
    def test_handle_stdout_and_stderr_line_funcs(self):
        mock_handle_stdout_line_func = mock.Mock()
        mock_handle_stderr_line_func = mock.Mock()

        conn_params = {
            'hostname': 'dummy.host.org',
            'username': 'ubuntu',
            'password': 'ubuntu',
            'handle_stdout_line_func': mock_handle_stdout_line_func,
            'handle_stderr_line_func': mock_handle_stderr_line_func
        }
        client = ParamikoSSHClient(**conn_params)
        client.connect()

        mock_get_transport = mock.Mock()
        mock_chan = mock.Mock()

        client.client.get_transport = mock.Mock()
        client.client.get_transport.return_value = mock_get_transport
        mock_get_transport.open_session.return_value = mock_chan

        def mock_recv_ready_factory(chan):
            chan.recv_counter = 0

            def mock_recv_ready():
                chan.recv_counter += 1
                if chan.recv_counter < 2:
                    return True

                return False
            return mock_recv_ready

        def mock_recv_stderr_ready_factory(chan):
            chan.recv_stderr_counter = 0

            def mock_recv_stderr_ready():
                chan.recv_stderr_counter += 1
                if chan.recv_stderr_counter < 2:
                    return True

                return False
            return mock_recv_stderr_ready

        mock_chan.recv_ready = mock_recv_ready_factory(mock_chan)
        mock_chan.recv_stderr_ready = mock_recv_stderr_ready_factory(mock_chan)
        mock_chan.recv.return_value = 'stdout 1\nstdout 2\nstdout 3'
        mock_chan.recv_stderr.return_value = 'stderr 1\nstderr 2\nstderr 3'

        # call_line_handler_func is False so handler functions shouldn't be called
        client.run(cmd='echo "test"', call_line_handler_func=False)

        self.assertEqual(mock_handle_stdout_line_func.call_count, 0)
        self.assertEqual(mock_handle_stderr_line_func.call_count, 0)

        # Reset counters
        mock_chan.recv_counter = 0
        mock_chan.recv_stderr_counter = 0

        # call_line_handler_func is True so handler functions should be called for each line
        client.run(cmd='echo "test"', call_line_handler_func=True)

        self.assertEqual(mock_handle_stdout_line_func.call_count, 3)
        self.assertEqual(mock_handle_stdout_line_func.call_args_list[0][1]['line'], 'stdout 1\n')
        self.assertEqual(mock_handle_stdout_line_func.call_args_list[1][1]['line'], 'stdout 2\n')
        self.assertEqual(mock_handle_stdout_line_func.call_args_list[2][1]['line'], 'stdout 3\n')
        self.assertEqual(mock_handle_stderr_line_func.call_count, 3)
        self.assertEqual(mock_handle_stdout_line_func.call_args_list[0][1]['line'], 'stdout 1\n')
        self.assertEqual(mock_handle_stdout_line_func.call_args_list[1][1]['line'], 'stdout 2\n')
        self.assertEqual(mock_handle_stdout_line_func.call_args_list[2][1]['line'], 'stdout 3\n')
開發者ID:lyandut,項目名稱:st2,代碼行數:71,代碼來源:test_paramiko_ssh.py


注:本文中的st2common.runners.paramiko_ssh.ParamikoSSHClient.run方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。