解析命令输出并将其存储在dicitonary中

时间:2017-06-22 21:17:30

标签: python python-3.x python-3.5 python-3.6

我创建了一个类来执行命令并返回命令的整个输出。我喜欢将输出存储在字典中,如下所示。

dict = {'ID': '*1', 'Type': 'udp', 'Destination': 'x.x.x.x', 'stats': 'RTT=1' , 'Return code' = 'OK', 'Last Run' = '0 seconds ago' }

命令输出:

IPSLAs Latest Operation Summary
Codes: * active, ^ inactive, ~ pending

    ID           Type        Destination       Stats       Return      Last
                                               (ms)        Code        Run
    -----------------------------------------------------------------------
    *1           udp       x.x.x.x            RTT=1       OK          0 seconds ago


from test.ssh import Ssh

    class  crypto:
        def __init__(self, username, ip, password ,router):
           self.user_name = username
           self.ip_address =  ip
           self.pass_word = password
           self.machine_type = router

        def sla_output(self, interface):
           command = 'show sla summary'
           router_ssh = Ssh(self.ip_address)
           result = router_ssh.cmd(command)
           return result

测试脚本:

from test.networks.router_parser import *

routerobj = crypto('user', 'ip', 'password', 'cisco')
status = routerobj.sla_output()
print (status)

0 个答案:

没有答案