Worth, Kevin
2008-Oct-28 17:25 UTC
[Net-ssh-users] Requesting shell channel using net-ssh2
Looks like I was able to answer my own question again... appears the below
works. The only trick was that I had to make sure to properly exit the shell
(thus the exit, exit, y below) because otherwise the program sits and hangs
forever.
Still would be nice if someone was able to implement a "shell" state
machine to add a layer of abstraction, but I think this might do the trick for
me for now.
-Kevin
#!/usr/bin/ruby
require ''net/ssh''
Net::SSH.startmyhostname, myusername, :password=>mypassword) do |ssh|
ssh.open_channel do |channel|
channel.request_pty do |pty, success|
if success #pty
puts "pty successfully obtained"
pty.send_channel_request "shell" do |sh, success|
if success #sh
puts "user shell started successfully"
###This is where I start inserting commands I want to run
channel.send_data "\n" #get a prompt
channel.send_data "no page\n" #disable paging
channel.send_data "help\n" #example command that
produces output
channel.send_data "exit\n"
channel.send_data "exit\n"
channel.send_data "y" #confirm exit "y/n"
prompt
###After this last command completes, I want the script to exit.
sh.on_data do |ch, data|
print data
end #on_data
sh.on_extended_data do |ch, type, data|
puts "got stderr: #{data}"
end #on_extended_data
sh.on_close do |ch|
puts "channel is closing!"
end #on_close
sh.on_eof do |ch|
puts "eof received!"
channel.close
end #on_eof
else
puts "could not start user shell"
end #if success sh
end #pty send_channel_request shell do
else
puts "could not obtain pty"
end #if success pty
end #channel request_pty do
end #open_channel
ssh.loop
end #ssh.start
From: Worth, Kevin
Sent: Tuesday, October 28, 2008 4:52 PM
To: ''net-ssh-users at rubyforge.org''
Subject: Requesting shell channel using net-ssh2
A network device I''m trying to use net-ssh to script doesn''t
seem to support the "exec" call. When I tried to request a shell (I
pulled the example from the "send_channel_request" rdoc), I got the
message that I had to request a pty first. This led me to the below code, which
is mostly pulled from examples in the rdoc. This code logs into the device, and
appears to start a shell successfully, but it then loops forever because in my
"on_data" callback, I have a "send_data" call. Am I going
about this in any way that resembles correctness?
Would sure be nice if a full "shell" capability were available like in
1.x or else the examples contained enough information that making a shell-like
environment in which you have all the control you needed was easy. Something
like the two examples in http://net-ssh.rubyforge.org/ssh/v1/chapter-5.html#s2
would be really helpful.
-Kevin
require ''net/ssh''
Net::SSH.start(myhostname, myusername,
:password=>mypassword,:verbose=>:info) do |ssh|
ssh.open_channel do |channel|
channel.request_pty do |pty, success|
if success
puts "pty successfully obtained"
else
puts "could not obtain pty"
end
end
channel.send_channel_request "shell" do |sh, success|
if success
puts "user shell started successfully"
else
puts "could not start user shell"
end
end
channel.on_data do |ch, data|
puts "got stdout: #{data}"
channel.send_data "\n"
end
channel.on_extended_data do |ch, type, data|
puts "got stderr: #{data}"
end
channel.on_close do |ch|
puts "channel is closing!"
end
channel.send_data("\nhelp")
end
ssh.loop
end
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://rubyforge.org/pipermail/net-ssh-users/attachments/20081029/6146ce06/attachment.html>