Methods to SSH (Perl)...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Methods to SSH (Perl)...
# 1  
Old 03-11-2011
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.
# 2  
Old 03-11-2011
Quote:
Originally Posted by mrwatkin
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.
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
Code:
ssh user@host command1 ';' command2

or
Code:
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
Code:
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";
}


Last edited by Corona688; 03-11-2011 at 01:49 PM..
# 3  
Old 03-11-2011
Quote:
Originally Posted by Corona688
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
Code:
ssh user@host command1 ';' command2

or
Code:
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
Code:
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.
# 4  
Old 03-11-2011
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Why do these 2 methods result in different outcomes?

I've been troubleshooting a ksh93 script I wrote today and have narrowed it down to the root cause. but I don't understand why? so, i've written the following script to demonstrate the problem i found today: #!/bin/ksh method=$1 FILE_LIST=( $(find /someplace -type f -printf... (10 Replies)
Discussion started by: BLinux
10 Replies

2. Linux

Help with PAM Logging methods.

Hi Folks, Would like to understand if there exists any method to write the logs for LDAP authenticated users and Local Users separately in two different files. If not, then do I distinguish whether the user is LDAP or local without looking at passwd. Bye the way, I am trying this weird... (0 Replies)
Discussion started by: awk-admirer
0 Replies

3. Cybersecurity

Linux Encryption methods

Hi all, I am looking to encrypt a filesystem with a CentOS 6.4 install. However I note that when using LUKS the system does not boot without prompting for the password encryption key. I am looking for an drive/filesystem encryption solution which will allow reboots and shutdown/starts of the... (4 Replies)
Discussion started by: landossa
4 Replies

4. Shell Programming and Scripting

Perl Methods Calling

Hello I am on my way to improve my wonderful Perl skills, I got an issue which I want to share with you all. I have a Perl module which looks like package Cocoa; require Exporter; @ISA = qw(Exporter); my $a=''; my $b=''; my $c=''; sub new { my $this = shift; # Create... (8 Replies)
Discussion started by: adisky123
8 Replies

5. Shell Programming and Scripting

ssh in perl

Hi Can anyone tell me what does the below command do in Perl Thanks in advance (2 Replies)
Discussion started by: irudayaraj
2 Replies

6. Shell Programming and Scripting

Perl SSH without a perl module

I'm trying to create a perl script that will do 1 SSH session, but be able to write multiple commands to the session and receive multiple outputs. I know there are modules out there like Net:SSH::Perl, but I'm not allowed to use it. I was thinking of doing something like an open3 on an ssh... (4 Replies)
Discussion started by: mrwatkin
4 Replies

7. Solaris

Unix learning methods

I have recently completed Solaris 10 System Administration book by Bill Calkins. Now I want to learn more about UNIX. I have tried to research online but there is too much information and I am sort of overwhelmed and don't know where to start. Can anybody give some idea on how to pursue my learning... (3 Replies)
Discussion started by: saudsos
3 Replies

8. UNIX for Advanced & Expert Users

yum provides methods

What is the difference between these yum provides and whatprovides methods? I know provides and whatprovides give the same results, but different methods of */ and \* give different results. Also whether you put */ and \* in front of the string or behind the string give different results. I have... (0 Replies)
Discussion started by: cokedude
0 Replies

9. Shell Programming and Scripting

Methods For Debugging Perl Problems

Note: Not a programmer by profession but occasionally have to program. I am looking for general methods and freely/readily available tools employed to debug problems during development of perl scripts. Anything that has really helped you out with problems you just couldn't find. A couple of... (5 Replies)
Discussion started by: Vi-Curious
5 Replies

10. Shell Programming and Scripting

Send "perl -e '...' " command through SSH, from a perl script

Hey guys I am trying to send a perl -e command to a number of systems using SSH. The command should retrieve some information for me. The problem is, the remote shell tries to interpolate my variables and doesn't get it should take the command literally and just execute it. Below the code.... (2 Replies)
Discussion started by: clrg
2 Replies
Login or Register to Ask a Question