Re: channel SSH2
by zentara (Cardinal) on May 23, 2012 at 13:10 UTC
|
Net::SSH2 has a shell method for its channel.
#!/usr/bin/perl
use warnings;
use strict;
use Net::SSH2;
# assuming a user named 'z' for demonstration
# connecting to localhost, so you need your sshd running
# see maillist archives at
# http://lists.sourceforge.net/lists/listinfo/ssh-sftp-perl-users
# for deeper discussions
my $ssh2 = Net::SSH2->new();
$ssh2->connect('localhost') or die "Unable to connect Host $@ \n";
$ssh2->auth_password('z','ztester') or die "Unable to login $@ \n";
#shell use
my $chan = $ssh2->channel();
$chan->blocking(0);
$chan->shell();
print $chan "ls -la\n";
print "LINE : $_" while <$chan>;
print $chan "who\n";
print "LINE : $_" while <$chan>;
print $chan "date\n";
print "LINE : $_" while <$chan>;
$chan->close;
__END__
| [reply] [d/l] |
|
|
Because I was executing long running commands (and even sometimes on the shorter commands), I had to do something like execute a 'echo DONE' command after every command I wanted to execute, and then read from every command until I got the DONE marker. I even went and wrote an execute_command($command) wrapper function to do this for me.
Update: And maybe I even did something like:echo "DONE[$?]"
| [reply] [d/l] [select] |
|
|
/me nods...
Very good. I am definitely a member of the pragmatic school of, “Works for You = Works For Me,” but with one equally pragmatic caution ...
Be sure to anticipate the error case. Be sure that you are not inadvertantly “eating up all the evidence” while waiting for a DONE-marker that never comes. Consider the various ways that things can go wrong, and what will indicate the presence of those failures. Be certain, by whatever means seem best to you, that the software you are communicating with not only handles the “normal completion” case, but the very-real alternate possibility of abnormal completion as well. In my experience, so-called ABENDs. (Caution: old still-sometimes mainframe geek) cannot reliably be discerned from program output, but facilities always exist by which they can reliably be caught.) Anticipate the train-wrecks and derailings along with everything else.
| |
|
|
this is completely unreliable!
| [reply] |
|
|
You are more of a network expert than me, can you explain why it is unreliable? The shell method works for me.
| [reply] |
|
|
Re: channel SSH2
by salva (Canon) on May 23, 2012 at 12:14 UTC
|
no, unless you run a shell on the remote side and make your program talk to it. | [reply] |
Re: channel SSH2
by locked_user sundialsvc4 (Abbot) on May 23, 2012 at 12:26 UTC
|
Following-on to Salva's suggestion, consider constructing a shell-script file, copying it to the host via sftp, then executing it (either directly, if it is marked executable, or indirectly through a command like source). The host computer will perceive that it has one file to execute which specifies an entire series of commands, and it will execute them all and send you all of the output therefrom. You will, of course, need to know how to evaluate that (combined...) output. You could also construct a more elaborate script for the host to execute. As an aside, I find it very useful to employ templating tools such as Template::Toolkit in situations like these, even though those tools are customarily used in web-sites. This makes it very easy for you to “tweak” exactly what command-file you send over, without substantially altering the code.
If the individual commands in question are big, hairy things, that take a long time to execute and so on, then maybe what you actually have is a “run batch-jobs” situation, and you should consider using batch execution software to submit them, run them, and to collect their output.
| |
Re: channel SSH2
by zentara (Cardinal) on May 23, 2012 at 17:32 UTC
|
my $chan = $ssh2->channel();
$chan->blocking(0);
$chan->shell();
$chan->exec("nohup /home/zentara/perlplay/net/zzsleep > foo.out 2> foo
+.err < /dev/null &");
$chan->exec("nohup /home/zentara/perlplay/net/zzsleep1 > foo1.out 2> f
+oo1.err < /dev/null &");
# etc etc
$chan->send_eof;
Just another idea to play with.
| [reply] [d/l] |