|
|
6cb3e9 |
From 7279bdb8a281de67be61f745f24344d0574e32cf Mon Sep 17 00:00:00 2001
|
|
|
6cb3e9 |
From: Lumir Balhar <lbalhar@redhat.com>
|
|
|
6cb3e9 |
Date: Tue, 4 Dec 2018 11:58:48 +0100
|
|
|
6cb3e9 |
Subject: [PATCH 1/2] Allow pifconfig script to show multiple IPv4 addresses
|
|
|
6cb3e9 |
|
|
|
6cb3e9 |
---
|
|
|
6cb3e9 |
scripts/pifconfig | 34 ++++++++++++++++++----------------
|
|
|
6cb3e9 |
tests/test_ethtool.py | 15 +++++++++------
|
|
|
6cb3e9 |
2 files changed, 27 insertions(+), 22 deletions(-)
|
|
|
6cb3e9 |
mode change 100644 => 100755 scripts/pifconfig
|
|
|
6cb3e9 |
|
|
|
6cb3e9 |
diff --git a/scripts/pifconfig b/scripts/pifconfig
|
|
|
6cb3e9 |
old mode 100644
|
|
|
6cb3e9 |
new mode 100755
|
|
|
6cb3e9 |
index f2b0707..c4e726d
|
|
|
6cb3e9 |
--- a/scripts/pifconfig
|
|
|
6cb3e9 |
+++ b/scripts/pifconfig
|
|
|
6cb3e9 |
@@ -17,10 +17,17 @@
|
|
|
6cb3e9 |
from __future__ import unicode_literals, print_function
|
|
|
6cb3e9 |
|
|
|
6cb3e9 |
import ethtool
|
|
|
6cb3e9 |
+import socket
|
|
|
6cb3e9 |
+import struct
|
|
|
6cb3e9 |
import sys
|
|
|
6cb3e9 |
from optparse import OptionParser
|
|
|
6cb3e9 |
|
|
|
6cb3e9 |
|
|
|
6cb3e9 |
+def bits2netmask(bits):
|
|
|
6cb3e9 |
+ mask = (1 << 32) - (1 << 32 >> bits)
|
|
|
6cb3e9 |
+ return socket.inet_ntoa(struct.pack(">L", mask))
|
|
|
6cb3e9 |
+
|
|
|
6cb3e9 |
+
|
|
|
6cb3e9 |
def flags2str(flags):
|
|
|
6cb3e9 |
string = ''
|
|
|
6cb3e9 |
if flags & ethtool.IFF_UP:
|
|
|
6cb3e9 |
@@ -60,24 +67,19 @@ def flags2str(flags):
|
|
|
6cb3e9 |
|
|
|
6cb3e9 |
|
|
|
6cb3e9 |
def show_config(device):
|
|
|
6cb3e9 |
- try:
|
|
|
6cb3e9 |
- ipaddr = ethtool.get_ipaddr(device)
|
|
|
6cb3e9 |
- netmask = ethtool.get_netmask(device)
|
|
|
6cb3e9 |
- broadcast = ethtool.get_broadcast(device)
|
|
|
6cb3e9 |
- except (IOError, OSError):
|
|
|
6cb3e9 |
- ipaddr, netmask, broadcast = None, None, None
|
|
|
6cb3e9 |
flags = ethtool.get_flags(device)
|
|
|
6cb3e9 |
- print('%s' % device)
|
|
|
6cb3e9 |
- if not (flags & ethtool.IFF_LOOPBACK):
|
|
|
6cb3e9 |
- print('\tHWaddr %s' % ethtool.get_hwaddr(device))
|
|
|
6cb3e9 |
- if ipaddr is not None:
|
|
|
6cb3e9 |
- print('\tinet addr:%s' % ipaddr)
|
|
|
6cb3e9 |
- if broadcast is not None and \
|
|
|
6cb3e9 |
- not (flags & (ethtool.IFF_LOOPBACK | ethtool.IFF_POINTOPOINT)):
|
|
|
6cb3e9 |
- print('\tBcast:%s' % broadcast)
|
|
|
6cb3e9 |
- if netmask is not None:
|
|
|
6cb3e9 |
- print('\tMask:%s' % netmask)
|
|
|
6cb3e9 |
+
|
|
|
6cb3e9 |
for info in ethtool.get_interfaces_info(device):
|
|
|
6cb3e9 |
+ print(device)
|
|
|
6cb3e9 |
+ if not (flags & ethtool.IFF_LOOPBACK):
|
|
|
6cb3e9 |
+ print('\tHWaddr %s' % ethtool.get_hwaddr(device))
|
|
|
6cb3e9 |
+
|
|
|
6cb3e9 |
+ for addr in info.get_ipv4_addresses():
|
|
|
6cb3e9 |
+ print('\tinet addr:%s' % addr.address, end=" ")
|
|
|
6cb3e9 |
+ if not (flags & (ethtool.IFF_LOOPBACK | ethtool.IFF_POINTOPOINT)):
|
|
|
6cb3e9 |
+ print('Bcast:%s' % addr.broadcast, end=" ")
|
|
|
6cb3e9 |
+ print('Mask:%s' % bits2netmask(addr.netmask))
|
|
|
6cb3e9 |
+
|
|
|
6cb3e9 |
for addr in info.get_ipv6_addresses():
|
|
|
6cb3e9 |
print('\tinet6 addr: %s/%s Scope: %s'
|
|
|
6cb3e9 |
% (addr.address,
|
|
|
6cb3e9 |
diff --git a/tests/test_ethtool.py b/tests/test_ethtool.py
|
|
|
6cb3e9 |
index 2d8bc0e..6162cd3 100755
|
|
|
6cb3e9 |
--- a/tests/test_ethtool.py
|
|
|
6cb3e9 |
+++ b/tests/test_ethtool.py
|
|
|
6cb3e9 |
@@ -175,17 +175,20 @@ class EthtoolTests(unittest.TestCase):
|
|
|
6cb3e9 |
scraped = None
|
|
|
6cb3e9 |
|
|
|
6cb3e9 |
self.assertIsStringOrNone(ei.ipv4_address)
|
|
|
6cb3e9 |
- if scraped:
|
|
|
6cb3e9 |
- self.assertEqual(ei.ipv4_address, scraped.inet)
|
|
|
6cb3e9 |
+ if scraped and scraped.inet:
|
|
|
6cb3e9 |
+ addresses = [ip.address for ip in ei.get_ipv4_addresses()]
|
|
|
6cb3e9 |
+ self.assertTrue(scraped.inet in addresses)
|
|
|
6cb3e9 |
|
|
|
6cb3e9 |
self.assertIsStringOrNone(ei.ipv4_broadcast)
|
|
|
6cb3e9 |
- if scraped and scraped.broadcast:
|
|
|
6cb3e9 |
+ if scraped and scraped.broadcast not in (None, '0.0.0.0'):
|
|
|
6cb3e9 |
# Broadcast is optional
|
|
|
6cb3e9 |
- self.assertEqual(ei.ipv4_broadcast, scraped.broadcast)
|
|
|
6cb3e9 |
+ broadcasts = [ip.broadcast for ip in ei.get_ipv4_addresses()]
|
|
|
6cb3e9 |
+ self.assertTrue(scraped.broadcast in broadcasts)
|
|
|
6cb3e9 |
|
|
|
6cb3e9 |
self.assertIsInt(ei.ipv4_netmask)
|
|
|
6cb3e9 |
- if scraped:
|
|
|
6cb3e9 |
- self.assertEqual(ei.ipv4_netmask, scraped.get_netmask_bits())
|
|
|
6cb3e9 |
+ if scraped and scraped.netmask:
|
|
|
6cb3e9 |
+ netmasks = [ip.netmask for ip in ei.get_ipv4_addresses()]
|
|
|
6cb3e9 |
+ self.assertTrue(scraped.get_netmask_bits(), netmasks)
|
|
|
6cb3e9 |
|
|
|
6cb3e9 |
self.assertIsStringOrNone(ei.mac_address)
|
|
|
6cb3e9 |
if scraped and scraped.hwaddr and scraped.hwtitle.lower() != 'unspec':
|
|
|
6cb3e9 |
--
|
|
|
6cb3e9 |
2.19.2
|
|
|
6cb3e9 |
|