当用户名文本列为空时,要隐藏cgi为主机名显示html文本 [英] Want to hide cgi rendered html text for hostname when username text column is empty

查看:115
本文介绍了当用户名文本列为空时,要隐藏cgi为主机名显示html文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我们的服务器上运行的.cgi文件中包含当前的perl代码。这让我们知道哪些员工正在使用特定的机器,通过显示:
hostname - username



但是,我希望能够隐藏主机名没有用户可以缩短呈现的文本,但我不熟悉这样做。



如果可能的话,我还想添加逻辑以在那里显示消息没有员工登录,就像'没有员工轮班'。



另外,这些不是真正的用户/密码,只是fyi。

 #! / usr / bin / perl 
$ ENV {'ORACLE_HOME'} =/ usr / lib / oracle / 11.2 / client64;
使用DBI;
my $ date =`date`;
my $ las ='bziegle jpietrza hpietrza lpietrza';
$ login =user;
$ password =pass;
my $ dbh = DBI-> connect(DBI:Oracle:icsprod,$ login,$ password);
dieUnable to connect:$ DBI :: errstr \\\
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' )
或(hosts.host_name = '1408hiksg951p2')
或(hosts.host_name = '1408hiksg951p3')
或(hosts.host_name = '1408mathb10p1')
或(hosts.host_name = '1408mcutc216p1')
或(hosts.host_name = '1408mrdh146sp7')
或(hosts.host_name = '1408mthw116p1')
或(hosts.host_name = '1408phys22p5' )
OR(hosts.host_name ='1408rhph316p11')
OR(hosts.hos t_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);
打印Content-type:text / html\\\
\\\
;
打印< html> \\\
;
print< meta http-equiv = refresh content = 300> \\\
;
print< meta http-equiv ='pragma'content ='no-cache'> \\\
;
print< style type = \text / css \> \\\
;
printbody {font-family:\Arial \,sans-serif; font-size:small; color:black} \\\
;
打印< / style> \\\
;
打印< head> \\\
;
打印< title> \\\
;
打印LA station usage.\\\
;
打印< / title> \\\
;
打印< / head> \\\
;
print< body> \\\
;
打印< table> \\\
;
print< tr>< td colspan = 5> $ date< / td>< / tr>; $ u $ gt;< u>< u>< u> TR> \\\
;
while($ sth-> fetch()){
$ lastlogin =〜s /。* $ //;
$ host_name =〜s / 1408 //;
foreach(@las){
if($ currentuser eq$ _){
$ lacolor =black;
最后;
} 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')
或(hosts.host_name ='1408hamp3144p33')
OR(hosts.host_name ='1408mathb10p2')
OR(hosts.host_name ='1408scg046p3')
OR(hosts.host_name =' (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>替代LA电台< / tr>< / td>< / tr> \\\
;
while($ sth-> fetch()){
$ lastlogin =〜s /。* $ //;
$ host_name =〜s / 1408 //;
foreach(@las){
if($ currentuser eq$ _){
$ lacolor =black;
最后;
} else {
$ lacolor =red;
}
}
print< tr>;
print< td> $ host_name< / td>;
print< td>< font color = \$ lacolor \> $ currentuser< / font>< / td>< td>& nbsp;< / td> ;
}
打印< / table>;
打印< / body> \\\
;
打印< / html> \\\
;
$ sth-> finish();
$
b
- > disconnect();


解决方案

数据库的布局并不清楚,但我认为如果没有人登录到主机上,那么 $ currentuser 变量将是未定义的。因此,在创建表的主循环中,如果情况如此,则可以跳过迭代。例如:

  while($ sth-> fetch()){
next除非定义了$ currentuser;

#其余的现有代码
}

但所有总之,这段代码需要很多工作。我相信它现在可以做你想做的,但这是一个难以维系的混乱,如果它很快没有被清理,它很可能会崩溃。一些建议:


  • 添加使用strict; 使用警告; 然后修复它们泄漏的所有问题。

  • 使用 Time :: Piece 而不是调用外部日期命令。 将主机名在数据库表中并在SQL中与该表进行连接。

  • 停止使用 bind_columns 。 (b)
    $ b $ pre $ 我的(undef,$ currentuser,$ lastlogin,$ host_name)= @row;


  • 使用模板系统为你的HTML输出。



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();

解决方案

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.

这篇关于当用户名文本列为空时,要隐藏cgi为主机名显示html文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆