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?
/usr/bin/ssh -o BatchMode...instead of/usr/bin/sshwith arguments-o BatchMode.... Is theRUNvariable even necessary (I assume this snippet might have been extracted from a larger script where it's actually useful)?