SSH script alias problem

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
ztrzwh

SSH script alias problem

Post by ztrzwh »

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
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
karlchen
Level 23
Level 23
Posts: 18173
Joined: Sat Dec 31, 2011 7:21 am
Location: Germany

Re: SSH script alias problem

Post by karlchen »

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
The people of Alderaan have been bravely fighting back the clone warriors sent out by the unscrupulous Sith Lord Palpatine for 762 days now.
Lifeline
ztrzwh

Re: SSH script alias problem

Post by ztrzwh »

So to make it work I have to backspace that space which shell creates. Is there a way to do that?
rene
Level 20
Level 20
Posts: 12240
Joined: Sun Mar 27, 2016 6:58 pm

Re: SSH script alias problem

Post by rene »

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.
Locked

Return to “Scripts & Bash”