I have a server with an alias as follows:
~/.bash_aliases
alias update='sudo apt update && sudo apt -y upgrade'
Now I want to create a shortcut from my desktop that can simply trigger this script remotely over sshpass: For a simple login, it works as follows:
sshpass -p "<password>" ssh <user>@<host>
But adding the alias as ssh command does not work and simply closes the terminal without action:
sshpass -p "<password>" ssh <user>@<host> 'update'
Question: is it possible at all?
In my first example, I made it easy by turning the bash session into an interactive shell, which sets the
expand_aliases
shopt_option. There is also a case-statement at the beginning of .bashrc that disallows sourcing .bashrc in non-interactive shells, which in turn, means your .bash_aliases won't be sourced. Furthermore, even ifssh
will interpret this as a non-interactive session, you may use the-t
option to force a pseudo-terminal allocation for menus and screen-based applications to work.It can be accomplished in non-interactive shells, as well, but apt will still need a pseudo-terminal, though (can be solved by changing the frontend used by apt to a non-interactive choice).
Next is the use of
-p
passwords in sshpass that does not seem to die off.sshpass
should never be used in this way, ever. I can't see a scenario where this is needed. First, it will end up in yourhistory
, i.e., of course, you don't have it turned off. It may also, in some circumstances, end up in the ps tree. sshpass makes very little to hide the pass in this mode. You should at least use the file option orSSHPASS=
.passwd_credentials should only contain the password.
And is used by the
-f
option.You may also encapsulate your
ssh
command in a script file.Using
sshpass
will not trigger the server to load the aliases, but you can pass the commands like this:HOWEVER ... you're going to get a message like this:
Which means you'll need to do a very ugly thing to make the system do what you want:
This method is suboptimal for a number of reasons, but it will technically accomplish your goals. Depending on what you're trying to accomplish, it may make sense to look at using
unattended-upgrades
instead.