in reply to Perl and Telnet

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)

Replies are listed 'Best First'.
Re^2: Perl and Telnet
by Leviathan7z (Initiate) on Jul 28, 2005 at 03:23 UTC
    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