| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
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.
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
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 "" 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"It's the same as #cmd but for multiple commands.
cli.cmds ["echo 'bananas'", "echo 'apples'"], rm_command: true, rm_prompt: true
# => ["bananas", "apples"]Use this method to specify a differnt 'prompt' for once. This is perfect for interactive commands.
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"cli.dialog "passwd", /Current Password:/i
cli.dialog "Old Password", /New Password:/i
cli.dialog "New Password", /Repeat Password:/i
cli.cmd "New Password"The very same as #cmd but it respects a flag whether to run commands with 'impact'. This can be used in a setup where you don't want to run certain commands under certain conditions. For example in testing.
cli.run_impact?
# => false
cli.impact "reboot now"
# => "skip: 'reboot now'" cli.run_impact = true
cli.impact "reboot now"
# => connection closed cli.write "echo 'hello'\n"
# => "echo 'hello'\n"
cli.read
# => "echo 'hello'\nhello\nuser@host:" cli.write_n "echo 'hello'"
# => "echo 'hello'\n"keep on processing till the stdout matches to given|default prompt and then read the whole stdin.
cli.write "echo 'hello'\n"
# => "echo 'hello'\n"
cli.read_till
# => "echo 'hello'\nhello\nuser@host:"This method is used by #cmd, see lib/net/ssh/cli.rb#cmd
cli.write_n "sleep 180"
# => ""
cli.read_for(seconds: 181)
# => "..."Have a deep look at the various Options configured by Net::SSH::CLI::OPTIONS in lib/net/ssh/cli.rb Nearly everything can be configured.
The following callbacks are available
cli.before_open_channel do
puts "The channel will open soon"
endcli.after_open_channel do
cmd "logger 'Net::SSH::CLI works'"
cmd "sudo -i"
endUsing the callbacks you can define a debugger which shows the stdout buffer content each time new data is received.
cli.after_on_stdout do
warn stdout
endcli.after_on_stdout do
stdout.gsub!("\r\n", "\n")
end cli.to_s
# => "localhost"
cli.hostname
# => "localhost"
cli.host
# => "localhost"NET::SSH::CLI can try to guess the prompt by waiting for it and using the last line. This works usually, but is not guaranteed to work well.
cli.open_channel
# => ...
cli.detect_prompt(seconds: 3)
# => "[my prompt]"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.
Bug reports and pull requests are welcome on GitHub at https://github.com/swisscom/net-ssh-cli.
The gem is available as open source under the terms of the MIT License.
| Back | FazBrowse Home | New Git URL |