Home > other >  Get and compare OpenSSH version in bash
Get and compare OpenSSH version in bash

Time:08-09

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?

CodePudding user response:

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/'

CodePudding user response:

every sub-version number you possibly want

ssh -V 2>&1 | mawk NF=NF FS='[._, ] ' OFS='\f'
OpenSSH
       8
        6p1
           LibreSSL
                   3
                    3
                     6

if u really want the patch number isolated too:

ssh -V 2>&1 | mawk 'sub("O\fen","Open",$!(NF=NF))^_'  FS='[._,p ] ' OFS='\f'
OpenSSH
       8
        6
         1
          LibreSSL
                  3
                   3
                    6

more vertically oriented :

 ssh -V 2>&1 | mawk 'sub(/\tLib/,"Lib",$!(NF=NF))'  FS='[._, ] ' OFS='\f\r\t'
OpenSSH
    8
    6p1
LibreSSL
    3
    3
    6

instead of having to hard-code in the seps, if u want a more generic one, FS='[^[:alnum:]] ' should work as well

CodePudding user response:

With bash and a regex:

major=8 ; minor=8

out=$(ssh -V 2>&1)
if [[ $out =~ _([0-9] )\.([0-9] ) && 
      ${BASH_REMATCH[1]} -ge $major && 
      ${BASH_REMATCH[2]} -ge $minor ]]; then
  echo "okay";
else
  echo "not okay";
fi

From help test:

-ge: greater-than-or-equal

CodePudding user response:

Another solution:

vers=($(ssh -V |& awk -F'[_.]' '{ print $2 " " $3 0 }'))
echo major: ${vers[0]}
echo minor: ${vers[1]}

To get a combined version number:

vers=$(ssh -V |& awk -F'[_.]' '{ print $2 "." $3 0 }')
echo $vers # e.g. "1.2"
  • Related