Net::SSH::CLI
Adds another layer on top of Net::SSH for a proper handling of CLI sessions which last longer than one command. This is especially usefull for enterprise Switches and Routers.
Installation
Add this line to your application's Gemfile:
gem 'net-ssh-cli'
And then execute:
$ bundle
Or install it yourself as:
$ gem install net-ssh-cli
Features
- provides an abstraction on top of the text-stream of a long living CLI sessions
- tries to be highly configurable
- has methods like #cmd and #dialog for common usecases
- offers waiting operations like #read_till
Usage
Net::SSH.start('host', 'user', password: "password") do |ssh|
cli = ssh.cli(default_prompt: /(\nuser@host):/m)
cli.cmd ""
# => "Last login: \nuser@host:"
cli.cmd "echo 'bananas'"
# => "echo 'bananas'\nbananas\nuser@host:"
end
net_ssh = Net::SSH.start('host', 'user', password: "password")
cli = Net::SSH::CLI::Session.new(net_ssh: net_ssh)
cli.cmd ""
cli = Net::SSH::CLI::Session.new(net_ssh_options: {host: 'host', user: 'user', password: 'password'})
cli.cmd ""
#cmd
cli = ssh.cli(default_prompt: /(\nuser@host):/m)
cli.cmd "echo 'bananas'"
# => "echo 'bananas'\nbananas\nuser@host:"
cli.cmd "echo 'bananas'", rm_command: true
# => "bananas\nuser@host:"
cli.cmd "echo 'bananas'", rm_prompt: true
# => "echo 'bananas'\nbananas"
cli.cmd "echo 'bananas'", rm_command: true, rm_prompt: true
# => "bananas"
Remove the command and the prompt for #cmd & #dialog by default
cli = ssh.cli(default_prompt: /(\nuser@host):/m, cmd_rm_command: true, cmd_rm_prompt: true)
cli.cmd "echo 'bananas'"
# => "bananas"
#dialog
cli.dialog "echo 'are you sure?' && read -p 'yes|no>'", /\nyes|no>/
# => "echo 'are you sure?' && read -p 'yes|no>'\nyes|no>"
cli.cmd "yes"
#read & #write
cli.write "echo 'hello'\n"
# => "echo 'hello'\n"
cli.read
# => "echo 'hello'\nhello\nuser@host:"
#write_n
cli.write_n "echo 'hello'"
# => "echo 'hello'\n"
#read_till
keep on processing till the stdout matches to given|default prompt and then read the whole stdin.
cli.write "\n"
# => "echo 'hello'\n"
cli.read_till
# => "echo 'hello'\nhello\nuser@host:"
This method is used by #cmd
Configuration
Have a deep look at Net::SSH::CLI::OPTIONS
at lib/net/ssh/cli.rb
Callbacks
The following callbacks are available
- #before_open_channel
- #after_open_channel
- #before_on_data
- #before_on_data
cli.before_open_channel do
puts "The channel will open soon"
end
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/swisscom/net-ssh-cli.
License
The gem is available as open source under the terms of the MIT License.