模拟paramiko.SSHClient().exec_command() [英] mock paramiko.SSHClient().exec_command()

查看:777
本文介绍了模拟paramiko.SSHClient().exec_command()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过ssh.exec_command()模拟执行某些远程命令 它将元组(stdin,stdout,stderr)作为paramiko.ChanelFile对象返回.

I need mock execution of some remote command via ssh.exec_command() It returns tuple (stdin, stdout, stderr) as paramiko.ChanelFile object.

因此,我将命令输出作为字符串(我想让exec_command()返回)以及如何使用输出字符串创建ChanelFile对象的问题.

So, I have output of command as string (which I want, exec_command() to return) and the question how to create ChanelFile object with my output string.

伪代码:

    def send_command(self, cmd)
        self.client.connect(hostname=self.host,
                            username=self.user,
                            password=self.password,
                            timeout=self.timeout)
        stdin, stdout, stderr = self.client.exec_command(cmd)

        return stdin, stdout, stderr

    if __name__ == '__main__':
        stdin, stdout, stderr = report.send_command('ls -la')
        resp = stdout.read().strip()

所以我需要创建粗壮的东西才能在其上运行read()方法.

So I need create stout to be able run read() method on it.

推荐答案

请参见 https://stackoverflow.com/a/8168742/5512755 MagicMock也可以在这里工作.

See https://stackoverflow.com/a/8168742/5512755 A MagicMock works here too.

stdout = mock.MagicMock()
stdout.read().strip.return_value = "file1\nfile2\nfile3\n"

可以解决问题.

这篇关于模拟paramiko.SSHClient().exec_command()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆