Unix commands work on server but not in ruby ssh session

前端 未结 3 1755
醉梦人生
醉梦人生 2020-12-06 23:44

I am trying to learn how to use the net-ssh gem for ruby. I want to execute the commands below, after I login to the directory - /home/james.

cd /
pwd
ls


        
相关标签:
3条回答
  • 2020-12-07 00:20

    And, Yey, I found how to do that, and in fact it is so simple. I think I did not get to this solution last time, because I was a little new to this thing of net-ssh, pty terminal. But yey, I found it finally, and here and example.

    require 'net/ssh'
    
    shell = {} #this will save the open channel so that we can use it accross threads
    threads = []
    # the shell thread
    threads << Thread.new do
      # Connect to the server
      Net::SSH.start('localhost', 'your_user_name', password: 'your_password') do |session|
        # Open an ssh channel
        session.open_channel do |channel|   
          # send a shell request, this will open an interactive shell to the server 
          channel.send_channel_request "shell" do |ch, success|    
            if success      
              # Save the channel to be used in the other thread to send commands
              shell[:ch] = ch
              # Register a data event
              # this will be triggered whenever there is data(output) from the server
              ch.on_data do |ch, data|
                  puts data
              end  
            end  
          end  
        end  
      end  
    end
    
    # the commands thread
    threads << Thread.new do
      loop do
        # This will prompt for a command in the terminal
        print ">"
        cmd = gets
        # Here you've to make sure that cmd ends with '\n'
        # since in this example the cmd is got from the user it ends with 
        #a trailing eol
        shell[:ch].send_data cmd
        # exit if the user enters the exit command
        break if cmd == "exit\n"
      end
    end
    
    threads.each(&:join)
    

    and here we are, an interactive terminal using net-ssh ruby gem. For more info look here its for the previous version 1, but it is so useful for you to understand how every piece works. And here

    0 讨论(0)
  • 2020-12-07 00:30

    This is because net/ssh is stateless, so it opens a new connection with each command execution. You can use the rye gem that implements a work around for this. but I do not know if it works with ruby > 2, since its development is not that active.

    Another way is to use a pty process, in which you'll open a pseudo terminal with the ssh command, than use the input and output files to write commands for the terminal and read the results. To read the results you need to use the select method of the IO class. But you need to learn how to use those utilities since its not that obvious for a non experienced programmer.

    0 讨论(0)
  • 2020-12-07 00:44

    It seems like every command runs on it's own environment, so the current directory is not carried over exec to exec. You can verify this if you do:

    exec('cd / && pwd', conn)

    It will print /. It is not clear from the documentation how to make all the commands execute on the same environment or if this is even possible at all.

    0 讨论(0)
提交回复
热议问题