Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSH command does not run from inside a bash script, but it does run from the shell

Tags:

linux

bash

shell

The ssh command does not run from inside a bash script, but it does run from the shell.

I created a simple script and it shows the ssh command fails before it reaches the remote machine.

The shell output shows the following:

  • The remote machine is up
  • The script is on localhost and the ssh command is in a function called doit
  • Executing the script returns error on line 5 (the line with the ssh command): "No such file or directory"
  • Copy and paste the ssh command to the shell, hit enter, it executes and reaches the remote machine, which returns and error because no key is setup (expected)

Output from the shell:

~ $ nping -c 1 104.248.173.32

Starting Nping 0.7.01 ( https://nmap.org/nping ) at 2019-04-23 22:04 ICT
libnsock mksock_bind_addr(): Bind to 0.0.0.0:0 failed (IOD #1): Invalid argument (22)
SENT (0.0016s) Starting TCP Handshake > 104.248.173.32:80
RECV (0.0017s) Handshake with 104.248.173.32:80 completed

Max rtt: 0.177ms | Min rtt: 0.177ms | Avg rtt: 0.177ms
TCP connection attempts: 1 | Successful connections: 1 | Failed: 0 (0.00%)
Nping done: 1 IP address pinged in 0.00 seconds
~ $ cat /tmp/test.sh
#!/usr/bin/env bash

function doit() {
  RUN="/usr/bin/ssh -o BatchMode=yes -o ConnectTimeout=3 [email protected] hostname"
  "$RUN"
  echo "RESULT: $?"
}

doit  
~ $ /tmp/test.sh
/tmp/test.sh: line 5: /usr/bin/ssh -o BatchMode=yes -o ConnectTimeout=3 [email protected] hostname: No such file or directory
RESULT: 127
~ $ /usr/bin/ssh -o BatchMode=yes -o ConnectTimeout=3 [email protected] hostname
Host key verification failed.
~ $ echo $?
255

I expected the ssh command in the script to attempt to run hostname on the remote machine, and for the remote machine to error because no key is setup.

What actually happened is that the ssh command in the script didn't run because there was a "No such file or directory" error.

Why am I seing a "No such file or directory" error when the ssh command is run from the script, even though the script is there, ssh is there, and the remote machine is there?

like image 542
vy218 Avatar asked Oct 15 '25 03:10

vy218


1 Answers

/usr/bin/ssh -o BatchMode=yes -o ConnectTimeout=3 root@ipaddress hostname

and

"/usr/bin/ssh -o BatchMode=yes -o ConnectTimeout=3 root@ipaddress hostname"

...are not the same. The former executes the command /usr/bin/ssh with the specified arguments. The latter treats the whole string, including the arguments, as the command name.

You did the former when running the command in the shell. However the doit function does the latter with "$RUN". You are getting "No such file or directory" because, indeed, there is no file named ssh -o BatchMode... (truncated for brevity) inside of /usr/bin.

Either execute the command directly if possible, or remove the quotes:

function doit1() {
  /usr/bin/ssh -o BatchMode=yes -o ConnectTimeout=3 root@ipaddress hostname
  echo "RESULT: $?"
}

function doit2() {
  # assuming there is some usefulness to using a variable in the actual script
  RUN="/usr/bin/ssh -o BatchMode=yes -o ConnectTimeout=3 root@ipaddress hostname"
  $RUN
  echo "RESULT: $?"
}
like image 89
cfillion Avatar answered Oct 17 '25 06:10

cfillion