在机器人框架中导入python netmiko库

时间:2017-10-22 19:57:32

标签: robotframework

我是机器人框架和netmiko的初学者。 我试图在python中使用netmiko打印出whoami。

import netmiko

mydevice = {
'device_type': 'linux',
'ip': '127.0.0.1',
'username': 'gns3',
'password': 'gns3',
'verbose':True
}

conn = netmiko.ConnectHandler(**mydevice)
cmd = 'whoami\n'
output = conn.send_command(cmd)

def display_cmd():
    print(conn.find_prompt() + cmd + output + '\n')

if __name__ == '__main__':
    print (display_cmd())

看起来很有效。

gns3@gns3vm:~/project$ python netmiko_test.py
SSH connection established to 127.0.0.1:22
Interactive SSH session established
gns3@gns3vm:~$whoami
gns3

None

然后我试着在这里制作快速简单的机器人文件。

*** Settings ***
Library         netmiko_test.py

*** Test Case ***
whoami      display whoami

*** Keywords ***
display whoami
    ${result}  display cmd
    Log To Console     ${result}

我预计相同的python输出将在log.html中包含所有PASS,但会出现以下错误。

gns3@gns3vm:~/project$ robot rf_test.robot
[ ERROR ] Error in file '/home/gns3/project/rf_test.robot': Importing test library '/home/gns3/project/netmiko_test.py' failed: SSHException: No existing session
Traceback (most recent call last):
  File "/home/gns3/project/netmiko_test.py", line 11, in <module>
    conn = netmiko.ConnectHandler(**mydevice)
  File "/usr/local/lib/python2.7/dist-packages/netmiko/ssh_dispatcher.py", line 131, in ConnectHandler
    return ConnectionClass(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/netmiko/base_connection.py", line 151, in __init__
    self.establish_connection()
  File "/usr/local/lib/python2.7/dist-packages/netmiko/base_connection.py", line 532, in establish_connection
    self.remote_conn_pre.connect(**ssh_connect_params)
  File "/usr/local/lib/python2.7/dist-packages/paramiko/client.py", line 391, in connect
    server_key = t.get_remote_server_key()
  File "/usr/local/lib/python2.7/dist-packages/paramiko/transport.py", line 718, in get_remote_server_key
    raise SSHException('No existing session')
PYTHONPATH:
  /usr/local/bin
  /usr/lib/python2.7
  /usr/lib/python2.7/plat-x86_64-linux-gnu
  /usr/lib/python2.7/lib-tk
  /usr/lib/python2.7/lib-old
  /usr/lib/python2.7/lib-dynload
  /usr/local/lib/python2.7/dist-packages
  /usr/lib/python2.7/dist-packages
==============================================================================
Rf Test
==============================================================================
whoami                                                                | FAIL |
No keyword with name 'display cmd' found.
------------------------------------------------------------------------------
Rf Test                                                               | FAIL |
1 critical test, 0 passed, 1 failed
1 test total, 0 passed, 1 failed
==============================================================================
Output:  /home/gns3/project/output.xml
Log:     /home/gns3/project/log.html
Report:  /home/gns3/project/report.html

你能告诉我哪里出错吗?

谢谢。

2 个答案:

答案 0 :(得分:0)

如果您发布的文件与您运行的文件相同,那么我猜您没有使用正确的功能打印到控制台。也就是说,因为这是一个相当小的例子,我为你格式化了一个库。因为我没有选择运行Netmiko我无法正常测试。

import netmiko

class NetmikoLibrary(object):

    ROBOT_LIBRARY_VERSION = 1.0

    def __init__(self):
        pass

    def display_cmd(self, cmd = 'whoami\n'):
        '''
            This is my Netmiko keyword to execute a command.
        '''
        mydevice = {
        'device_type': 'linux',
        'ip': '127.0.0.1',
        'username': 'gns3',
        'password': 'gns3',
        'verbose':True
        }

        conn = netmiko.ConnectHandler(**mydevice)

        output = conn.send_command(cmd)
        return output

这意味着你可以加载你的库略有不同:Library NetmikoLibrary

答案 1 :(得分:0)

谢谢Kootstra。我不知道所有变量都必须在执行命令(display_cmd)中,但认为即使位于display_cmd之外也可以寻址所有内容。

现在可以在重新定位display_cmd内的变量后使用。

import netmiko

def display_cmd():
    mydevice = {
    'device_type': 'linux',
    'ip': '127.0.0.1',
    'username': 'gns3',
    'password': 'gns3',
    'verbose':True
    }

    conn = netmiko.ConnectHandler(**mydevice)
    cmd = 'whoami\n'
    output = conn.send_command(cmd)

    return (conn.find_prompt() + cmd + output + '\n')

if __name__ == '__main__':
    print (display_cmd())

这样的结果全部通过了。

gns3@gns3vm:~/project$ robot rf_test.robot
==============================================================================
Rf Test
==============================================================================
whoami                                                                gns3@gns3vm:~$whoami
gns3

whoami                                                                | PASS |
------------------------------------------------------------------------------
Rf Test                                                               | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Output:  /home/gns3/project/output.xml
Log:     /home/gns3/project/log.html
Report:  /home/gns3/project/report.html

谢谢。