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

Hi, I've got a problem using Perl to connect to Telnet servers. Here's the situation. I have one script named execScripts.pl that calls another script named login.pl to ebstablish the connection and log in. execScripts.pl then calls yet another script named process.pl to issue commands to telnet server and receive reply. So is there a way for process.pl to use the telnet connection already created by login.pl? Any help would be very much appreciated. Thank you.

Replies are listed 'Best First'.
Re: Perl and Telnet
by sk (Curate) on Jul 27, 2005 at 04:51 UTC
    If they are separate scripts then I cannot think of a way in which one script can pass on the telnet object to another script. if you want something like this you need to break them into modules and call the method to start the telnet process and return the telnet object. Once you get the telnet object you should be able to use it another place

    . BTW, how are you doing the telnet? are you using the command 'telnet" or using a module? I would recommend Net::Telnet

    -SK

    update: Here is a code snippet using Net::Telnet

    #!/usr/bin/perl -w use Net::Telnet; my $telnet; # Your prompt setting might be different $telnet = new Net::Telnet ( Timeout => 10, Errmode => 'die', Prompt => + '/.*%/'); $telnet->open('YOUR HOSTNAME'); # Enter your host name here print ("Please enter Username: "); $user = <STDIN>; print ("Please enter password: "); $pass = <STDIN>; $telnet->login($user, $pass); # You can hard code user/pass but here i + am getting getting user input to avoid typing them in. print ($telnet->cmd('date')); # Run a command then print out the resul +t

    You can put this entire block until execution into a package/sub. Then whenever you want to execute a command you can just use the telnet object that was created.

    (assuming your return it back to the caller)

      Hi Sk, NetWallah! Thanks for your response. Yes, I'm using module Net:Telnet. As you suggested, I could easily separate the program logic into methods instead of files. The problem is I want a bit flexibility into my program. After calling login.pl, execScripts.pl can invoke process.pl or one or many other scripts to issue commands to the telnet server. How can I fork one script as if it is the child process of another script?
        Here is a framework and hopefully it works for you. You start a script that does the "telnet" and waits for other scripts to execute commands on that telnet session. For such things you check out  perldoc perlipc. You can make your telnet program as a mini-server which will take requests from clients and then process them and return the results for you.

        For info about forking processes try  perldoc -f fork. Also there are lot of nodes on this. Try supersearch

        I am not sure what exactly this app would do but trying to help the "remote" server by having only one telnet session might be too much work for you and might not be a big deal for the server (if you are not planning on sending tons of msgs/sec). If the scripts should exchange data, then reading on IPC will help you.

        Good Luck!

        -SK

Re: Perl and Telnet
by NetWallah (Canon) on Jul 27, 2005 at 16:31 UTC
    Untested speculation - but this may work:

    Declare the $telnet object in the "execScripts.pl" (Caller) module - then fork a process to start the other script. The forked process has R/W access to the original $telnet - you need to figure out how to import this into the script (Perhaps using "do").

         "Income tax returns are the most imaginative fiction being written today." -- Herman Wouk