Reputation: 564
I'm trying to write in some functionality into the Net::SSH::Telnet Library for a project of mine. Specifically I need to be able to read the exit-status of any command that I write.
Here's the Original source from the Net::SSH::Telnet Library that I'm modifying
@buf = ""
@eof = false
@channel = nil
@ssh.open_channel do |channel|
channel.request_pty { |ch,success|
if success == false
raise "Failed to open ssh pty"
end
}
channel.send_channel_request("shell") { |ch, success|
if success
@channel = ch
waitfor(@options['Prompt'], &blk)
return
else
raise "Failed to open ssh shell"
end
}
channel.on_data { |ch,data| @buf << data }
channel.on_extended_data { |ch,type,data| @buf << data if type == 1 }
channel.on_close { @eof = true }
end
@ssh.loop
And now here are the changes I have made:
@stderr = ""
@exit_code = nil
@buf = ""
@eof = false
@channel = nil
@ssh.open_channel do |channel|
channel.request_pty { |ch,success|
if success == false
raise "Failed to open ssh pty"
end
}
channel.send_channel_request("shell") { |ch, success|
if success
@channel = ch
waitfor(@options['Prompt'], &blk)
return
else
raise "Failed to open ssh shell"
end
}
channel.on_data { |ch,data|
@stdout << data
@buf << data
}
channel.on_extended_data { |ch,type,data|
@stderr << data
@buf << data if type == 1
}
channel.on_request("exit-status") do |ch,data|
@exit_code = data.read_long
end
channel.on_close {
@eof = true
}
end
@ssh.loop
The problem is that when I run this, the @exit_code variable never changes from nil.
The reason I am doing this is because I need three major pieces of functionality: 1. Stateful (interactive) communication (so that if I cd into a directory the next command will occur in that directory) 2. The ability to read the exit status of any command I execute 3. The ability to respond to prompts
I selected Net::SSH::Telnet to adjust because it already offered 1 & 3. I figured I could fix it to support number 2.
If anyone can see how to fix this I would greatly appreciate it. The source for the rest of Net::SSH::Telnet is here:
https://github.com/jasonkarns/net-ssh-telnet/
and I have a forked version of the repo here to which I would gladly accept pull requests.
https://github.com/0x783czar/net-ssh-remotescript
Also if anyone has a suggestion of any other ruby libraries that would offer those three functionalities I would greatly appreciate any suggestions.
Upvotes: 1
Views: 1032
Reputation: 35443
You may want to take a look at the source for the Ruby "ssh" gem and "remote_task" gem.
The relevant source code is here:
It has code for handling the "cd" command, i/o channels, and the exit status.
Upvotes: 1