maybe you want to use exec! ?


ŌŚ Sat, 27 Nov 2010 01:17:14 +0800£¬Guillermo Riojas  guillermo.riojas / gmail.com> Š“µĄ:

> Hi there,
>
> Recently i started playing with the Net::SSH gem. So far, good, but i
> just came across i question i haven't been able to respond.
>
> My intention is to issue several commands using the same channel in
> order to be able to give continuity to my list of instructions, makingt somehow "interactive". So far, after the first command is typed andhe output returned, the rest of the instructions seem to be "not
> executed". As i am pretty new to the gem and SSH in general, i realized
> that somehow once the channel is closed the session too and of course,pening a new channel puts me back in the starting point.
>
> So if i would like to create folder, navigate to the folder i just
> created , do some other stuff there and then exit, wont work as i
> expect, because every command sent starts in a new session.
>
> A sample of the code here:
>
> def exec_cmd
>   cont = true
>
>   channel = @ssh.open_channel do |chn|
>     while cont
>       print "cmd> "; STDOUT.flush; str = gets.chop  #User input command
>       cont = false if str.empty?
>       channel_execute chn, str.to_s
>     end
>   end #channel open loop
>
>   channel.wait
> end
>
> def channel_execute(chn, cmd = nil)
>   return nil if cmd.nil?
>   puts "Executing Command [#{cmd}]"
>
>   chn.exec(cmd.to_s) do |ch,success|
>     raise "Could not execute command" unless success
>
>     ch.on_data do |c, data|
>       out = data.split("\n")
>       out.each do |e|
>         puts "#{e}"
>       end
>     end
>
>     ch.on_extended_data do |c, type, data|
>       out = data.split("\n")
>       out.each do |e|
>         puts "#{e}"
>       end
>     end
>
>     ch.on_close { @log.debug("exec") {"Command loop finished."} }
>
>   end #chn exec loop
>
>   chn.wait
>
> end
>
>
> Now, my first work around is sending once the list of all the commands,
> in this way "mkdir testfolder; cd testfolder; mkdir testfolder2; cd /;.."
>
> However, is there any way i can keep the channel "open" in order to beble to send each and every command separately?
>
> Any hint is greatly appreaciated.
>
> guillermo