Как исправить отсутствие возвращаемого значения из subprocess.check_output ()? - PullRequest
1 голос
/ 21 июня 2020

Мой код сканирует маршрутизатор в поисках адреса ma c с помощью подпроцесса. Но при запуске возвращает «Нет». Как я могу это исправить?

import re
import os
import subprocess

# MAC address regex
macRegex = re.compile("[0-9a-f]{2}([-:]?)[0-9a-f]{2}(\\1[0-9a-f]{2}){4}$")

cmd = "chcp 65001 && ipconfig | findstr /i \"Default Gateway\""
res = subprocess.check_output(cmd, shell=True, stderr=subprocess.DEVNULL, stdin=subprocess.DEVNULL)

def GetMacByIP():
 z = subprocess.check_output('arp -a ', shell=True, stderr=subprocess.DEVNULL, stdin=subprocess.DEVNULL)
 a = z.decode(encoding="cp866")
 f = a.find("Physical Address")
 o = a[f:].split(' ')
 for a in o:
  if macRegex.match(a):
     return a.replace('-', ':')

1 Ответ

1 голос
/ 21 июня 2020

Эта часть здесь:

def GetMacByIP():
 z = subprocess.check_output('arp -a ', shell=True, stderr=subprocess.DEVNULL, stdin=subprocess.DEVNULL)
 a = z.decode(encoding="cp866")
 f = a.find("Physical Address")
 o = a[f:].split(' ')
 for a in o:
  #print(a)
  #print(macRegex.match(a))
  if macRegex.match(a): # It's possible that this if statement never meets
     return a.replace('-', ':')

У вас есть инструкция if. Когда условие никогда не соответствует заявлению, вы не сказали python, что возвращать, и поэтому он возвращает None.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...