ifconfig
에는 두 가지 출력 모드가 있습니다. 기본값은 출력이 많이 나오는 기본값이고, 작은 숫자는 -s
입니다 (적은 값을 제공하거나 원하는 정보에서 다른 비트를 선택합니다) . 그렇다면 기본 모드에서 ifconfig를 사용하고 스크립트 (파이썬, 펄, 루비, awk, 배쉬 + sed + ...)에서 원하는 특정 정보를 체리 - 가져 오는 것은 어떻습니까? 예를 들어, 파이썬/w :
import re
import subprocess
ifc = subprocess.Popen('ifconfig', stdout=subprocess.PIPE)
res = []
for x in ifc.stdout:
if not x.strip():
print ' '.join(res)
del res[:]
elif not res:
res.append(re.match(r'\w+', x).group())
else:
mo = re.match(r'\s+inet addr:(\S+).*Mask:(\S+)', x)
if mo:
res.extend(mo.groups())
elif re.match(r'\sUP\s', x):
res.append('up')
elif re.match(r'\sDOWN\s', x):
res.append('down')
if res: print ' '.join(res)
당신은 (쉽게 내가 언급 한 다른 모든 언어로 번역하는 I 희망)가 원하는대로 출력이되어야한다.