Help understanding what this ls -l command is checking in a script

Hello,

we have a script that has the two following lines:

ssh -qno StrictHostKeyChecking=no -o ConnectTimeout=1 user@IP 'ls -l /home/opsmgrsvc >/dev/null 2>&1' > /dev/null 2>&1
 status="$(echo $?)"

I can't understand what these two lines are doing?
When I execute the first line nothing is output. I get that the command is being directed somewhere that is not shown. And when I execute the echo $? line I get a number. Not sure what this number means though? Is this command checking if I can successfully login to this server or not?

Also, is there a way to concatenate the two lines into one command line instruction to return the value?

Thank you.

The command connects to a remote server, does and ls -l of a directory, which list files in the directory on the remote machine. The command is between the tic characters '

The output of the ls command goes into the bit bucket (kind of like a black hole for output). The status of the ssh connect and ls (success ( == 0 ) or failure code (== non-zero) ) is stored in the variable $status .

A sort of roundabout way to see if the remote machine is awake on the network, and if the directory exists over there. The options -qno for ssh:
-n used to run in the background when ssh does not read from user's stdin (terminal)
-q quiet mode, stops extra messages
-o (option) turn off strict host key checking. I wonder why...

1 Like

Thanks very much Jim for this quick reply.

This script was created to inventory hundreds of servers I suspect they used the -o command to turn off strict host key checking so the script wouldn't get stuck logging into a new server. I'm adding ssh passwordless to our script and will not need that switch in the future.

Thank you for explaining what this command is doing. is there a list of return codes (non zero) that denote what we couldn't get in? For now 0 being success on logging in and non-zero meaning fail is probably enough for us.

Is there a way to concatenate the two commands to save the result of the first command into the status variable all on one line? I've tried something like:

ssh -qno StrictHostKeyChecking=no -o ConnectTimeout=1 user@IP 'ls -l /home/opsmgrsvc >/dev/null 2>&1' > /dev/null 2>&1 &&  status="$(echo $?)"

And then I tried using:

echo $status

to see what was in the variable but it was empty. Am I close in putting this into one line?

whatever command ; status="$?"

In a standard shell the status variable is $?
In csh it is $status .
bash is a standard shell, and also has $status as a synonym for $?

tyler@gentoo ~ $ echo $SHELL
/bin/bash
tyler@gentoo ~ $ echo $status

tyler@gentoo ~ $ echo $?
0
tyler@gentoo ~ $

My bad, bash does not have $status.
It is zsh that has $status as a synonym for $?

1 Like