<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Networking Blog &#187; apache</title>
	<atom:link href="http://blog.webdir.bg/tag/apache/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.webdir.bg</link>
	<description>Networking - Cisco, Juniper, Linux</description>
	<lastBuildDate>Thu, 02 Feb 2012 21:09:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>PERL Apache Realtime Output From Script</title>
		<link>http://blog.webdir.bg/perl-apache-realtime-output-from-script/</link>
		<comments>http://blog.webdir.bg/perl-apache-realtime-output-from-script/#comments</comments>
		<pubDate>Tue, 22 Jun 2010 13:10:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://blog.webdir.bg/?p=325</guid>
		<description><![CDATA[PERL Apache Realtime Output From Script, using  Net::Telnet]]></description>
			<content:encoded><![CDATA[<p>As of Apache 1.3, CGI scripts are essentially not buffered. Every time your script does a &#8220;flush&#8221; to output data, that data gets relayed on to the client. Some scripting languages, for example Perl, have their own buffering for output &#8211; this can be disabled by setting the $| special variable to 1. Of course this does increase the overall number of packets being transmitted, which can result in a sense of slowness for the end user.<br />
CGI scripts that generate their own headers are called nph  (non-parsed headers) scripts. The server must know in advance whether the particular CGI script intends to return a complete set of headers. Web servers handle this differently, but most recognize CGI scripts with a nph- prefix in their filename.<br />
When sending complete headers, you must at least send the   status line plus the Content-type and Server headers. You must print the entire status line; you should not print the Status header. As you will recall, the status line includes the protocol and version string (e.g., &#8220;HTTP/1.1&#8243;), but as you should recall, CGI provides this to you in the environment variable SERVER_PROTOCOL. Always use this variable in your CGI scripts, instead of hardcoding it, because the version in the SERVER_PROTOCOL may vary for older clients.<br />
For example the next perl script, is using telnet to connect to remote host and is executing traceroute. ( The source of this script is taken from freshmeat&#8217;s looking glass project). The most important lines are from 2 to 5. This script is using &#8220;Content-type: text/plain&#8221;, if you plan to use html tags chenged to &#8220;Content-type: text/html&#8221;.<br />
$| &#8211; If set to nonzero, forces a flush after every write or print.<br />
Example of nph-realtime-output.pl</p>
<pre class="brush: perl; title: ; notranslate">
#!/usr/bin/perl
print &quot;$ENV{SERVER_PROTOCOL} 200 OK\n&quot;;
print &quot;Server: $ENV{SERVER_SOFTWARE}\n&quot;;
print &quot;Content-type: text/plain\n\n&quot;;
$|=1;
use Net::Telnet;
$login=&quot;MyLogin&quot;;
$password=&quot;MySecret&quot;;
$port=&quot;23&quot;;
$host=&quot;xxx.xxx.xxx.xxx&quot;;
$command=&quot;traceroute www.google.com | no-more&quot;;
$telnet = new Net::Telnet;
$telnet-&gt;errmode( sub { print &quot;ERROR:&quot; . join('|', @_) . &quot;\n&quot;; } );
$telnet-&gt;timeout('10');
$telnet-&gt;option_callback( sub { return; } );
$telnet-&gt;option_accept(Do =&gt; 31);
$telnet-&gt;open(Host =&gt; $host, Port =&gt; $port);
if ($login ne &quot;&quot;) {
  $telnet-&gt;waitfor('/(ogin|name|word):.*$/');
  $telnet-&gt;print(&quot;$login&quot;);
}
if ($password ne &quot;&quot;) {
    $telnet-&gt;waitfor('/word:.*$/');
    $telnet-&gt;print(&quot;$password&quot;);
}
$telnet-&gt;waitfor(Match =&gt; '/.*[\$%&gt;] {0,1}$/',
                 Match =&gt; '/^[^#]*[\$%#&gt;] {0,1}$/');
$telnet-&gt;telnetmode(0);
$telnet-&gt;put(pack(&quot;C9&quot;,
                  255,                  # TELNET_IAC
                  250,                  # TELNET_SB
                  31, 0, 200, 0, 0,     # TELOPT_NAWS
                  255,                  # TELNET_IAC
                  240));                # TELNET_SE
$telnet-&gt;telnetmode(1);
my $telnetcmd = $command;
$telnet-&gt;print(&quot;$telnetcmd&quot;);
$telnet-&gt;getline;               # read out command line
while (1) {
  if ($#output &gt;= 0) {
    $_ = shift (@output);
  }
  elsif (! $telnet-&gt;eof) {
    my ($prematch, $match) = $telnet-&gt;waitfor(Match =&gt; '/\n/',
                                              Match =&gt; '/[\$%#&gt;] {0,1}$/',
                                              Errmode =&gt; &quot;return&quot;)
    or do {
    };
    if ($match =~ /[\$%#&gt;] {0,1}$/) {
      $telnet-&gt;print(&quot;quit&quot;);
      $telnet-&gt;close;
      last;
    }
    push @output, $prematch . $match;
    next;
  }
  else {
    last;
  }
  print $_;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.webdir.bg/perl-apache-realtime-output-from-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

