Zou het theoretisch mogelijk zijn het volgende cgi script om te zetten in php??
Ik heb nl niet veel verstand van cgi, maar volgens mij opent een file en interprete die. Dat is in php wel mogelijk maar ik weet eerlijk gezegd om wat voor file het gaat. Dit is het script van sasacct, die via iptables het verkeer meet per ip. Ik weet niet of iemand het kent, of dat het misschien al gedaan is (niets gevonden in ieder geval)
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
| #!/usr/bin/perl use strict; use CGI::Carp('fatalsToBrowser'); use CGI qw(:all); use Time::Local; my $q = new CGI; my $startdate=(param('syear').param('smonth').param('sday')); my $enddate=(param('eyear').param('emonth').param('eday')); my $dograph = $q->param('graph'); my $bits = $q->param('bits'); my $zoom = $q->param('zoom'); my $ip=$q->param('ip'); my %remote; my %rkeys; my %local; my ($sec,$min,$hour,$mday,$mon,$year) = localtime(); my $system_time = sprintf("%04d%02d%02d% 02d%02d%02d", ($year+1900), $mon+1, $mday, $hour, $min, $sec); my @years = (2001,2002,2003); my %mlabels= ('01' => 'Jan','02' => 'Feb','03' => 'Mar','04' => 'Apr','05' => 'May','06' =>'Jun','07' => 'Jul', '08' => 'Aug','09' => 'Sep',10 => 'Oct',11=>'Nov',12 => 'Dec'); my @months =('01','02','03','04','05','06','07','08','09','10','11','12'); my @days = qw(01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31); my $configfile = "/etc/sasacct.conf"; #EDIT HERE IF NEEDED #Parse configfile open(CNF,"$configfile"); while(<CNF>) { if ($_ =~ /^D/ && !exists($rkeys{D})) { chop $_; s/^D://g; s/\/$//g; $_ = $_."\/"; $rkeys{D} = $_; } if ($_ =~ /^L/){ chop $_; s/^L://g; my ($ip,@i)=split(/:/,$_); $local{$ip}= \@i if !defined($local{$ip}); } if ($_ =~ /^R/) { chop $_; s/^R://g; my ($ip,@j)=split(/:/,$_); $remote{$ip}=\@j if !defined($remote{$ip}); } } close CNF; #Generate array containing the ips my @ips; push @ips,"All"; if (stat($rkeys{D})) { opendir DIR,$rkeys{D} || die ("Cannot open: $!"); my @files = readdir(DIR); closedir DIR; for(@files) { if ($_ =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(\_\d)*?$/ && $_ !~ /rrd/g){ push @ips,$_; } } } #begin html data print header(); print start_html( -title => 'SASacct Accounting Statistics', -bgcolor => '#FFFFFF', -link => '#0000EE', -vlink =>'#551A8B' , -alink => '#FF0000' ); print center,p('Traffic Statistics'); print hr; if (defined($startdate) && defined($enddate)) { my ($in,$out) = &gen_traff($startdate,$enddate,$ip); print &gen_form(); #print hr; if (defined($in) && defined($out)){ print center,p("Total traffic from $startdate to $enddate for $ip ($local{$ip}[0])"); my $total = $in+$out; my $totalmb = int ($total/1024/1024); print table({-celpadding => '0' , -cellspacing =>'0' , -border => '0'}, Tr({-bgcolor=> '#6495ED'}, th(['Ip Number','Input Traffic[bytes]','Output Traffic[bytes]', 'Total [bytes]','Total [MBytes]'])), Tr({-valign =>"middle",-align =>"right"}, td(["$ip","$in","$out","$total","$totalmb"]))); } # Decide what will we graph if ($dograph eq 'on' && $ip ne "All" ){ my $utcstart = timelocal('00','00','00',param('sday'),param('smonth')-1,param('syear')); my $utcend = timelocal('00','00','00',param('eday'),param('emonth')-1,param('eyear')); ($utcend = $utcstart + 86000) if ($utcstart == $utcend); ($bits eq "on") ? ($bits = 1) : ($bits = 0); ($zoom eq "on") ? ($zoom = 1): ($zoom = 0); my $fpathrrd = $rkeys{D}.param('ip').".rrd"; print br,br; #On the fly graph goes here. if ( -f ("$fpathrrd") && ($utcstart < $utcend)) { print img({-src => "/cgi-bin/showgraph.cgi?utcstart=$utcstart&utcend=$utcend&file=$fpathrrd&bits=$bits&zoom=$zoom"}); } } print br,br; print hr; } else { print &gen_form(); } print qq~ <center><em>generated by <a href="http://rousse.pm.org/sasacct">sasacct</a></em></center> ~ ; print end_html(); #end html #Function generating input form sub gen_form{ return startform( -method => 'POST'), center,p('Start Date [YYYY/MM/DD]'), popup_menu(-name => 'syear', -values => \@years,-Default=>@years[1]), popup_menu(-name=>'smonth', -values=> \@months, -labels=> \%mlabels), popup_menu(-name => 'sday', -values=> \@days), p('End Date [YYYY/MM/DD]'), popup_menu(-name => 'eyear', -values => \@years, -Default=>@years[1]), popup_menu(-name=>'emonth', -values => \@months , -labels => \%mlabels), popup_menu(-name => 'eday', -values=> \@days), p('Select IP Address'), p,checkbox(-name=>'graph',-label => 'Generate graphics for the selected period'), p,checkbox(-name=>'bits',-label => 'Show graph in Bits per Second'), p,checkbox(-name=>'zoom',-label => 'Zoom the graphic'), p,popup_menu(-name => 'ip', -values=>\@ips), p,submit(-value => 'Submit'),reset(-value => 'Reset'), endform; } #Function summarizing the accounting data from date to date sub gen_traff{ my($start,$end,$ip)= @_; my %bytes; if ($ip eq "All") { &gen_traff_all($start,$end); }else { open(IN,"$rkeys{D}"."$ip") if stat("$rkeys{D}"."$ip"); my $line = <IN>; chop $line; while (defined($line)) { my ($date,$time,$ip,$in,$out) = split(/\s+/,$line); if ($date ge $start && $date le $end ) { $bytes{in} += $in; $bytes{out} += $out; } $line = <IN>; } return $bytes{in},$bytes{out}; } } sub gen_traff_all { my($start,$end) =@_; my %bytes; foreach (@ips){ open (IN,"$rkeys{D}"."$_") if stat("$rkeys{D}"."$_") ; my $line = <IN>; chop $line; while ( defined($line)) { my ($date,$time,$ip,$in,$out) = split(/\s+/,$line); if ($date ge $start && $date le $end ) { $bytes{in} += $in; $bytes{out} += $out; } $line = <IN>; } } return $bytes{in},$bytes{out}; } |
Ik heb nl niet veel verstand van cgi, maar volgens mij opent een file en interprete die. Dat is in php wel mogelijk maar ik weet eerlijk gezegd om wat voor file het gaat. Dit is het script van sasacct, die via iptables het verkeer meet per ip. Ik weet niet of iemand het kent, of dat het misschien al gedaan is (niets gevonden in ieder geval)