Want to hide cgi rendered html text for hostname when username text column is empty

强颜欢笑 提交于 2019-12-12 02:14:06

问题


I have the current perl code in a .cgi file that we run on our server. This allows us to know which employees are using specific machines, by displaying: hostname - username

However, I would like to be able to hide the host names that have no user present to shorten the rendered text, but I am unfamiliar with doing this.

If possible, I would also like to add logic to display a message when there are no staff logged in, like 'No staff on shift'.

Also, these aren't the real user/passwords, just fyi.

   #! /usr/bin/perl
$ENV{'ORACLE_HOME'} ="/usr/lib/oracle/11.2/client64";
use DBI;
my $date=`date`;
my $las='bziegle jpietrza hpietrza lpietrza';
$login="user";
$password="pass";
my $dbh = DBI->connect("DBI:Oracle:icsprod",$login,$password);
die "Unable to connect: $DBI::errstr\n" unless (defined $dbh);
my $sql = qq{ 
SELECT  hosts.currentuser, TO_CHAR(hosts.lastlogin, 'HH:MM:SS MM/DD/YYYY'),
        hosts.host_name
FROM    infadmin.inv_hosts hosts
WHERE   (hosts.host_name = '1408bcc204ap1')
OR      (hosts.host_name = '1408bres201p7')
OR      (hosts.host_name = '1408brngb281p1')
OR      (hosts.host_name = '1408brngb281p2')
OR      (hosts.host_name = '1408hamp3144p21')
OR      (hosts.host_name = '1408hiksg951m1')
OR      (hosts.host_name = '1408hiksg951p1')
OR      (hosts.host_name = '1408hiksg951p2')
OR      (hosts.host_name = '1408hiksg951p3')
OR      (hosts.host_name = '1408mathb10p1')
OR      (hosts.host_name = '1408mcutc216p1')
OR      (hosts.host_name = '1408mrdh146sp7')
OR      (hosts.host_name = '1408mthw116p1')
OR      (hosts.host_name = '1408phys22p5')
OR      (hosts.host_name = '1408rhph316p11')
OR      (hosts.host_name = '1408scg046p2')
OR      (hosts.host_name = '1408scg073p2')
OR      (hosts.host_name = '1408stew102p1')
OR      (hosts.host_name = '1408wthr114p2')
ORDER BY hosts.host_name
};
my $sth = $dbh->prepare($sql);
$sth->execute();
my($currentuser, $lastlogin, $host_name);
$sth->bind_columns(undef, \$currentuser, \$lastlogin, \$host_name);
print "Content-type: text/html\n\n";
print "<html>\n";
print "<meta http-equiv=refresh content=300>\n";
print "<meta http-equiv='pragma' content='no-cache'>\n";
print "<style type=\"text/css\">\n";
print "body { font-family: \"Arial\", sans-serif; font-size: small; color: black }\n";
print "</style>\n";
print "<head>\n";
print "<title>\n";
print "LA station usage.\n";
print "</title>\n";
print "</head>\n";
print "<body>\n";
print "<table>\n";
print "<tr><td colspan=5>$date</td></tr>";
print "<tr><td><u>LA Station</u></td><td><u>Login</u></td></tr>\n";
while($sth->fetch()) {
        $lastlogin=~s/ .*$//;
        $host_name=~s/1408//;
        foreach(@las)   {
                if ($currentuser eq "$_") {
                        $lacolor = "black";
                        last;
                } else {
                        $lacolor = "red";
                }
        }
        print "<tr>";
        print "<td>$host_name</td>";
        print "<td><font color=\"$lacolor\">$currentuser</font></td><td>&nbsp;</td>";
}
$sth->finish();
my $sql = qq{ 
SELECT  hosts.currentuser, TO_CHAR(hosts.lastlogin, 'HH:MM:SS MM/DD/YYYY'),
        hosts.host_name
FROM    infadmin.inv_hosts hosts
WHERE   (hosts.host_name = '1408bres201p4')
OR      (hosts.host_name = '1408hamp3144p31')
OR      (hosts.host_name = '1408hamp3144p32')
OR      (hosts.host_name = '1408hamp3144p33')
OR      (hosts.host_name = '1408mathb10p2')
OR      (hosts.host_name = '1408scg046p3')
OR      (hosts.host_name = '1408scg073p3')
OR      (hosts.host_name = '1408sc179p4')
OR      (hosts.host_name = '1408sc179p5')
OR      (hosts.host_name = '1408brngb278p1')
OR      (hosts.host_name = '1408brngb278p2')
OR      (hosts.host_name = '1408mcutc216p2')
OR      (hosts.host_name = '1408mrdh146sp6')
OR      (hosts.host_name = '1408phys22p6')
OR      (hosts.host_name = '1408rhph316p1')
OR      (hosts.host_name = '1408rhph316p10')
OR      (hosts.host_name = '1408rhph316p2')
OR      (hosts.host_name = '1408wthr114p3')
OR      (hosts.host_name = '1408scg070p1')
OR      (hosts.host_name = '1408scg070p2')
OR      (hosts.host_name = '1408mathb10m1')

ORDER BY hosts.host_name
};
my $sth = $dbh->prepare($sql);
$sth->execute();
my($currentuser, $lastlogin, $host_name);
$sth->bind_columns(undef, \$currentuser, \$lastlogin, \$host_name);
print "<tr></tr><tr><td colspan=3><u>Alternate LA Stations</u></td></tr>\n";
while($sth->fetch()) {
        $lastlogin=~s/ .*$//;
        $host_name=~s/1408//;
        foreach(@las)   {
                if ($currentuser eq "$_") {
                        $lacolor = "black";
                        last;
                } else {
                        $lacolor = "red";
                }
        }
        print "<tr>";
        print "<td>$host_name</td>";
        print "<td><font color=\"$lacolor\">$currentuser</font></td><td>&nbsp;</td>";
}
print "</table>";
print "</body>\n";
print "</html>\n";
$sth->finish();
$
b
->disconnect();

回答1:


It's not really clear how your database is laid out, but I think that if no-one is logged in on a host then the $currentuser variable will be undefined. So within the main loop that creates the table, you could just skip an iteration if that's the case. Something like this:

while($sth->fetch()) {
    next unless defined $currentuser;

    # Rest of your existing code
}

But all in all, this code needs a lot of work. I'm sure it currently does what you want, but it's an unmaintainable mess which is very likely to fall apart if it's not cleaned up soon. Some suggestions:

  • Add use strict; and use warnings; and then fix all the problems they reveal.
  • Use Time::Piece instead of calling the external date command.
  • Put the hostnames in a database table and join against that table in your SQL.
  • Stop using bind_columns. It's more readable to do something like:

    while (my @row = $sth->fetchrow_array) {
       my (undef, $currentuser, $lastlogin, $host_name) = @row;
    
  • Use a templating system for your HTML output.



来源:https://stackoverflow.com/questions/42718893/want-to-hide-cgi-rendered-html-text-for-hostname-when-username-text-column-is-em

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!