Search code examples
bashversionopenssh

Get and compare OpenSSH version in bash


I am trying to compare the OpenSSH version to target v8.8 or higher I found that I can fetch the OpenSSH version with

ssh -V 2>&1 | awk '{print $1;}'

That returns, for example:

OpenSSH_7.6p1

But how can I isolate the major and minor version numbers to compare them?


Solution

  • With sed, major version number (exclude fixed text OpenSSH_ then capture any number of digits)

    ssh -V 2>&1 | sed 's/OpenSSH_\([0-9]\+\).*/\1/'
    

    and minor version number (exclude fixed text OpenSSH_ then exclude any number of digits and a dot, then capture any number of digits)

    ssh -V 2>&1 | sed 's/OpenSSH_[0-9]\+\.\([0-9]\+\).*/\1/'