Code Monkey home page Code Monkey logo

hss's Introduction

hss

Gem Version GitHub Workflow Status MIT Licensed

SSH helper that uses regex and fancy expansion to dynamically manage SSH shortcuts

Installation

gem install hss
ln -s /path/to/your/config.yml ~/.hss.yml

SCP configuration

This script can be used by SCP and other things that use SSH as a transport. To use it with SCP, just specify hss for the -S flag:

scp -S hss host:files/ other_host:location/

To use it with rsync, use the -e flag:

rsync -e hss files/ host:location/

You can alias this for the greater good:

alias pcs='scp -S hss'
alias cnysr='rsync -e hss'

Caveat: Because of how hss processes hosts, it will only operate on the first host-like thing in your command. As such, using scp to copy a local file to an hss'd host will work, as will the inverse, and copying a file from an hss'd host to a normal host works, but not the inverse.

Configuration

  1. You can put your configuration in ~/.hss.yml or another file specified by the "HSS_CONFIG" environment variable. You can refer to multiple config files in "HSS_CONFIG" by separating them with colons and the configs will be merged (HSS_CONFIG=$HOME/.hss-shared.yml:$HOME/.hss-mine.yml).
  2. The only required configuration section is "patterns", which contains an array of hashes with the following attributes:
  • note: Name of this pattern ("my servers")
  • example: Shows how this is used ("jimbo -> [email protected]")
  • short: Regex for shortcut ("^(jimbo|bob|chuck)$")
    • Use matching to collect things you want to use in the expanded form
  • long: Expanded form of this shortcut ("root@#$1.example.org")
    • This will be evaluated using Ruby's string interpolation, so you can use "#{var}", "#$1", "#{function()}", etc.
  1. Helpers are available for use in long forms
  • expand(x): Uses the "expansions" section of the config. Format for this section is a hash of lists, as such:
expansions:
    expanded_form:
        - short_form1
        - short_form2
        - shrtfrm3
  • shortcut(x): Uses the "shortcuts" section of the config. Format for this section is a hash of strings, as such
shortcuts:
    short1: expand to this
    other_short: 'expand to something else!'
  • command(x): Runs the given string as a command and uses the output for the expanded form
  • default(x, y): If x is not nil, uses x. If it's nil, use y
  • external(source, key): Loads source as a YAML file and looks for the given key. The YAML should be made of hashes (you can nest them) and the key should be dot-separated:
fish:
    color: blue # accessible with key = 'fish.color'
    alpha:
        dog: sparky
        cat: grouchy # accessible with key 'fish.alpha.cat'

Usage

  1. Run hss help for a list of available commands
  2. Run hss $command to do that thing

Falling back to another command

If you define the environment variable "HSS_PASS", hss will fall back to that command if it fails to find a match:

# export HSS_PASS='ssh'

# hss [email protected]
PTY allocation request failed on channel 0
Hi akerl! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.

Changing the default command

By default, hss prepends "ssh " to the long command from your configuration file. If you're using something different (like mosh), you can set HSS_COMMAND in your environment to override that.

Debugging

If you want hss to print the command it would have run rather than executing it, you just need to set the HSS_DEBUG environment variable to something:

export HSS_DEBUG=foo
hss bar # will print rather than exec

License

hss is released under the MIT License. See the bundled LICENSE file for details.

hss's People

Contributors

akerl avatar akerl-cultivator[bot] avatar displague avatar ravenx99 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

hss's Issues

scp example doesn't describe limitations of host expansion

The scp line demonstrates host:file other_host:location/. I think it is important to mention that other_host will not be processed by hss in this example and must be a resolvable name on host.

Success - hss_host -> localfile

displague@split ~ % scp -S hss h15:/tmp/t/y /tmp/u     
y

Success - hss_host -> host

displague@split ~ % scp -S hss h15:/tmp/t/y split:/tmp/q/

Fails - hss_host -> hss_host

displague@split ~ % scp -S hss h15:/tmp/t/y h15:/tmp/q/
ssh: Could not resolve hostname h15: Name or service not known
lost connection

Fails - host -> hss_host

displague@split ~ % scp -S hss split:/tmp/t/y h15:/tmp/q/
/var/lib/gems/2.1.0/gems/hss-0.2.11/lib/hss.rb:43:in `handle': Couldn't find a matching host for: split (NameError)
    from /var/lib/gems/2.1.0/gems/hss-0.2.11/bin/hss:22:in `<top (required)>'
    from /usr/local/bin/hss:23:in `load'
    from /usr/local/bin/hss:23:in `<main>'

Allow passthrough to SSH

Right now I have this bash function, but I think it might be useful as a core HSS feature:

function hss {
command hss "$@" || ssh "$@"
}

I'd like to use HSS for every command so I don't have to think about which command to use. I'm not sure if I'm representative of a huge percent of users, so maybe this should be an option in the YAML file?

better long_form templating

I should be able to include shortcuts within shortcuts and other nested magics

This will probably involve using an explicit assignment rather than using the magic $# variables from the regexp

Won't Execute over POSH/Windows Terminal

I want to get HSS to work in a windows environment. I was able to install ruby and I can gem the script over but it doesn't want to run.

    PS C:\Users\username> gem install hss
    Successfully installed hss-1.0.1
    Parsing documentation for hss-1.0.1
    Done installing documentation for hss after 0 seconds

I believe I have made the symbolic link to a config file correctly which would be the equivalent to your installation step:

`New-Item -ItemType SymbolicLink -Path "C:\Users\username\.hss.yml" -Target "C:\Users\username\hss-config\admin_config.yml"`

However I don't think that's related to the problem (yet). Although it is installed and my environment acknowledges the application, it will execute but returns error citing missing paths:

    PS C:\Users\username> hss help
    C:/Users/username/.local/share/gem/ruby/3.2.0/gems/hss-1.0.1/lib/hss.rb:57:in `rescue in load_config': Failed to load config:  (RuntimeError)
    from C:/Users/username/.local/share/gem/ruby/3.2.0/gems/hss-1.0.1/lib/hss.rb:51:in `load_config'
    from C:/Users/username/.local/share/gem/ruby/3.2.0/gems/hss-1.0.1/lib/hss.rb:29:in `initialize'
    from C:/Users/username/.local/share/gem/ruby/3.2.0/gems/hss-1.0.1/bin/hss:5:in `new'
    from C:/Users/username/.local/share/gem/ruby/3.2.0/gems/hss-1.0.1/bin/hss:5:in `<top (required)>'
    from C:/Ruby32-x64/bin/hss:32:in `load'
    from C:/Ruby32-x64/bin/hss:32:in `<main>'
    C:/Users/username/.local/share/gem/ruby/3.2.0/gems/hss-1.0.1/lib/hss.rb:53:in `read': No such file or directory @ rb_sysopen - C (Errno::ENOENT)
    from C:/Users/username/.local/share/gem/ruby/3.2.0/gems/hss-1.0.1/lib/hss.rb:53:in `block in load_config'
    from C:/Users/username/.local/share/gem/ruby/3.2.0/gems/hss-1.0.1/lib/hss.rb:53:in `load_config'
    from C:/Users/username/.local/share/gem/ruby/3.2.0/gems/hss-1.0.1/lib/hss.rb:29:in `initialize'
    from C:/Users/username/.local/share/gem/ruby/3.2.0/gems/hss-1.0.1/bin/hss:5:in `new'
    from C:/Users/username/.local/share/gem/ruby/3.2.0/gems/hss-1.0.1/bin/hss:5:in `<top (required)>'
    from C:/Ruby32-x64/bin/hss:32:in `load'
    from C:/Ruby32-x64/bin/hss:32:in `<main>'

I can gem other scripts like rails and it is a painless install. This one however is being troublesome. What am I missing in my ruby or powershell config? I think the problem might be that the output is using forward slash ( / ) notation but is not adapted to Microsoft's martian back slash ( \ )directory notation. Could that be the cause? Do you know how I can make fixes?

Fix hideous eval line

If you know how to fix this to be less haxxory, please tell me!

eval '"' + long_form + '"'

It needs to expand variables which were hidden in long_form when the expansion happens.

Support multiple configs

I'm thinking HSS_CONFIG should support being read like PATH: "/path1:/other/path:/usr/bin/place"

Unit tests

I need a way to confirm stuff works:

  • various arg orders
  • ssh
  • scp
  • each helper
  • system ruby vs homebrew

hss fails with ControlPersist enabled

Enabling ControlPersist for * hosts in ~/.ssh/config causes hss to fail to connect with the following error:

Bad packet length 1397966893.
Disconnecting: Packet corrupt

"external" helper

Should be able to pull a value from an external source; preferably, from a YAML file based on a key match or the full file if no key is specified

Proper moduleness

Started working on this in the templateness branch, needs to be completed

MoSH Support?

Would MoSH support be something doable for HSS?

Particularly because currently I have a few servers behind a NAT where I'm using a customer SSH port as well as a custom MoSH port range. HSS magic would be awesome.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.