Methods to SSH (Perl)...

Can anyone break down the different methods of using SSH in perl? I'm currently using Net::SSH::Expect, which allows me to login to a machine and execute multiple commands without having to ssh again. This feature of holding the session works well for me, but it's slow. If I set timeouts to 4 seconds, I have to wait the full 4 seconds on each command I execute. the timeouts can be changed on the fly, but that still isn't the best plan for my script. Does anyone know of a method that allows these sessions and will continue the script immediately after a command is executed? The waiting part is an issue for me and sometime, I need to set a really long timeout. If I'm not explaining this well enough, I can elaborate:

If I set timeouts = 10, it'll take 10 seconds to login, 10 seconds to run $ssh->exec("cd somedirectory"); and 10 seconds for each subsequent exec.

If my changing of a directory can finish in less than a second, I want it to begin the next line of code immediately. Can anyone help me out?

Thanks in advance for all responses.

---------- Post updated at 12:29 PM ---------- Previous update was at 09:46 AM ----------

Also, are there any books that break down the advantages/disadvantages of other functions/classes/etc. ? I'd like to see something with code usage as well.

There's much, much better ways to do this, "expect" is just a third-party hack which tackles a few special cases that positively can't be handled any other way.

In a shell I would do

ssh user@host command1 ';' command2

or

ssh user@host exec /bin/sh -s arg1 arg2 arg3 < local-shellscript.sh

In Perl you can just dump those raw into system(), which of course will wait, but you can

use POSIX ":sys_wait_h";

...

$pid=fork();
if($pid == 0)
{
        exec("ssh user@host command1 ';' command2");
        die "exec failed";
}

# parent will continue here without waiting for the child.
# you can check periodically with
if(waitpid($pid, WNOHANG) > 0)
{
        print "Child finished with status $?\n";
}

I see and understand the concept to an extent, but it doesn't look like this code can assign the output of the commands to variable within my perl script. If this is not the case, please explain.

Thanks.

There's lots and lots and lots of ways to do it. The obvious and easy ones only communicate one way but since you can still pass variables in at start-time that may be sufficient. It's possible you may need to modify your communication scheme slightly to make it more efficient. You could even go so far as to pipe entire perl or shell scripts over to the remote host and have the logic done there...

So, I can't give a precise answer until I see what you're actually doing.