public inbox for development@lists.ipfire.org
 help / color / mirror / Atom feed
From: "Peter Müller" <peter.mueller@link38.eu>
To: development@lists.ipfire.org
Subject: [PATCH 1/2] display active logins at remote.cgi
Date: Sat, 20 Jan 2018 18:24:31 +0100	[thread overview]
Message-ID: <20180120182431.239a7317.peter.mueller@link38.eu> (raw)

[-- Attachment #1: Type: text/plain, Size: 3655 bytes --]

Display active user logins (both local and remote) at
the remote.cgi page in the WebUI. This might be useful
for debugging broken SSH sessions or simply checking that
nobody is currently logged in. :-)

Signed-off-by: Peter Müller <peter.mueller(a)link38.eu>
---
 html/cgi-bin/remote.cgi | 91 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 91 insertions(+)

diff --git a/html/cgi-bin/remote.cgi b/html/cgi-bin/remote.cgi
index 10a3e87cc..5acce4e99 100644
--- a/html/cgi-bin/remote.cgi
+++ b/html/cgi-bin/remote.cgi
@@ -25,15 +25,23 @@ use strict;
 #use warnings;
 #use CGI::Carp 'fatalsToBrowser';
 
+use IO::Socket;
+
 require '/var/ipfire/general-functions.pl';
+require "${General::swroot}/geoip-functions.pl";
 require "${General::swroot}/lang.pl";
 require "${General::swroot}/header.pl";
 
+my %color = ();
+my %mainsettings = ();
 my %remotesettings=();
 my %checked=();
 my $errormessage='';
 my $counter = 0;
 
+&General::readhash("${General::swroot}/main/settings", \%mainsettings);
+&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
+
 &Header::showhttpheaders();
 
 $remotesettings{'ENABLE_SSH'} = 'off';
@@ -187,6 +195,38 @@ print "</table>\n";
 
 &Header::closebox();
 
+&Header::openbox('100%', 'center', $Lang::tr{'ssh active sesstions'});
+
+print <<END;
+		<table class="tbl" width='66%'>
+			<thead>
+				<tr>
+					<th align="center">
+						<strong>$Lang::tr{'ssh username'}</strong>
+					</th>
+					<th align="center">
+						<strong>$Lang::tr{'ssh login time'}</strong>
+					</th>
+					<th align="center">
+						<strong>$Lang::tr{'ip address'}</strong>
+					</th>
+					<th align="center">
+						<strong>$Lang::tr{'country'}</strong>
+					</th>
+					<th align="center">
+						<strong>$Lang::tr{'rdns'}</strong>
+					</th>
+				</tr>
+			</thead>
+			<tbody>
+END
+
+&printactivelogins();
+
+print "</tbody>\n</table>\n";
+
+&Header::closebox();
+
 &Header::closebigbox();
 
 &Header::closepage();
@@ -205,3 +245,54 @@ sub viewkey
     print "<tr><td>$key ($name)</td><td><code>$fingerprint</code></td><td align='center'>$keysize</td></tr>\n";
   }
 }
+
+sub printactivelogins()
+{
+	# print active SSH logins (grep outpout of "who -s")
+	my $command = "who -s";
+	my @output = `$command`;
+	chomp(@output);
+
+	my $id = 0;
+
+	if ( scalar(@output) == 0 )
+	{
+		# no logins appeared
+		my $table_colour = ($id++ % 2) ? $color{'color22'} : $color{'color20'};
+		print "<tr bgcolor='$table_colour'><td colspan='5'>$Lang::tr{'ssh no active logins'}</td></tr>\n";
+	} else {
+		# list active logins...
+
+		foreach my $line (@output)
+		{
+			my @arry = split(/\ +/, $line);
+
+			my $username = @arry[0];
+			my $logintime = join(' ', @arry[2..4]);
+			my $remoteip = @arry[5];
+			$remoteip =~ s/[()]//g;
+
+			# display more information about that IP adress...
+			my $ccode = &GeoIP::lookup($remoteip);
+			my $flag_icon = &GeoIP::get_flag_icon($ccode);
+
+			# get rDNS...
+			my $iaddr = inet_aton($remoteip);
+			my $rdns = gethostbyaddr($iaddr, AF_INET);
+			if (!$rdns) { $rdns = $Lang::tr{'lookup failed'}; };
+
+			my $table_colour = ($id++ % 2) ? $color{'color22'} : $color{'color20'};
+
+			print <<END;
+			<tr bgcolor='$table_colour'>
+				<td>$username</td>
+				<td>$logintime</td>
+				<td>$remoteip</td>
+				<td align='center'><a href='country.cgi#$ccode'><img src='$flag_icon' border='0' alt='$ccode' title='$ccode' /></a></td>
+				<td>$rdns</td>
+			</tr>
+END
+;
+		}
+	}
+}
-- 
2.13.6



                 reply	other threads:[~2018-01-20 17:24 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20180120182431.239a7317.peter.mueller@link38.eu \
    --to=peter.mueller@link38.eu \
    --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