Search code examples
bashshellauthenticationsshtty

How to run a trunk of commands thru' SSH with login shell and not to print out commands


I have Node.js installed by NVM on server and it doesn't work with SSH. It shows node: command not found in non-login shell.

For example, this won't work

ssh server1 <<doc
    node -v
doc

This doesn't work too

ssh server1 <<<"bash -l; node -v"

The NVM init is inside .bashrc so I tried to source it but still not ok

ssh server1 <<doc
    source /etc/profile
    source ~/.bashrc
    node -v
doc

The only way to make it work is forcing ssh using TTY

ssh -tt server1 <<doc
    node -v
doc

However, the script is long and I don't wish to see the shell printing out every command. Even after add 'set +x' to top of script it still prints out every command.

And -tt will force TTY and the ssh command doesn't stop. Add 'exit' to the end of script is ok but still the issue of printing out command above.

How to do ssh without -tt and just as in login shell?


Solution

  • I found a way to run ssh -tt without printing out commands, kinda only:

    cat >~/script.tmp <<'DOC'
        stat / # Example command
        stat ~ # Example command
        # lots more commands
    DOC
    scp ~/script.tmp some.server:~
    ssh -tt some.server <<<"bash script.tmp; exit"