
Show HN: Net:SSH:CLI – A Gem to Work with CLI Sessions - faebi
https://github.com/swisscom/net-ssh-cli
======
faebi
I just wanted to share my simple gem I am using at work for a while now.
Originally I was using a custom version of net-ssh-telnet for a long time. I
use this gem to handle long-running SSH CLI Sessions on Network Equipment
which can't handle multiple Channels per SSH connection. In an ideal world
this gem wouldn't be needed, but here I am. I use this gem for various scripts
and apps to get and write data to our equipment. The main problem this gem
tries to solve is to handle the state of an unreliable text stream. It has
various methods to work with the current buffer and to wait for a certain
output. In my case I can't just rely on looking for a prompt on the CLI
session since there are many inconsistencies on the CLI implementations.

It's used on equipment of multiple vendors but sometimes I just use it to run
a few commands on our servers.

I assume there are many better ways to implement this buffer-handling and
waiting logic. I also found it quite hard to get the performance right and to
deal with net-ssh itself. To be honest, I wouldn't consider this my best work
at all, but it does it's job nicely and is a pleasure to use compared to
everything else I have seen.

You can try it yourself:

    
    
      gem install net-ssh-cli
    
    
      require 'net/ssh/cli'
      require 'readline'
      Net::SSH.start('localhost', ENV["USER"], password: Readline.readline("Enter your password:")) do |ssh|
        cli = ssh.cli(default_prompt: /(#{ENV["USER"]}.*\z)/) # lets hope your prompt contains your username
        puts cli.cmd "ls -alh"
        puts cli.cmd "echo 'hello hackernews reader'"
      end

