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 "
Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232

2 Answers2

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

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.

Edward
  • 2,509