🇨🇿 cs

Reading output of remote command with Dropbear ssh client

Today I faced strange behavior of Dropbear SSH client (dbclient) on my home OpenWRT home router. Before anything else, I would like to note that I do not have the recent version, but the version included in my OpenWRT installation. Exactly, it’s Dropbear v0.53.1. But most distributions do not have recent versions so I hope this will remain usefull to others.

I have a shell script which uses ssh client to read some information from my other server. Imagine following shell code snippet:

result=`ssh -i key_file user@server remote_command` && {
  echo "Obtained information is $result"
}

This code will connect to remote machine and run remote_command which produces some output. This output will be assigned to variable $result. If remote command succeeds (returns exit code 0) the message with obtained value will be printed.

For particular reason, this snippet did not work if the whole script was run by cron. I googled a lot. I found some issues in Dropbear mailing list including quite dirty but working solution.

The reason is that script run by cron has no standard input available. Due to this, dbclient does not read any output from remote command either, so the $result variable is set empty and no error code is returned.

This behavior can be simulated from running shell as

ssh -i key_file user@server echo "foo" </dev/null

Nothing will be returned. Note that this modification does not affect OpenSSH client behavior (SSH implementation on most “big” Linux machines).

The quickest solution is to “create some stdin to ssh client”, redirect from /dev/zero is enough. Following code works:

result=`ssh -i key_file user@server remote_command </dev/zero` && {
  echo "Obtained information is $result"
}

Now the whole script works either standalone or as a cronjob.

Comments