SSH script alias problem

Forum rules
Before you post please read how to get help
ztrzwh
Level 1
Level 1
Posts: 2
Joined: Sun Sep 10, 2017 5:53 pm

SSH script alias problem

Postby ztrzwh » Sun Sep 10, 2017 6:04 pm

Hi. I have a problem in creating alias in my subnet network with mikrotik routers (192.168.0.0/16) which can use ssh connection. The problem is that when I try to create an alias (alias ssl='ssh lary@192.168.') and after typing the last 2 digit of the ip address it says that can not find ip address 192.168.
$ ssl 13.152 (192.168.13.152) gives me an error. Is there any way that this think go on. With other alias is working find actually. I made alias install='sudo apt-get install' and when I type in terminal $ install grep is working. What is wrong with that

User avatar
karlchen
Level 18
Level 18
Posts: 8027
Joined: Sat Dec 31, 2011 7:21 am
Location: Germany

Re: SSH script alias problem

Postby karlchen » Sun Sep 10, 2017 6:32 pm

Hello, ztrzwh.

The issue is that the argument which you pass to your alias ssl will be appended with a leading blank between the expanded alias and the argument.
Means: ssl 13.152 yields this expanded commandline ssh lary@192.168. 13.152
Of course, the IP address 192.168. cannot be found.

Regards,
Karl
Image
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.

ztrzwh
Level 1
Level 1
Posts: 2
Joined: Sun Sep 10, 2017 5:53 pm

Re: SSH script alias problem

Postby ztrzwh » Sun Sep 10, 2017 6:47 pm

So to make it work I have to backspace that space which shell creates. Is there a way to do that?

rene
Level 6
Level 6
Posts: 1366
Joined: Sun Mar 27, 2016 6:58 pm

Re: SSH script alias problem

Postby rene » Mon Sep 11, 2017 11:22 am

shell functions are a better match in this case: they can take parameters. That is, if you create a file ~/.bashrc that contains

Code: Select all

ssl() {
    /usr/bin/ssh lary@192.168.$1
}

you have what you want (after . ~/.bashrc or relaunching the terminal); you can also put it directly in ~/.profile. Note that you can leave out "lary@" if "lary" is in fact your username on the local system as well.

A better answer might consist of the advise to add 192.168.13.52 and other machines you wish to connect to by name to /etc/hosts so that that you can just say ssh lary@host1, ssh lary@host2 and so on.... but other than that go wild. Shell functions are far more flexible than aliases.


Return to “Scripts & Bash”