{"id":21888602,"url":"https://github.com/swisscom/net-ssh-cli","last_synced_at":"2025-04-15T10:19:35.882Z","repository":{"id":56885363,"uuid":"170866803","full_name":"swisscom/net-ssh-cli","owner":"swisscom","description":"A library to make interactive SSH sessions more convenient.","archived":false,"fork":false,"pushed_at":"2023-04-07T11:50:52.000Z","size":109,"stargazers_count":7,"open_issues_count":1,"forks_count":3,"subscribers_count":16,"default_branch":"master","last_synced_at":"2025-03-28T19:11:25.845Z","etag":null,"topics":["cli","console","interactive-shell","ssh"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/swisscom.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-02-15T13:10:54.000Z","updated_at":"2024-01-03T19:58:41.000Z","dependencies_parsed_at":"2022-08-20T14:31:18.496Z","dependency_job_id":null,"html_url":"https://github.com/swisscom/net-ssh-cli","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swisscom%2Fnet-ssh-cli","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swisscom%2Fnet-ssh-cli/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swisscom%2Fnet-ssh-cli/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swisscom%2Fnet-ssh-cli/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/swisscom","download_url":"https://codeload.github.com/swisscom/net-ssh-cli/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248837197,"owners_count":21169374,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cli","console","interactive-shell","ssh"],"created_at":"2024-11-28T11:16:03.352Z","updated_at":"2025-04-15T10:19:35.854Z","avatar_url":"https://github.com/swisscom.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Net::SSH::CLI\n\nAdds 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.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'net-ssh-cli'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install net-ssh-cli\n\n## Features\n\n - provides an abstraction on top of the text-stream of a long living CLI sessions\n - tries to be highly configurable\n - has methods like #cmd and #dialog for common usecases\n - offers waiting operations like #read_till\n\n## Usage\n\n```ruby\nNet::SSH.start('host', 'user', password: \"password\") do |ssh|\n  cli = ssh.cli(default_prompt: /(\\nuser@host):/m)\n  cli.cmd \"\"\n  # =\u003e \"Last login: \\nuser@host:\"\n\n  cli.cmd \"echo 'bananas'\"\n  # =\u003e \"echo 'bananas'\\nbananas\\nuser@host:\"\nend\n```\n\n```ruby\n  net_ssh = Net::SSH.start('host', 'user', password: \"password\")\n  cli = Net::SSH::CLI::Session.new(net_ssh: net_ssh)\n  cli.cmd \"\"\n```\n\n```ruby\n  cli = Net::SSH::CLI::Session.new(net_ssh_options: {host: 'host', user: 'user', password: 'password'})\n  cli.cmd \"\"\n```\n\n### #cmd\n```ruby\n  cli = ssh.cli(default_prompt: /(\\nuser@host):/m)\n  cli.cmd \"echo 'bananas'\"\n  # =\u003e \"echo 'bananas'\\nbananas\\nuser@host:\"\n  cli.cmd \"echo 'bananas'\", rm_command: true\n  # =\u003e \"bananas\\nuser@host:\"\n  cli.cmd \"echo 'bananas'\", rm_prompt: true\n  # =\u003e \"echo 'bananas'\\nbananas\"\n  cli.cmd \"echo 'bananas'\", rm_command: true, rm_prompt: true\n  # =\u003e \"bananas\"\n  cli.cmd \"echo 'bananas'\", rm_command: true, rm_prompt: true, minimum_duration: 9\n  # =\u003e \"bananas\"\n  cli.cmd \"echo 'bananas'\", rm_command: true, rm_prompt: true, prompt: /\\nuser@host:/m\n  # =\u003e \"bananas\"\n  cli.cmd \"echo 'bananas'\", rm_command: true, rm_prompt: true, timeout: 60\n  # =\u003e \"bananas\"\n```\n\nRemove the command and the prompt for #cmd \u0026 #dialog by default\n```ruby\n  cli = ssh.cli(default_prompt: /(\\nuser@host):/m, cmd_rm_command: true, cmd_rm_prompt: true)\n  cli.cmd \"echo 'bananas'\"\n  # =\u003e \"bananas\"\n```\n\nYou can define a timeout for a `#cmd` in order to avoid hanging commands. The timeout gets passed into the underlying function #read_till.\nThis is usefull in case your prompt won't match because of an unexpected behaviour or undefined behaviour. For example some form of unexpected dialog. \nThe underlying implementation is using a soft timeout because `Timeout.timeout` is dangerous. In order to deal anyway with hanging low level issues, `Timeout.timeout` is used too, but with a higher value than the soft timeout.\n\n```ruby\n  cli = ssh.cli(default_prompt: /(\\nuser@host):/m, read_till_timeout: 11)\n  cli.cmd \"echo 'bananas'\"                      # timeout is set to 11\n  # =\u003e \"bananas\"\n  cli.cmd \"echo 'bananas'\", timeout: 22         # timeout is set to 22\n  # =\u003e \"bananas\"\n  cli.cmd \"sleep 33\", timeout: 22               # timeout is set to 22\n  # Net::SSH::CLI::Error::CMD\n```\n\n### #cmds\n\nIt's the same as `#cmd` but for multiple commands.\n\n```ruby\n  cli.cmds [\"echo 'bananas'\", \"echo 'apples'\"], rm_command: true, rm_prompt: true\n  # =\u003e [\"bananas\", \"apples\"]\n```\n\n### #dialog\n\nUse this method to specify a differnt 'prompt' for once. This is perfect for interactive commands. \n\n```ruby\n  cli.dialog \"echo 'are you sure?' \u0026\u0026 read -p 'yes|no\u003e'\", /\\nyes|no\u003e/\n  # =\u003e \"echo 'are you sure?' \u0026\u0026 read -p 'yes|no\u003e'\\nyes|no\u003e\"\n  cli.cmd \"yes\"\n```\n\n```ruby\ncli.dialog \"passwd\", /Current Password:/i\ncli.dialog \"Old Password\", /New Password:/i\ncli.dialog \"New Password\", /Repeat Password:/i\ncli.cmd \"New Password\"\n```\n\n### #impact\n\nThe very same as `#cmd` but it respects a flag whether to run commands with 'impact'.\nThis can be used in a setup where you don't want to run certain commands under certain conditions.\nFor example in testing. \n\n```ruby\n  cli.run_impact?\n  # =\u003e false\n  cli.impact \"reboot now\"\n  # =\u003e \"skip: 'reboot now'\"\n```\n\n```ruby\n  cli.run_impact = true\n  cli.impact \"reboot now\"\n  # =\u003e connection closed\n```\n\n### #read \u0026 #write\n\n```ruby\n  cli.write \"echo 'hello'\\n\"\n  # =\u003e \"echo 'hello'\\n\"\n  cli.read\n  # =\u003e \"echo 'hello'\\nhello\\nuser@host:\"\n```\n\n### #write_n\n```ruby\n  cli.write_n \"echo 'hello'\"\n  # =\u003e \"echo 'hello'\\n\"\n```\n\n### #read_till\nkeep on processing till the stdout matches to given|default prompt and then read the whole stdin.\n```ruby\n  cli.write \"echo 'hello'\\n\"\n  # =\u003e \"echo 'hello'\\n\"\n  cli.read_till\n  # =\u003e \"echo 'hello'\\nhello\\nuser@host:\"\n```\n\nThis method is used by #cmd, see ``lib/net/ssh/cli.rb#cmd``\n\n### #read_for\n\n```ruby\n  cli.write_n \"sleep 180\"\n  # =\u003e \"\"\n  cli.read_for(seconds: 181)\n  # =\u003e \"...\"\n```\n\n## Configuration\n\nHave a deep look at the various Options configured by ``Net::SSH::CLI::OPTIONS`` in ``lib/net/ssh/cli.rb``\nNearly everything can be configured.\n\n\n### Callbacks\n\nThe following callbacks are available\n - before_open_channel\n - after_open_channel\n - before_on_stdout\n - after_on_stdout\n - before_on_stdin\n - after_on_stdin\n\n```ruby\ncli.before_open_channel do\n  puts \"The channel will open soon\"\nend\n```\n\n```ruby\ncli.after_open_channel do\n  cmd \"logger 'Net::SSH::CLI works'\"\n  cmd \"sudo -i\"\nend\n```\n\nUsing the callbacks you can define a debugger which shows the `stdout` buffer content each time new data is received.\n\n```ruby\ncli.after_on_stdout do\n  warn stdout\nend\n```\n\n```ruby\ncli.after_on_stdout do\n  puts \"the following new data arrived on stdout #{new_data.inspect} from #{hostname}\"\nend\n```\n\nor convert new lines between different OS\n```ruby\ncli.after_on_stdout do\n  stdout.gsub!(\"\\r\\n\", \"\\n\")\nend\n```\n\nor hide passwords\n```ruby\ncli.after_on_stdout do\n  stdout.gsub!(/password:\\S+/, \"\u003cHIDDEN\u003e\")\nend\n```\n\nor change the stdin before sending it\n```ruby\ncli.before_on_stdin do\n  content.gsub(\"\\n\\n\", \"\\n\")\nend\n```\n\n### #hostname #host #to_s\n\n```ruby\n  cli.to_s\n  # =\u003e \"localhost\"\n  cli.hostname\n  # =\u003e \"localhost\"\n  cli.host\n  # =\u003e \"localhost\"\n```\n\n### #detect_prompt\n\nNET::SSH::CLI can try to guess the prompt by waiting for it and using the last line.\nThis works usually, but is not guaranteed to work well.\n\n```ruby\n  cli.open_channel\n  # =\u003e ...\n  cli.detect_prompt(seconds: 3)\n  # =\u003e \"[my prompt]\"\n```\n\n### An outdated view of all available Options\n\nPlease check the file `lib/net/ssh/cli.rb` `OPTIONS` in order to get an up-to-date view of all available options, flags and arguments.\n\n```ruby\n      OPTIONS = ActiveSupport::HashWithIndifferentAccess.new(\n        default_prompt:            /\\n?^(\\S+@.*)\\z/,                             # the default prompt to search for\n        cmd_rm_prompt:             false,                                        # whether the prompt should be removed in the output of #cmd\n        cmd_rm_command:            false,                                        # whether the given command should be removed in the output of #cmd\n        cmd_rm_command_tail:       \"\\n\",                                         # which format does the end of line return after a command has been submitted. Could be something like \"ls\\n\" \"ls\\r\\n\" or \"ls \\n\" (extra spaces)\n        run_impact:                false,                                        # whether to run #impact commands. This might align with testing|development|production. example #impact(\"reboot\")\n        read_till_timeout:         nil,                                          # timeout for #read_till to find the match\n        read_till_hard_timeout:    nil,                                          # hard timeout for #read_till to find the match using Timeout.timeout(hard_timeout) {}. Might creates unpredicted sideffects\n        read_till_hard_timeout_factor: 1.2,                                      # hard timeout factor in case read_till_hard_timeout is true\n        named_prompts:             ActiveSupport::HashWithIndifferentAccess.new, # you can used named prompts for #with_prompt {} \n        before_cmd_procs:          ActiveSupport::HashWithIndifferentAccess.new, # procs to call before #cmd \n        after_cmd_procs:           ActiveSupport::HashWithIndifferentAccess.new, # procs to call after  #cmd\n        before_on_stdout_procs:    ActiveSupport::HashWithIndifferentAccess.new, # procs to call before data arrives from the underlying connection \n        after_on_stdout_procs:     ActiveSupport::HashWithIndifferentAccess.new, # procs to call after  data arrives from the underlying connection\n        before_on_stdin_procs:     ActiveSupport::HashWithIndifferentAccess.new, # procs to call before data is sent to the underlying channel \n        after_on_stdin_procs:      ActiveSupport::HashWithIndifferentAccess.new, # procs to call after  data is sent to the underlying channel\n        before_open_channel_procs: ActiveSupport::HashWithIndifferentAccess.new, # procs to call before opening a channel \n        after_open_channel_procs:  ActiveSupport::HashWithIndifferentAccess.new, # procs to call after  opening a channel, for example you could call #detect_prompt or #read_till\n        open_channel_timeout:      nil,                                          # timeout to open the channel\n        net_ssh_options:           ActiveSupport::HashWithIndifferentAccess.new, # a wrapper for options to pass to Net::SSH.start in case net_ssh is undefined\n        process_time:              0.00001,                                      # how long #process is processing net_ssh#process or sleeping (waiting for something)\n        background_processing:     false,                                        # default false, whether the process method maps to the underlying net_ssh#process or the net_ssh#process happens in a separate loop\n        on_stdout_processing:      100,                                          # whether to optimize the on_stdout performance by calling #process #optimize_on_stdout-times in case more data arrives\n        sleep_procs:               ActiveSupport::HashWithIndifferentAccess.new, # procs to call instead of Kernel.sleep(), perfect for async hooks\n      )\n```\n\n## Development\n\nAfter 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.\n\nTo 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](https://rubygems.org).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/swisscom/net-ssh-cli.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fswisscom%2Fnet-ssh-cli","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fswisscom%2Fnet-ssh-cli","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fswisscom%2Fnet-ssh-cli/lists"}