britney has asked for the wisdom of the Perl Monks concerning the following question:

I learned perl and did not use for a long time. Now i have a problem, i hope you can show me to edit the code and make it work. I have a iplist.txt , when a system boot up, it will write IP , hotsname to iplist.txt
10.31.61.230,fall11,Thu 03/30/2011 15:20:46.70 10.31.61.212,spring96,Thu 03/30/2011 15:19:56.89 10.31.61.230,spring96,Thu 03/31/2011 15:40:46.70 10.31.61.230,winter11,Thu 03/31/2011 15:40:46.70 Because IP will change and hostname can be change too because it has d +ifferent windows OS How do i get new log with the latest info ? like latestip.txt Now 10.31.61.230,fall11,Thu 03/30/2011 15:20:46.70 change hostname the next day so i have to keep new hostname and ip add +ress 10.31.61.230,winter11,Thu 03/31/2011 15:40:46.70 If we have IP change from 10.31.61.212,spring96,Thu 03/30/2011 15:19:56.89 to 10.31.61.230,spring96,Thu 03/31/2011 15:40:46.70 then keep the new IP address and hostname 10.31.61.230,spring96,Thu 03/31/2011 15:40:46.70 so it will create new file and this new log latestip.txt will be 10.31.61.230,winter11,Thu 03/31/2011 15:40:46.70 10.31.61.230,spring96,Thu 03/31/2011 15:40:46.70
#viewip.pl #print "content-type: text/html\n\n"; use Time::Local; $today = timelocal(localtime); $datafile=`cat ../hosts/iplist.txt | sort -r | sort -u -t ',' -k 2,2 > + ../hosts/latestip.txt`; my $count = 0; open (DATA,"/home/myplace/hosts/latestip.txt") || die ("Can't Open dat +a File \n"); @data=<DATA>; close DATA; &header_response; $x=0; foreach $line (@data) { $x++; ($ip, $hostname , $datetime)=split(/\,/,$line); print "<TD BGCOLOR='d3d3d3'>$hostname</TD><TD BGCOLOR='d3d3d3'>$ip +&nbsp;</TD><TD BGCOLOR='d3d3d3'>$datetime&nbsp;</TD></TR> \n"; $count++; } &footer_response; ###################################################################### +############################## sub header_response { print "Content-type: text/html\n\n"; print "<HTML><HEAD><TITLE>test</TITLE>\n"; print "</HEAD>\n"; print qq^<BODY background="images/bg2.jpg" TEXT="blue" LINK="blue" + VLINK="#9900CC" ALINK="#330066">\n^; print "<CENTER><h1><font color=black>Our IP Info</font></h1></CENT +ER>\n"; print "<BR>\n"; print "</form></CENTER>\n"; print "<center><FONT size=+1><TABLE BORDER=1><TR bgcolor=#669900>< +TD><center><font color=white>Host Name</center></TD><TD><center><font + color=white>IP address</center></TD><TD><font color=white><center>La +st Boot Windows</center></TD></TR>\n\n"; return; } sub footer_response { print "</TABLE></font></CENTER>\n"; print "<BR><BR><BR><BR>\n"; print " </BODY></HTML>\n"; return; }
Thanks for teaching me.

Replies are listed 'Best First'.
Re: How to get latest IP and hostname.
by jwkrahn (Abbot) on Apr 01, 2011 at 06:53 UTC
    How do i get new log with the latest info ?

    You need to sort your file on the date field to get the "latest" info, however your date field is not sortable in its present condition.    To sort it properly you need to have the most significant field (year) first followed in order by significance to the least significant field (seconds).    Or you could store the field as the Unix time in seconds since the epoch and sort it numerically.

    You should start your programs with these two lines to let Perl help you find mistakes:

    use warnings; use strict;


    use Time::Local; $today = timelocal(localtime);

    You can replace those two lines with:

    $today = time;

    Although you never use the $today variable anywhere.



    $datafile=`cat ../hosts/iplist.txt | sort -r | sort -u -t ',' -k 2,2 > + ../hosts/latestip.txt`;

    You have a useless use of cat there:

    $datafile = `sort -r ../hosts/iplist.txt | sort -u -t ',' -k 2,2 > ../ +hosts/latestip.txt`;

    And you don't really need a temporary file there, you could just use open on the pipe.



    my $count = 0;

    You never use this variable anywhere.



    open (DATA,"/home/myplace/hosts/latestip.txt") || die ("Can't Open dat +a File \n"); @data=<DATA>; close DATA;

    You don't really need this temp file, and you don't really need to read the whole file into memory:

    open PIPE, '-|', 'sort -r ../hosts/iplist.txt | sort -u -t ',' -k 2,2' or die "Cannot open pipe from sort because: $!"; while ( <PIPE> ) { # do your stuff here } close PIPE or warn $! ? "Error closing sort pipe: $!" : "Exit status $? from sort";


    $x=0;

    You never use this variable anywhere.



    sub header_response { print "Content-type: text/html\n\n"; print "<HTML><HEAD><TITLE>test</TITLE>\n"; print "</HEAD>\n"; print qq^<BODY background="images/bg2.jpg" TEXT="blue" LINK="blue" + VLINK="#9900CC" ALINK="#330066">\n^; print "<CENTER><h1><font color=black>Our IP Info</font></h1></CENT +ER>\n"; print "<BR>\n"; print "</form></CENTER>\n"; print "<center><FONT size=+1><TABLE BORDER=1><TR bgcolor=#669900>< +TD><center><font color=white>Host Name</center></TD><TD><center><font + color=white>IP address</center></TD><TD><font color=white><center>La +st Boot Windows</center></TD></TR>\n\n"; return; } sub footer_response { print "</TABLE></font></CENTER>\n"; print "<BR><BR><BR><BR>\n"; print " </BODY></HTML>\n"; return; }

    Why are these subroutines?    Each one should just be a single print statement:

    print <<'HTML'; Content-type: text/html <HTML><HEAD><TITLE>test</TITLE> </HEAD> <BODY background="images/bg2.jpg" TEXT="blue" LINK="blue" VLINK="#9900 +CC" ALINK="#330066"> <CENTER><h1><font color=black>Our IP Info</font></h1></CENTER> <BR> </form></CENTER> <center><FONT size=+1><TABLE BORDER=1><TR bgcolor=#669900><TD><center> +<font color=white>Host Name</center></TD><TD><center><font color=whit +e>IP address</center></TD><TD><font color=white><center>Last Boot Win +dows</center></TD></TR> HTML print <<'HTML'; </TABLE></font></CENTER> <BR><BR><BR><BR> </BODY></HTML> HTML
      Thanks for showing me my mistake . The variable i used for counting i removed and left some extra code there ;-) Now i have to convert it to unix datetime then sort it ? 1. how it know if the same IP then choose the latest hotsname ? 2. How it know if the same hostname then choose the latest IP ? Thanks
Re: How to get latest IP and hostname.
by Khen1950fx (Canon) on Apr 01, 2011 at 07:56 UTC
    Another way to format time is to use POSIX strftime:
    #!/usr/bin/perl use strict; use warnings; use POSIX qw(strftime); my $today = strftime "%a %m/%d/%Y %H:%M:%S", localtime; print $today, "\n";
    I ran it, and it returned:
    Thu 03/31/2011 23:45:52