0

I have 5 servers to manage (ssh, tunnel, etc). So is there a way/plugin to perform a free text search and execute a command. For example I map a sentence to a bash command and filter in terminal via free text search. I know about history search with ctrl + R command but its a bit cumbersome to search through all ips to find the relevant server.

For Example

Text : "ssh to server 1" >> "ssh nirojan@ipaddress"
Text : "tunnel to server 5" "ssh -L 5234:localhost:5433 "
  • 2
    Use more hostnames? – Panki Jun 3 at 10:57
  • Yes that's also a possibility ! I was also wondering whether any tool or plugin exist for this kind of operation. Thanks. – Nirojan Selvanathan Jun 3 at 10:59
2

If your shell supports aliases (see comments section), you could use aliases:

alias sshserv1='ssh root@5.8.66.33'
alias sshserv2='ssh root@1.4.6.8'

You would put these in your ~/.bashrc for example.

More info here: http://tldp.org/LDP/abs/html/aliases.html

Alternatively: the brilliant hstr tool (here: https://github.com/dvorka/hstr) is a great command history searcher and beats Ctrl+R on all fronts.

  • Thanks for the reference to the hstr tool, this is pretty cool. – Nirojan Selvanathan Jun 3 at 11:05
  • 1
    Note that aliases come from csh and are also found in all Korn-like shells, not only bash. In Bourne-like and rc-like shells, you can use functions instead. In any shell, you can use scripts. – Stéphane Chazelas Jun 4 at 5:53
  • @StéphaneChazelas thank you for pointing that out. Edited my answer to reflect that. – Edward Jun 4 at 6:25
4

For SSH specifically

Define host aliases in your SSH client configuration file ~/.ssh/config. For example:

Host server1
    Hostname ipaddress
    User nirojan

Host server5-tunnel
    Hostname server5
    LocalForward 5234:localhost:5433

Then ssh server1 is equivalent to nirojan@ipaddress, and so on. The advantage of this method is that it works with everything that uses SSH under the hood, for example rsync -a somefile server1:some/directory.

If you've enabled context-sensitive completion on bash or zsh, the shell will complete host aliases where you can use them with commands like ssh, sftp, rsync, etc.

Also, if you need to do the same thing on multiple servers, see Automatically run commands over SSH on many servers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.