Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

regarding session in CGI

by mnsbb (Initiate)
on Dec 17, 2003 at 14:21 UTC ( [id://315274]=perlquestion: print w/replies, xml ) Need Help??

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

Hi pals,

I am trying to telnet to a machine and perform some operations. The idea is get some inputs from the user in the first screen and configure the machine with those inputs through the second one.

If i telnet to machine in the second screen it is taking 12 seconds time. i was told that i shall use CGI session, so that i can telnet to the machine inthe first screen itself and store the telnet to a session; pass the session to the second screen and retrieve there.

I am struck with storing and retrieving the session.

here is the code first screen:

#!e:/perl/bin/perl use CGI; use CGI::Session; require "telnet.pm"; print "Content-type:text/html\n\n"; print << "END_OF_MESSAGE"; <HTML><HEAD><TITLE>AutoGenerate</TITLE> <base target="_top"> </base> </HEAD> some stuff to collect inputs </HTML> END_OF_MESSAGE #create cgi object $cgi=new CGI; $session = new CGI::Session( "driver:File", $cgi, {Directory=>"C:/temp +/"} ); #create telnet object, and save it in session my $telnet = new Net::Telnet ( Timeout=>1, Errmode=>'return'); $telnet->open(Host=>'hostname', Port=>'port') or die "cannot login"; print "\nLogged: $telnet\n"; $teln = $cgi->param("$telnet"); $session->param($telnet, $teln);

the second screen:

#!e:/perl/bin/perl use CGI::Session; use CGI; print "Content-type:text/html\n\n"; $session = new CGI::Session( "driver:File", $cgi, {Directory=>"C:/temp +/"} ); $telnet = $session->param("$teln"); print "\nretrieved from session: $telnet"; $q = new CGI; if ($telnet) { $telnet->print("configure term\n"); $telnet->waitfor('/configure/i'); $telnet->print("exit\n"); $telnet->waitfor('/[\$%#:>].$/i'); $telnet->print("exit\n"); $telnet->waitfor('/[\$%#:>].$/i'); } else { print "Else of telnet: $telnet"; } print <<"EOM"; <HTML> </HTML> EOM


I am facing problems in retrieving the session in teh second screen and not able to configure.
Can anyone help me..
Thanks in advance, mnsbb

janitored by ybiC: Balanced <code> and <readmore> tags

Replies are listed 'Best First'.
Re: regarding session in CGI
by hanenkamp (Pilgrim) on Dec 17, 2003 at 14:41 UTC

    You're going to have a problem doing this since your CGIs are going to be run each as a separate process. Therefore, between requests, your telnet object is being serialized to disk, but the socket will be closed when the application exists. Therefore, the second request no longer refers to an active socket.

    There are a number of work-arounds, though. You could run your scripts inside of mod_perl and save your telnet information in memeory to keep the socket alive between requests. Another solution would be to have a separate process running as a sort of middleware to actually manage the telnet connection and then use IPC to tell the persistent script what to do--a named pipe could work well here.

      thanks a lot hanenkamp. I m new to perl and cgi. i m little confused with mod_perl. Hence i beseech u to give me a clear picture of where mod_perl sits in my code, or please refer me some clear documents in gaining knowledge about that. Thanks again. mnsbb

        Actually, mod_perl is just a platform within which you run your code. See http://perl.apache.org/ for details on mod_perl. Essentially, your CGI scripts can be used as is to run inside a special mod_perl handler process. Or you can rewrite your scripts to use mod_perl's handler API directly.

        Thus, you can probably use your code as is within mod_perl (though, you'll still need some modification to cache your telnet connection) or you can rewrite it to use the mod_perl framework directly, if you prefer.

        Hi mnsbb,
        Don't worry, you're not alone...
        ...or please refer me some clear documents in gaining knowledge about that...
        I'm looking at them right now which can be found at http://perl.apache.org. I too am struggling a bit with mod_perl but the benefits seem to be worth it in the long run.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://315274]
Approved by Ninthwave
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-03-29 14:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found