8

I have /root/test.sh on a remote server, which has the following content:

#!/bin/bash
date

On the remote server, I get the following output:

# ./test.sh
Fri Dec 18 07:41:10 EST 2015

# bash <(cat /root/test.sh)
Fri Dec 18 07:41:23 EST 2015

However, if I try to run the same commands from my local machine, I get the following result:

$ ssh root@remote_server /root/test.sh
Fri Dec 18 07:44:32 EST 2015

$ ssh root@remote_server bash <(cat /root/test.sh)
cat: /root/test.sh: No such file or directory
bash: /dev/fd/63: No such file or directory

So what's the problem of the last run? You might find it weird to run a script like this bash <(cat /root/test.sh). The reason I did it is because it is a simplified version of my real situation. My real code is to download a Bash script from the Internet and run it locally. I have a lot of machines to run, with different parameters, so I'm trying to run them from my local computer.

Qian Chen
  • 799
  • 1
    Try ssh root@remote_server /bin/cat test.sh, for the simplified version. – Hastur Dec 18 '15 at 13:04
  • 1
    This is actually pretty similar to http://unix.stackexchange.com/questions/248345/passing-variable-from-one-script-to-another and is more-or-less suffering from the same problem I think. You don't really need process substitution as you might see from the other question – Eric Renouf Dec 18 '15 at 13:27

3 Answers3

13

You can't pass file descriptors like this over ssh. <(...) construction creates virtual files on your system and it does not have a meaning when executing on remote system.

If you really want to use it, put it into the quotes, which will evaluate on the remote system, if you have bash there

ssh root@remote_host "bash <( cat /root/test.sh )"
Jakuje
  • 21,357
4

My suggestion is to do a script on the remote host (/root/test.sh) make it executable (chmod u+x /root/test.sh) and run in the following way

ssh root@remote_server /bin/bash -c  /root/test.sh

About /bin/bash: /dev/fd/63: No such file or directory
You can read reasons on this answer [1].

Note:
Take the good habit to use the full path (/bin/bash) instead of only the command name (bash) to avoid unneeded security risks [2].


Another way should be

ssh root@remote_server << EOF
cat  /root/test.sh
# whatever else
EOF

It will execute each line until the second EOF... you can do your variants about it...

Hastur
  • 2,355
1

I recently faced the same problem, but then I realized that it only happens if you run with sudo/root user.

If I run:

sudo bash <(echo "It worked")

I get:

bash: /dev/fd/63: No such file or directory

But if I run:

bash <(echo "It worked")

It works just fine.

Ruben Alves
  • 151
  • 1
  • 6
  • Issue is related to ssh not to sudo – Archemar Apr 30 '21 at 07:30
  • Well, I tried the same command locally with sudo (without ssh) and remotely with ssh (with and without root user) and the problem was always when running with sudo, regardless of ssh. – Ruben Alves Apr 30 '21 at 21:36