in reply to Net::SSH2 Interactive command example

Thanks for the replies lads. I had tried tinkering with the shell method myself in Net::SSH2 which was on the perlmonks website cause I thought this might be the workaround. Alas to no avail.It still closes the channel by it's own accord. I actually have this working on Net::SSH::Perl::Buffer module but the problem I have with this method is that it is so slow in connecting to the host.It seems to be slow in regard to the key exchange. I had seen a lot on the web in regard to how to quicken it up by installing Math::BigINT::GMP but I saw no reduction in the time it took to connect to the host so I went back to Net::SSH2. So I was hoping that Net::SSH2 would have the solution as it is quicker on connecting to the host. Another get-around is to use system (ssh $host tail -f /log/log.txt); and not use any Perl Modules but I feel this is almost cheating as I don't consider this true perl. Anyone got any ideas on how to get a "tail -f " to work with Net::SSH2 or am I just wasting peoples time here. Cheers, Shooter
  • Comment on Re: Net::SSH2 Interactive command example

Replies are listed 'Best First'.
Re^2: Net::SSH2 Interactive command example
by u235sentinel (Hermit) on Jun 18, 2007 at 15:29 UTC
    Could you show us your code. I've done a lot of development this last year using this module. I believe I used 'shell' when sending commands over the established channel. I don't recall it closing the channel but it's been awhile since I've written code with this module.

    I recall having a lot of problems with STDERR and this module. Is this what you are having difficulty with? From the channel module page it says:
    shell

    Start a shell on the remote host; calls process("shell").

    exec ( command )

    Execute the command on the remote host; calls process("exec", command). Note that only one of these requests can succeed per channel (cp. "exec" in perlfunc); if you want to run a series of commands, consider using shell instead.

    I recommend 'shell'. It worked well for me. Post the code and we'll check it out.

    BTW, while this module is still immature IMO (not even at 1.0 I think). David has done a fantastic job and it's far better than the original NET::SSH::PERL IMO. I've been able to hit a couple hundred Unix/Linux servers in a few minutes and run inventory reports. It's very speedy and does a great job. We're now using it to create user accounts and set passwords in minutes rather than days.

    Yeah it's archaic but we can't decide how to tie everything together with LDAP and I was tired of waiting for management to approve funding :-)
      Sorry for the delay in getting back to you. Apologies I just could not get to a computer yesterday. The Code I was using was the one taken from the perlmonks web-site.
      my $chan2 = $ssh2->channel(); $chan2->shell(); print $chan2 "hostname -a\n"; print "LINE : $_" while <$chan2>; print $chan2 "tail -f /var/adm/messages"; print "LINE : $_" while <$chan2>; $chan2->close;
      (In fact I tried various options on that perlmonks page without success.) The only difference being the "tail -f" command I used instead of "uname" or "who". I notice when I do a "tail -50000". The channel would stay open for as long as it took to get through the 50000 lines. So there is no problem with keeping the channel open as such.I think the problem lies when it is being an interactive real-time command like "tail -f" or "snoop" when I want to keep the channel up and for some reason it takes a snap-shot,or what's in it's buffer and then closes. Any help, greatly appreciated !!!!!!!!!!!!!!!!!!!!!!!. U235sentinel have you ever come across this before?. Regards, Shooter
        print $chan2 "tail -f /var/adm/messages";
        print "LINE : $_" while <$chan2>;
        $chan2->close;

        I'm guessing it thinks it's finished then closes the channel. I didn't realize you meant you needed it open say for hours (for example). My need for the module was to inventory (or change something ) across hundreds of Unix and Linux servers and report the results. It's quick. We can create user accounts and set the password across 500 servers in just a few minutes. Eventually they will get Vintella working and replace the user creation piece. Someday :-)

        Have you tried running with Net::SSH2::Debug enabled? I'm curious if it might shed a clue there. Or even printing $! if there was an error somewhere. If it runs 50,000 lines then why not until we tell it to stop.

        I haven't run into this myself. The code is still under a year old and not even 1.0 yet. So I'm sure there are surprises yet to be discovered :-)

        I'll poke around and see if I can find something that might help us understand how to do this. Cross your fingers.

        It might be a good idea to post this question on the net::ssh2 forum. David Robbins has popped on there to help answer questions about the module. It was mentioned to try using POLL(). Personally I never got it working properly (neither have several other developers). I'm feeling that piece of the code is still broken. Perhaps in the next libssh2 update it will be fixed.
Re^2: Net::SSH2 Interactive command example
by Khen1950fx (Canon) on Jun 18, 2007 at 17:43 UTC
    I think the reason that Net::SSH::Perl::Buffer was slow was that the compression level might have been too high. You can use Net::SSH::Perl::Comp to set the compression level lower. For example,

    my $comp = Net::SSH::Perl::Comp->new('Zlib',1);

    sets the compression level to 1, the valid levels being 0 to 9. The default is 5.

      Hello everyone I have been trying to use this kind of scripts to connect to a remote machine and execute some commands. I think the connection is well done, but the execution of commands is not working for me. This particular case is about: 1.- I connect via ssh to a linux machine 2.- I enter in a special mode with MML commands (machine to man language) typing 'mml' 3.- I do staff with this mml commands My script is failing in step 2. I just want to send this "mml" and it's not working :( :(
      if( $ssh2->connect($host) ) { if( $ssh2->auth_password($user,$pass) ) { print "connected.....\n"; #shell use my $chan = $ssh2->channel(); $chan->blocking(0); $chan->shell(); print "sending command....\n"; my $num = 10; while($num--){ sleep(1); } print $chan "mml\n"; select(undef,undef,undef,0.2); print $buf while ($len = $chan>read($buf,512)) > 0;
      I tried with several methods: print $chan, exec ('comand'), everything in this thread... but still not working. Any idea?