解析 Linux 命令的输出

问题描述 投票:0回答:1

我正在尝试匹配一个特定行以捕获远程主机上桥接控制命令的输出中的地址。我不确定这是否应该使用标准输出和读取线来完成。我是菜鸟...

import paramiko
import re

hostname = "192.168.88.79"
port = 22
username = "admin"
password = "password"


def lan0_mac():
    s = paramiko.SSHClient()
    s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    s.connect(hostname, port, username, password)
    command = "brctl showmacs br0"
    stdin, stdout, stderr = s.exec_command(command)
    for line in stdout.readlines():
        mac = re.search(r'1\s{5}(([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2}))\s{7}no', line).group(1)
        print (mac)
    s.close()


lan0_mac()

我得到:

AttributeError:“NoneType”对象没有属性“group”

python regex paramiko
1个回答
0
投票

获取代码并修改它,假设它获得多行输出:

import paramiko
import re

hostname = "192.168.88.79"
port = 22
username = "admin"
password = "password"


def lan0_mac():
    s = paramiko.SSHClient()
    s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    s.connect(hostname, port, username, password)
    command = "brctl showmacs br0"
    stdin, stdout, stderr = s.exec_command(command)
    for line in stdout.readlines():
        test_result = re.search(r'1\s{5}(([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2}))\s{7}no', line)
        if test_result:
            mac = test_result.group(1)
            print (mac)
    s.close()


lan0_mac()
© www.soinside.com 2019 - 2024. All rights reserved.