in reply to channel SSH2

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__

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

Replies are listed 'Best First'.
Re^2: channel SSH2
by runrig (Abbot) on May 23, 2012 at 16:06 UTC

    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[$?]"

      /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.

Re^2: channel SSH2
by salva (Canon) on May 23, 2012 at 13:32 UTC
    this is completely unreliable!
      You are more of a network expert than me, can you explain why it is unreliable? The shell method works for me.

      I'm not really a human, but I play one on earth.
      Old Perl Programmer Haiku ................... flash japh
        try running slow commands, for instance...
        for $i in 1 2 3 4; do sleep 1; echo $i; done
        You need a way to find out where the output of some command ends. You can look for the shell prompt, or make your commands print their output encoded in some format (for instance MIME-encoded), but in any case, it is a hassle.