mirror of
https://github.com/lucaspalomodevelop/core.git
synced 2026-03-20 03:16:12 +00:00
python3: replace subprocess.call in src/opnsense/scripts/interfaces/* for https://github.com/opnsense/core/issues/3574
This commit is contained in:
parent
d241a64138
commit
2f69282b28
@ -28,10 +28,8 @@
|
||||
--------------------------------------------------------------------------------------
|
||||
list arp table
|
||||
"""
|
||||
import tempfile
|
||||
import subprocess
|
||||
import os
|
||||
import os.path
|
||||
import sys
|
||||
import ujson
|
||||
import netaddr
|
||||
@ -51,27 +49,24 @@ if __name__ == '__main__':
|
||||
dhcp_leases[dhcp_ipv4_address] = {'hostname': lease.split('client-hostname')[1].strip()[1:-2]}
|
||||
|
||||
# parse arp output
|
||||
with tempfile.NamedTemporaryFile() as output_stream:
|
||||
subprocess.call(['/usr/sbin/arp', '-an'], stdout=output_stream, stderr=open(os.devnull, 'wb'))
|
||||
output_stream.seek(0)
|
||||
for line in output_stream:
|
||||
line = line.decode()
|
||||
line_parts = line.split()
|
||||
if len(line_parts) > 3 and line_parts[3] != '(incomplete)':
|
||||
record = {'mac': line_parts[3],
|
||||
'ip': line_parts[1][1:-1],
|
||||
'intf': line_parts[5],
|
||||
'manufacturer': '',
|
||||
'hostname': ''
|
||||
}
|
||||
manufacturer_mac = netaddr.EUI(record['mac'])
|
||||
try:
|
||||
record['manufacturer'] = manufacturer_mac.oui.registration().org
|
||||
except netaddr.NotRegisteredError:
|
||||
pass
|
||||
if record['ip'] in dhcp_leases:
|
||||
record['hostname'] = dhcp_leases[record['ip']]['hostname']
|
||||
result.append(record)
|
||||
sp = subprocess.run(['/usr/sbin/arp', '-an'], capture_output=True, text=True)
|
||||
for line in sp.stdout.split('\n'):
|
||||
line_parts = line.split()
|
||||
if len(line_parts) > 3 and line_parts[3] != '(incomplete)':
|
||||
record = {'mac': line_parts[3],
|
||||
'ip': line_parts[1][1:-1],
|
||||
'intf': line_parts[5],
|
||||
'manufacturer': '',
|
||||
'hostname': ''
|
||||
}
|
||||
manufacturer_mac = netaddr.EUI(record['mac'])
|
||||
try:
|
||||
record['manufacturer'] = manufacturer_mac.oui.registration().org
|
||||
except netaddr.NotRegisteredError:
|
||||
pass
|
||||
if record['ip'] in dhcp_leases:
|
||||
record['hostname'] = dhcp_leases[record['ip']]['hostname']
|
||||
result.append(record)
|
||||
|
||||
# handle command line argument (type selection)
|
||||
if len(sys.argv) > 1 and sys.argv[1] == 'json':
|
||||
|
||||
@ -28,36 +28,30 @@
|
||||
--------------------------------------------------------------------------------------
|
||||
list ndp table
|
||||
"""
|
||||
import tempfile
|
||||
import subprocess
|
||||
import os
|
||||
import os.path
|
||||
import sys
|
||||
import ujson
|
||||
import netaddr
|
||||
|
||||
if __name__ == '__main__':
|
||||
result = []
|
||||
|
||||
# parse ndp output
|
||||
with tempfile.NamedTemporaryFile() as output_stream:
|
||||
subprocess.call(['/usr/sbin/ndp', '-an'], stdout=output_stream, stderr=open(os.devnull, 'wb'))
|
||||
output_stream.seek(0)
|
||||
data = output_stream.read().decode().strip()
|
||||
for line in data.split('\n')[1:]:
|
||||
line_parts = line.split()
|
||||
if len(line_parts) > 3 and line_parts[1] != '(incomplete)':
|
||||
record = {'mac': line_parts[1],
|
||||
'ip': line_parts[0],
|
||||
'intf': line_parts[2],
|
||||
'manufacturer': ''
|
||||
}
|
||||
manufacturer_mac = netaddr.EUI(record['mac'])
|
||||
try:
|
||||
record['manufacturer'] = manufacturer_mac.oui.registration().org
|
||||
except netaddr.NotRegisteredError:
|
||||
pass
|
||||
result.append(record)
|
||||
sp = subprocess.run(['/usr/sbin/ndp', '-an'], capture_output=True, text=True)
|
||||
for line in sp.stdout.split('\n')[1:]:
|
||||
line_parts = line.split()
|
||||
if len(line_parts) > 3 and line_parts[1] != '(incomplete)':
|
||||
record = {'mac': line_parts[1],
|
||||
'ip': line_parts[0],
|
||||
'intf': line_parts[2],
|
||||
'manufacturer': ''
|
||||
}
|
||||
manufacturer_mac = netaddr.EUI(record['mac'])
|
||||
try:
|
||||
record['manufacturer'] = manufacturer_mac.oui.registration().org
|
||||
except netaddr.NotRegisteredError:
|
||||
pass
|
||||
result.append(record)
|
||||
|
||||
# handle command line argument (type selection)
|
||||
if len(sys.argv) > 1 and sys.argv[1] == 'json':
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user