I'd like to speed up connecting to specific servers.
I have the servers let's say:
123.123.123.1
123.123.123.2
123.123.123.3
I normally connect with the following:
ssh -p 12345 [email protected]
This is a pain because the only difference between the servers is the last number of the ip.
I tried the following code:
alias ssht='{ ip=$(cat -); ssh -p 12345 my_user@"123.123.123.$ip"; }<<<'
However I get an error:
karl@karls-laptop ~/scripts $ ssht 1
Pseudo-terminal will not be allocated because stdin is not a terminal.
Is there a way to get this working?
Use the intended way and write the options and aliases into
~/.ssh/config
:and so on...
And then connect just using
This calls for a function -- simple and robust, whereas an
alias
in this case would be fragile.Something like this should do:
The condition
[[ $1 =~ ^(1|2|3)$ ]]
makes sure you have entered one of 1, 2, 3 as first argument (any trailing argument is ignored).Now, you can give the desired last octet as the first argument:
Put this in your
~/.bashrc
for having it available in any interactive session.You can use patterns in
~/.ssh/config
. Fromman ssh_config
:Combined with:
So, in your
~/.ssh/config
, put:Then:
Use a function instead :
A better solution is to use a ssh
config
file:with some lines similar to:
You can also use ssh keys to improve the speed, finally you only run:
and you are connected to that server.
You don't even need to use a name like ssht. Names starting with digits, even only digits are valid hostnames in ssh config file.
Below works on Xubuntu Xenial
Part of my ~/.ssh/config
Command I run (below I added
-vv
for verbose logging toSTDOUT
a.k.a your screen by default)Output
Update - below is a hackish alternative solution, take it just for bashist exercise, maybe for quick and dirty stuff:
What it does
-L
,-R
,-D
), verbose mode (-vv
), no-TTY (-T
) etc.For example I want to start a socks proxy with no terminal
As you can see, no command prompt, it executed with
-vv
,-T
,-D 1080
arguments.I can verify tunnel (basically SOCKS5 proxy) on my machine, too