prevent ssh from executing result in shell

Hi,

I am writing a script on Solaris 10 and want to execute a remote ssh command. Normally this command should just return the value 0000000000002356 but when using ssh it seems it is passing the result to the shell to execute.

ssh root@10.5.112.145 `/usr/bin/nawk -F\, '$1=="USG" && $2=="01" && $3=="20" && $4=="2010" { print $11 }' /var/log/usage/lic.log | sort -rn | head -1`
sh: 0000000000002356: not found

I have tried various backticks and quotes to escape this but can't. Ssh here documents dont seem to work on Solaris...

bash-3.00# ssh root@10.5.112.145 <<EOF /usr/bin/nawk -F\, '$1=="USG" && $2=="01" && $3=="20" && $4=="2010" { print $11 }' /var/log/usage/lic.log | sort -rn | head -1 EOF
>

Yes, the first part tries to run the command "0000000000002356", but on the remote host. If you'd use regular quotes (') instead of backticks (`) the nawk command will be sent to, and executed, on the remote host. Until then, it's executed locally, and the result give to ssh to run elsewhere.

As for your second question: yes, ssh ignores pipes and here-docs for security reasons. Anything sent in that way will only be piped to a command running on the remote host, but never in an interactive session.

Thanks for that pludi,

So what can I do then?

I can't use normal quotes because they are required in the nawk command that I want to run. I have tried escaping them but it doesnt work.

Quoting and ssh: this is really painful, and your mileage will vary depending on used shell and ssh setup.

I found piping to be a usable workaround though. You may try something like this:

> cat > ./tmp.sh
awk -F: '{ print $1 }' /etc/passwd
^D
> ssh localhost < ./tmp.sh
Pseudo-terminal will not be allocated because stdin is not a terminal.
Warning: no access to tty (Bad file descriptor).
Thus no job control in this shell.
root
bin
daemon
adm
lp
sync
...

The errors/warning in the beginning are annoying of course - but you can remove them with

> ssh localhost < tmp.sh |& tac | head --lines=-3 | tac

Ugly, I know. You can also play with the -T and '-t -t' options of ssh - in some setups that seems to help.

I'd be happy if someone else knows a better solution :slight_smile: