Reputation: 2010
Here's what I'm trying to do:
ssh andy@<ip_address> "cat .bash_aliases; sayhello"
Here's what happens:
alias sayhello="echo hello"
bash: sayhello: command not found
To be more specific about my problem I'm trying to invoke the command
"sudo etherwake -i eth0 <mac_address>"
over ssh -- this executes (I think) on my local computer, giving a sudo: unable to resolve host [blabla]
error. It seems like any commands that are not standard bash commands are parsed by my local machine.
If that's what's happening, how do I get around it? If not, what is the explanation?
Upvotes: 9
Views: 6275
Reputation: 2010
@ajreal gave a simple solutions in the above comments -- just put what you want to happen in a file, then execute the file.
So I created a file on the host called sayhello.sh
(containing only the line echo Hello
), then on my local machine used
ssh andy@<ip_address> "sh sayhello.sh"
Upvotes: 1
Reputation: 6158
Well, it's a pretty messed up hack, but it works:
$ ssh xxx.xxx.158.40 'source aliases; alias'
alias emacs='emacs -nw'
alias l.='ls -d .*'
alias ll='ls -l'
alias sayhello='echo hello'
alias vi='vim'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
$ ssh xxx.xxx.158.40 'source aliases; $( alias sayhello | sed -e "s/.$//" -e "s/.*=.//" )'
hello
This also works:
ssh xxx.xxx.158.40 "source aliases; \$( alias sayhello | cut -d\' -f2 )"
Upvotes: 0
Reputation: 17169
In general this is not a good idea to use aliases in scripts.
Yet I can suggest one way to do it, but bare in mind how unsafe it is.
eval
, Here we go.
ssh remote_host "shopt -s expand_aliases ; source ~/.bash_aliases ; eval sayhello"
By default alias expansion is enabled only for interactive shells. To turn it on use shopt -s
command.
You will need to source the aliases into your shell context anyway.
Now you are set to use your aliases via the eval
command.
Upvotes: 16