How to use template tool kit


vi /var/www/cgi-bin/somescript.pl

--------------------------------
#!/usr/bin/perl

use File::Copy;
use strict ;
use warnings;
use Data::Dumper ;
use CGI;
use Template;
 use Socket;
print "Content-type: text/html\n\n";
my $remote_host = '59.163.11.66';
exit  unless($ENV{REMOTE_ADDR} eq $remote_host );

my $tt = Template->new( INCLUDE_PATH => "/var/www/html/formBackup" ) || die "template process failed: $!";
my $q = new CGI;
my $editfile = "/tmp/aa1.$$" ;
my @allmastercf = qw(/etc/postfix /etc/postfix2nd );
my $hostname = `hostname`;


my %hash = get_ips();
##print Dumper ([\%hash]);
my (%postfixinstance,%ipsfile,%tag,%ips,$ptr,$myip,$iaddr,$name);
my %hash1;


my $ERRORS="";
foreach my $mastercf (@allmastercf) {
copy("$mastercf/master.cf", $editfile);
my @array1;
open (IN,$editfile ) or die "cannot open file ";
# -o smtp_mbind_address=/etc/postfix/ips_intoday
while (<IN>) {
        chomp;
        next unless /smtp_mbind_address=(\/\S+),?\b/;
        push(@array1, $1) ;
}

push(@array1, "$mastercf/ips") ;
close (IN);

my @ips;
my @nofiles ;
my @noips;




foreach my $ipfile (@array1) {
  unless(open (IN1, "$ipfile")){
    print STDERR "Could not open  $ipfile\n";
    next;
  }
while (<IN1>) {
chomp;
$iaddr = inet_aton("$_"); # or whatever address
$name  = gethostbyaddr($iaddr, AF_INET);
push (@ips, "<br>$_ --> $name</br>");


#$hash1{$mastercf}{$ipfile} = "@ips";
}
  close (IN1) ;
push @{$hash1{ipandfile}},{ip=>"@ips",ipfile=>$ipfile};
@ips ="";

}

push @{$tag{resulth}},{instance=>$mastercf},{info=>$hash1{ipandfile}};

%hash1 = ();
}

#
# Return a hash array %ret = ( '192.168.2.1' => 'eth0:1' , '192.168.2.2' => 'eth0:2' );
#
#print Dumper ([\%tag]);
$tt->process("serverips.html",\%tag) || die $tt->error();




sub get_ips {
        my (%hash);
        my ($ifconfig)="/sbin/ifconfig";
        my (@lines)=qx|$ifconfig | or die("Can't get info from ifconfig: ".$!);
        foreach(@lines){
                if(/inet addr:([\d.]+)/){
                        $hash{$1}++  ;
                }
        }
        return %hash;
}


-----------------------------------------------------------------------------------

vi /var/www/html/formBackup/servreip.html



------------------------------------------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>netcore Data backup </title>
<link rel="stylesheet" type="text/css" href="/databu/view.css" media="all">
<script type="text/javascript" src="/databu/view.js"></script>

</head>
<body id="main_body1" >

<img id="top" src="/databu/to1p.png" alt="">
<div id="form_container">

<h1><a>EMM server instance ips  and PTR  </a></h1>
<div class="form_description">
<!--<p>This is the file is written to /etc/backup/BACKUPDAILY</p> -->
</div>
<ul >

<li id="li_1" >
<label class="description" for="element_1">EMM server instance ips  and PTR </label>

<table width="681" cellspacing="2" bgcolor="darkGray">
  <tbody align="left" valign="baseline" bgcolor="lightGray">
    <tr>
      <td><h3>Instance</h3> </td>
      <td><h3><CENTER>IP    ---> PTR </CENTER></h3></td>
      <td><h3>IP File</h3></td>
    </tr>
[% FOREACH item IN resulth %]
    <tr>
      <td bgcolor="lightBlue">[% item.instance %]</td></tr>
      [% FOREACH item2 IN item.info %]
      <tr>
      <td>&nbsp;</td>
      <td>[% item2.ip %]</td>
      <td>[% item2.ipfile %]</td>
    </tr>
    [% END %]
[% END %]
  </tbody>
</table>




</li>

<li class="buttons">
<input type="hidden" name="form_id" value="286586" />

</li>
</ul>
</div>
<img id="bottom" src="/databu/bottom.png" alt="">
</body>
</html>
---------------------

No comments:

Other Articles

Enter your email address: