Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6238503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:13:32+00:00 2026-05-24T11:13:32+00:00

Im trying to break the input and the processing into two parts so that

  • 0

Im trying to break the input and the processing into two parts so that the prompts don’t have to be specified (like wait_for). the problem is that the command is received however never processed. the channel process is called from the get_input thread (it prints the puts in the on_process) but nothing ever happens.

(the on_process is only called once after after the on_data is called then the channel just sits active? == true at this point still)

ENV['PATH'] = 'H:\mydocu~1\usrbin\ansi140\x86;' + ENV['PATH']
`ansicon.exe -p`

require 'net/ssh'

hostname = "server"
username = "user"
password = "password"

@data = ''
@print = true
@cmd = ''

#clear the log
File.open('ssh_command.log', 'w') {|f| f.write('')}


get_input = Thread.new { 
      while true
        @cmd = nonblocking_stdin_gets
        exit if @cmd.strip == "Q"
        File.open('ssh_command.log', 'a+') { |f| f.write("{#{@cmd}}") } 
        if @cmd.rstrip != '' or @cmd =~ /\r?\n/
          puts "sending..."
          @print = false
          @openchannel.process
        end
      end 
    }

if RUBY_PLATFORM =~ /win32/
  require 'Win32API'
  $win32_console_kbhit = Win32API.new("msvcrt", "_kbhit", [], 'I')
  def console_input_ready?
    $win32_console_kbhit.call != 0
  end
  def nonblocking_stdin_gets
    sleep(0.1) until console_input_ready?
    $stdin.gets # don't bother to use getc, it also blocks until user hits <return>
  end
else
  def nonblocking_stdin_gets
    $stdin.gets # it just works, on unix
  end
end

@openchannel = nil

Net::SSH.start(hostname, username, :password => password) do |session|

  @openchannel = session.open_channel do |channel|

    channel.on_data do |ch, data|
      @data += data
    end 

    channel.on_extended_data do |ch, type, data|
      puts "got stderr: #{data}"
    end

    channel.on_request "exit-status" do |ch, data|
      puts "process terminated with exit status: #{data.read_long}"
    end

    channel.on_open_failed { |ch, code, desc| puts "err: #{desc}" }

    # must come before shell
    channel.request_pty :term => "xterm" do |ch, success|
      if success
        puts "pty successfully obtained"
      else
        puts "could not obtain pty"
      end
    end

    channel.send_channel_request "shell" do |ch, success|
      if success
        puts "user shell started successfully"
      else
        puts "could not start user shell"
      end
    end

    channel.on_eof do |ch|
      puts "remote end is done sending data"
    end

    channel.on_close do |ch|
      puts "channel is closing!"
    end

    channel.on_process do |ch|
      print @data if @print
      puts "command:#{@cmd}"
      if @cmd.rstrip != '' or @cmd =~ /\r?\n/
        #system("cls")
        channel.send_data(@cmd.rstrip + "\r\n")
        @cmd = ''
        @print = true
      else
        puts "no command"
      end 
    end

  end #channel_end
  session.loop 
end #session_end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:13:33+00:00Added an answer on May 24, 2026 at 11:13 am

    so I figured it out, the processing event is only run after data is received, and the send_data only adds to the data queue

    so to make sure that the process event is run multiple times…
    add this

    session.loop(0.1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an input box that as you type displays a div called suggestions
we are running into an odd issue when trying to parse an input file.
I am trying to parse a string something like this: Input 20:00 - output
I’m trying to make something that will take lines of input from the user,
I'm trying to use a break statement in a for loop, but since I'm
Trying to get my css / C# functions to look like this: body {
Trying to find some simple SQL Server PIVOT examples. Most of the examples that
Trying to make a make generic select control that I can dynamically add elements
Trying to make a MySQL-based application support MS SQL, I ran into the following
Trying to do this sort of thing... WHERE username LIKE '%$str%' ...but using bound

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.