public inbox for development@lists.ipfire.org
 help / color / mirror / Atom feed
From: Stefan Schantl <stefan.schantl@ipfire.org>
To: development@lists.ipfire.org
Cc: Stefan Schantl <stefan.schantl@ipfire.org>
Subject: [PATCH 3/3] lldp.cgi: Show discovered peers in alphabetical order
Date: Sat, 15 Nov 2025 11:07:58 +0100	[thread overview]
Message-ID: <20251115100758.7039-3-stefan.schantl@ipfire.org> (raw)
In-Reply-To: <20251115100758.7039-1-stefan.schantl@ipfire.org>

Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
---
 html/cgi-bin/lldp.cgi | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/html/cgi-bin/lldp.cgi b/html/cgi-bin/lldp.cgi
index 194f4ec82..a1b1d4bf9 100644
--- a/html/cgi-bin/lldp.cgi
+++ b/html/cgi-bin/lldp.cgi
@@ -39,6 +39,9 @@ my @errormessages = ();
 my %settings = ();
 &General::readhash("${General::swroot}/lldp/settings", \%settings);
 
+# Hash which will contain any discovered peers.
+my %peerhash = ();
+
 # Save on main page
 if ($cgiparams{"ACTION"} eq $Lang::tr{'save'}) {
 	# Store whether enabled or not
@@ -136,6 +139,14 @@ END
 		# Fetch the interface object
 		my $interface = $json->{"lldp"}[0]->{"interface"};
 
+		# Loop through all detected peers and add their sent names as keys
+		# and their data as values to the peerhash.
+		foreach my $peer (@{ $interface}) {
+			my $name = &Header::escape($peer->{"chassis"}[0]->{"name"}[0]->{"value"});
+
+			$peerhash{$name} = $peer;
+		}
+
 		print <<END;
 			<table class='tbl'>
 				<tr>
@@ -165,16 +176,17 @@ END
 				</tr>
 END
 
-				foreach my $peer (@{ $interface }) {
-					my $intf = $peer->{"name"};
-					my $proto = $peer->{"via"};
+				# Sort the detected peers alphabetically and loop over them.
+				foreach my $peer (sort { $a cmp $b } keys %peerhash) {
+					my $intf = $peerhash{$peer}{"name"};
+					my $proto = $peerhash{$peer}{"via"};
 					my $name = "";
 					my $descr = "";
 					my $port_name = "";
 					my $vlan_id = "";
 
 					# Fetch the chassis
-					foreach my $chassis (@{ $peer->{"chassis"} }) {
+					foreach my $chassis (@{ $peerhash{$peer}{"chassis"} }) {
 						$name = &Header::escape(
 							$chassis->{"name"}[0]->{"value"}
 						);;
@@ -187,12 +199,12 @@ END
 					}
 
 					# Fetch the port
-					foreach my $port (@{ $peer->{"port"} }) {
+					foreach my $port (@{ $peerhash{$peer}{"port"} }) {
 						$port_name = $port->{"descr"}[0]->{"value"};
 					}
 
 					# Fetch the VLAN
-					foreach my $vlan (@{ $peer->{"vlan"} }) {
+					foreach my $vlan (@{ $peerhash{$peer}{"vlan"} }) {
 						$vlan_id = $vlan->{"vlan-id"};
 					}
 
@@ -226,7 +238,7 @@ END
 				}
 
 				# Show a message if there are no neighbors
-				unless (scalar @{ $interface }) {
+				unless (keys %peerhash) {
 					print <<END;
 						<tr>
 							<td colspan="6" style="text-align: center;">
-- 
2.47.3



      parent reply	other threads:[~2025-11-15 10:09 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-15 10:07 [PATCH 1/3] lldp.cgi: Add mission validation for description field Stefan Schantl
2025-11-15 10:07 ` [PATCH 2/3] lldp.cgi: Call binary for peers and do json stuff only if the service is enabled Stefan Schantl
2025-11-15 10:07 ` Stefan Schantl [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251115100758.7039-3-stefan.schantl@ipfire.org \
    --to=stefan.schantl@ipfire.org \
    --cc=development@lists.ipfire.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox